0

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

<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

wygekas
  • 1
  • 4
  • Please go read [ask]. You should to describe to us what your actual, specific problem is, rather than just showing us some code snippets and explaining what you “want”. – misorude Feb 17 '20 at 12:04
  • As stated above, problem is that i can't change query depending on selected item which is pulled from data base – wygekas Feb 17 '20 at 12:11
  • What do you mean, you “can’t”? I don’t see you even _try_ anywhere. You are not doing anything in that code, with the passed `page` parameter. – misorude Feb 17 '20 at 12:14
  • All bad code will only make harder to understand what im trying to accomplish. As i said i was trying to asign query result from first snippet to global variable, and then use it in second snippet query. But it was not working. I was hoping someone would show how to assign query result to global variable, and then use it in different query. – wygekas Feb 17 '20 at 12:18
  • I have suspicion that maybe query is instantly processed, and when i click to assign value to global variable it is too late to send it to another query – wygekas Feb 17 '20 at 12:22
  • If you want to send the value of `$modeliai_results['Modelis']` to `standartiniskatalogas.php` when that link is clicked, then you need to start by adding this to the query string of the URL in the href attribute of your link there. – misorude Feb 17 '20 at 12:23
  • _“I have suspicion that maybe query is instantly processed, and when i click to assign value to global variable”_ - you don’t have any global variables here. (And those would not help you in the first place, because we are not talking about one singular script instance running here, we are talking about passing values gathered in one script via the client on to another script. Those two completely separate and independent scripts don’t “share” any variables to begin with, these are two completely different requests.) – misorude Feb 17 '20 at 12:25

1 Answers1

-1

My mistake was that i concentrated on "onclick" event which would set global variable and that global variable would be used in query. Problem was that, php with java script not so big friends and would create not linkable URLs to same content. The solution was to use simple thing "$_GET". Assign $_GET to some php variable, and then use it in "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

 <?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
         ?>
wygekas
  • 1
  • 4
  • [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/q/7537377/1839439) – Dharman Feb 18 '20 at 17:55