AdamSteele Posted December 8, 2019 Share Posted December 8, 2019 (edited) Hello, i have a query that returns the rows of certain parts of a databse. i made a button on the end of each echo, to make sure that, the button being printed with each echo, correlates to that echo. However, the SESSIONS only return the same varaibles each time. The last in the list. $sql = "SELECT p.Title, p.PerfDate, p.PerfTime FROM performance AS p INNER JOIN production AS r ON p.Title = r.Title"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo '<tr> <td>'.$row['Title'].'</td> <td>'.$row['PerfDate'].'</td> <td>'.$row['PerfTime'].'</td> <td><button name="availability" value="'.$row['Title'].'">Availability</button></td> </tr>'; $_SESSION["Title"] = $row['Title']; $_SESSION["PerfDate"] = $row['PerfDate']; $_SESSION["PerfTime"] = $row['PerfTime']; } } else { echo "0 results"; } $_SESSION["name"] = $_GET["name"]; ?> How am i able to make it so each button correlates to each row? There's a screenshot of how it looks for reference Edited December 8, 2019 by AdamSteele Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/ Share on other sites More sharing options...
requinix Posted December 8, 2019 Share Posted December 8, 2019 $_SESSION["Title"] = $row['Title']; $_SESSION["PerfDate"] = $row['PerfDate']; $_SESSION["PerfTime"] = $row['PerfTime']; How do you expect this to work when you are dealing with multiple rows? What do you think it will do? Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572362 Share on other sites More sharing options...
AdamSteele Posted December 8, 2019 Author Share Posted December 8, 2019 15 minutes ago, requinix said: $_SESSION["Title"] = $row['Title']; $_SESSION["PerfDate"] = $row['PerfDate']; $_SESSION["PerfTime"] = $row['PerfTime']; How do you expect this to work when you are dealing with multiple rows? What do you think it will do? Hello, sorry im completely new to PHP, i was expecting that the row that the button was printed on would correlate to the $row result, and therefore each button would return the corresponding row's that it was printed with. However i'm not sure how to achieve that past what i tired to do Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572363 Share on other sites More sharing options...
mac_gyver Posted December 8, 2019 Share Posted December 8, 2019 why are you storing these values in session variables at the point where you are displaying the choices? what purpose does this serve? 2 hours ago, AdamSteele said: ON p.Title = r.Title your database design should use identifiers (ids) (auto-increment integer primary indexes) when storing related data. 2 hours ago, AdamSteele said: <button name="availability" value="'.$row['Title'].'">Availability</button> your 'add to cart' buttons should uniquely identify the performance date and time. i recommend that the value they submit should be a unique id from your database table (see the point directly above this one.) submitting the title only tells you which show was selected, so, the current method would not tell you which date and time was picked. Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572364 Share on other sites More sharing options...
requinix Posted December 8, 2019 Share Posted December 8, 2019 1 hour ago, AdamSteele said: Hello, sorry im completely new to PHP, i was expecting that the row that the button was printed on would correlate to the $row result, and therefore each button would return the corresponding row's that it was printed with. However i'm not sure how to achieve that past what i tired to do You're expecting way, way too much from it. PHP isn't a human being. It can't look at all your code at once and understand how every little thing is connected. It definitely won't be able to know that a particular button that you outputted is related to some particular code, let alone what the code was doing at a very specific moment while it was running. If you want things to work a certain way then you must be absolutely explicit about it every step of the way. Want a button to correspond to a certain row? Make sure that the button itself includes some value from the row, then write code that takes the value and looks up the row data. Then think about what you've done and ask yourself whether you've told PHP to do exactly what you wanted to happen. Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572365 Share on other sites More sharing options...
AdamSteele Posted December 8, 2019 Author Share Posted December 8, 2019 7 minutes ago, mac_gyver said: why are you storing these values in session variables at the point where you are displaying the choices? what purpose does this serve? your database design should use identifiers (ids) (auto-increment integer primary indexes) when storing related data. your 'add to cart' buttons should uniquely identify the performance date and time. i recommend that the value they submit should be a unique id from your database table (see the point directly above this one.) submitting the title only tells you which show was selected, so, the current method would not tell you which date and time was picked. Hello. Im storing them there, so that on a different page (Which the button opens once it is clicked) the choice they picked comes up, and from that they can pick a seat based off that specific production. The Date and Time being picked doe's work in correlation to the button, it shows the title, date and time appropriately to the correct title. However, only for the very bottom of the list. See the attached image. It doesn't display these outputs based on the button that is clicked in terms of the row, however just the bottom of the list. But does display the variables themselves correctly, just not the correct ones. Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572366 Share on other sites More sharing options...
AdamSteele Posted December 8, 2019 Author Share Posted December 8, 2019 10 minutes ago, requinix said: You're expecting way, way too much from it. PHP isn't a human being. It can't look at all your code at once and understand how every little thing is connected. It definitely won't be able to know that a particular button that you outputted is related to some particular code, let alone what the code was doing at a very specific moment while it was running. If you want things to work a certain way then you must be absolutely explicit about it every step of the way. Want a button to correspond to a certain row? Make sure that the button itself includes some value from the row, then write code that takes the value and looks up the row data. Then think about what you've done and ask yourself whether you've told PHP to do exactly what you wanted to happen. how would i go about linking each individual echo'd out button to the row it was printed out with? I really have no clue im sorry, the code above was my best attempt at doing so Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572367 Share on other sites More sharing options...
requinix Posted December 8, 2019 Share Posted December 8, 2019 12 hours ago, AdamSteele said: how would i go about linking each individual echo'd out button to the row it was printed out with? Did you see the part of mac_gyver's post about having the button submit a unique ID from your database? Quote Link to comment https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/#findComment-1572375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.