简体   繁体   English

如何进行php动态查询

[英]How to make php dynamic queries

im trying to change query depending on what user has selected.我试图根据用户选择的内容更改查询。 In my case there are displayed two photos with labels pulled from data base.在我的情况下,显示了两张带有从数据库中提取的标签的照片。 And depending which image is clicked i want that label to be used in query who will display items associated in database with that label.并且根据单击的图像,我希望该标签用于查询谁将显示数据库中与该标签关联的项目。 I tried using global variables, but it i didn't work for me.我尝试使用全局变量,但它对我不起作用。 I cant figure out how to insert data into query after user clicked something我不知道如何在用户单击某些内容后将数据插入到查询中

There is code fragment where i pull photos with labels from database, and depending on which i select i want "$modeliai_results['Modelis']" (vehicle model label) to be sent and used in another query below有代码片段,我从数据库中提取带有标签的照片,根据我选择的内容,我希望发送“$modeliai_results['Modelis']” (车辆模型标签)并在下面的另一个查询中使用

<div class="eile">
    <?php
    echo '
    <a href="standartiniskatalogas.php?page=kategorijos"/>
    <img src="data:image/jpeg;base64,'.base64_encode( $modeliai_results['Nuotrauka'] ).'"/>
    <div class="description">'.$modeliai_results['Marke'].' '.$modeliai_results['Modelis'].'
    </div></a>';
?>

Page from second image which should execute query depending on selection from previous page:应该根据上一页的选择执行查询的第二张图片的页面:

<?php
    $kategorijos_sql="SELECT * FROM kategorijos (HERE SHOULD BE ADDED: "WHERE Modelis='MODEL NAME WHICH WAS SELECTED WHEN PRESSED ON IMAGE'";
    $kategorijos_query=mysqli_query($dbconnect, $kategorijos_sql);
    $kategorijos_results=mysqli_fetch_assoc($kategorijos_query);

do{
    echo '
    <div class="eile2">
    <a href="isore/isore.html">
    <img src="data:image/jpeg;base64,'.base64_encode( $kategorijos_results['Nuotrauka'] ).'"/>
    <div class="description">'.$kategorijos_results['Kategorija'].'</div>
    </a></div>'; /* uzdaro echo*/
    }
    while ($kategorijos_results=mysqli_fetch_assoc($kategorijos_query))
     ?>

ps.附: Im not using any extra tools or libraries.我没有使用任何额外的工具或库。

Selecting object Display data associated with object选择对象显示与对象关联的数据

My mistake was that i concentrated on "onclick" event which would set global variable and that global variable would be used in query.我的错误是我专注于“onclick”事件,该事件将设置全局变量并且该全局变量将用于查询。 Problem was that, php with java script not so big friends and would create not linkable URLs to same content.问题是,带有 java 脚本的 php 不是那么大的朋友,并且会为相同的内容创建不可链接的 URL。 The solution was to use simple thing "$_GET".解决方案是使用简单的东西“$_GET”。 Assign $_GET to some php variable, and then use it in "href".将 $_GET 分配给某个 php 变量,然后在“href”中使用它。 That way when you click on any kind of object with "a href="'.$variable.'"> bla bla /a>" you get redirected to page with added tail to URL from which you can access to use in other queries这样,当您单击带有 "a href="'.$variable.'"> bla bla /a>" 的任何类型的对象时,您会被重定向到添加了尾部到 URL 的页面,您可以从中访问以用于其他查询

 <?php
        $model= $_GET['model']; //variable from URL tail  
        $category= $_GET['category'];

        $category_sql="SELECT * FROM service WHERE Model='$model' AND Category='$category'"; //my SQL where i use variables selected from URL
        $category_query=mysqli_query($dbconnect, $category_sql);
        $category_results=mysqli_fetch_assoc($category_query);
        do{

            echo ' //print html together with php code
        <tr class="data">
            <td>
                <a href="catalogue.php? page=service&model='.$model.'&category='.$category.'&service='.$category_results['Name'].'"> //here i make href which when clicked will pass values named with $ sign
<img src="data:image/jpeg;base64,'.base64_encode( $category_results['Image'] ).'"/>//Here i take image from data base
<br><button>'.$category_results['Name'].'</button></a>
            </td>
            <td> <p>'.$category_results['Description'].'</p> //I take values from data base
            </td> 
            <td>'.$category_results['Price'].' &euro;</td>
        </tr>';
    }
    while ($category_results=mysqli_fetch_assoc($category_query)) //loop which cycles through all data base items
         ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM