Jump to content

Recommended Posts

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 

b41e8c2a7e628e48669b908656586ea7.png

Edited by AdamSteele
Link to comment
https://forums.phpfreaks.com/topic/309654-only-returns-end-value-of-row/
Share on other sites

$_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?

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

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.

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.

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.

1d0488b7a07b062cbbb2f28ab7d31c5d.png

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.