Click to See Complete Forum and Search --> : transferring loops value to the next page
Hi,
I've added a counter to identify tickboxes and dropdown options in the following code. I am not sure how I can display on the next page the values in the rows where checkboxes are ticked and display the option selected from each dropdown in these rows:
while ($g = mysql_fetch_assoc($result2)) {
$options .= "<option value='".$g["modcode"]."'>".$g["modcode"]."</option>";
}
$counter = 1;
while ($i = mysql_fetch_assoc($result)){
echo "<form action='shoppingcart.php' method='post'>";
echo "<tr><td>".$i["room_name"]."</td>";
echo "<td>".$i["s_date"]."</td>";
echo "<td>".$i["s_start_time"]."</td>";
echo "<td>".$i["s_end_time"]."</td>";
echo "<td>".$i["cost_per_ses"]."</td>";
echo "<td>".$i["b_indicator"]."</td>";
echo "<td><select name='moducode".$counter."'>";
echo $options;
echo"</select></td>";
echo "<td><input type='checkbox' name ='booking".$counter."' value = 'book'></td></tr>";
$counter++;
}
I am thinking to do something along the following pseducode lines in the next page but I am not sure what I should base my loop on:
for each of the rows displayed from this loop in the previous page: while ($i = mysql_fetch_assoc($result)){
if ($_POST[moducode] != " ") {
echo "<td>".$i_POST[room_name]."</td>";
echo "<td>".$i_POST[s_date]."</td>";
etc
NogDog
08-15-2006, 01:01 PM
Maybe add a...
echo "<input type='hidden' name='count' value='$counter'>";
...after the loop ends and before the </form> tag?
replica
08-15-2006, 02:42 PM
On the data selection page...
You're pulling data from a database so I'm guessing you have a unique constraint (auto_increment as the primary key) or something uniquely identifying each row.
I'd use this instead of the $counter variable if possible. Actually that would be essential to this working. =)
On the data display(ing) page...
While checking the POST data from the form submission
$selectedRows = array();
if( isset( $_POST['FormSubmittingButton'] ) )
{
//first get all selected rows
foreach( $_POST as $key => $value )
{
if( preg_match( "/^booking/", $key )
{
//create a new array index with the
//selected row's index and add a default option value
$selectedRows[ ''. subtring( $key, 7 ) ] = -1; //the '' forces it to be a string
}
}
//now get the option values
foreach( $_POST as $key => $value )
{
if( preg_match( "/^moducode/", $key )
{
//push the selected option value onto the row
//containing the same index
$selectedRows[ ''. subtring( $key, 8 ) ] = $value;
}
}
}
now $selectedRows should be an associative array containing unique IDs and option values for those IDs
I'd then use the information inside $selectedRows to build a sql query, something like this
$inQuery = '';
$count = 0;
foreach( $selectedRows as $key => $value )
{
if( $count++ > 0 ) $inQuery .= ',';
$inQuery .= "'" . $key . "'";
}
$sql = "
SELECT *
FROM WhateverTable
WHERE uniqueID IN( $inQuery )";
$res = mysql_query( $sql );
then for each $row = mysql_fetch_assoc( $res ) the selected option value will be
print '
<td>' . $row['Something'] . '</td>
<td>' . $row['Whatever'] . '</td>
<td>' . $selectedRows[ $row['uniqueID'] ] . '</td>
';
I haven't tested any of that. Just top of my head stuff but maybe it'll give you some ideas.
I don't entirely understand the process above - my form is collecting data from 2 queries, so the unique id will not apply here?. Anyhow, I've tried something similar to your code and it works however on the displaying page, ONLY one row of the rows where a checkbox is selected is displayed. Although when I tested I checked 3 boxes in the form. I think there's a loop or something small missing in my displaying page that will print everything that was selected.
My posting page (where the form is):
$options = null;
while ($g = mysql_fetch_assoc($result2)){
$options .= "<option value='".$g["modcode"]."'>".$g["modcode"]."</option>\n";
}
//hidden field to hold the student id
echo "<input type=hidden name=studid value='".$g["student_id"]."'>";
$counter = 1;
while ($i = mysql_fetch_assoc($result)){
echo "<form action='shoppingcart.php' method='post'>";
echo "<tr><td>".$i["room_name"]."</td>";
//hidden field to hold the room name
echo "<input type=hidden name=rmname value=".$i["room_name"]." >";
//hidden field to hold the room code
echo "<input type=hidden name=rmcode value=".$i["r_code"]." >";
//echo "<td>".$i["session_id"]."</td>";
echo "<td>".$i["room_number"]."</td>";
//hidden field to hold the room Number
echo "<input type=hidden name=rmnum value=".$i["r_number"]." >";
echo "<td>".$i["s_date"]."</td>";
//hidden field to hold the session date
echo "<input type=hidden name=sesdate value=".$i["s_date"]." >";
echo "<td>".$i["s_start_time"]."</td>";
//hidden field to hold the session start time
echo "<input type=hidden name=ses_stime value=".$i["s_start_time"]." >";
echo "<td>".$i["s_end_time"]."</td>";
//hidden field to hold the session id
echo "<input type=hidden name=sessid value=".$i["session_id"]." >";
echo "<td>".$i["room_cost_per_session"]."</td>";
echo "<td>".$i["booking_indicator"]."</td>";
echo "<td><select name='moducode".$counter."'>";
echo $options;
echo"</select></td>";
echo "<td><input type='checkbox' name ='booking".$counter."' value = 'book'></td></tr>";
echo"</tr>";
}
echo "</table>\n";
echo "<input type='submit' name='submit' value='Book'></form>";
And the displaying page:
foreach ($_POST as $key => $val) {
if (substr($key, 0, 7) == "booking") {
$counter = str_replace("booking", "", $key);
echo "<p>The module you selected is <b>".$_POST[moducode.$counter]."</b>";
echo "<p>The room number you selected is <b>".$_POST[rmnum]."</b>";
echo "<p>The room code you selected is <b>".$_POST[rmcode]."</b>";
echo "<p>The session date you selected is <b>".$_POST[sesdate]."</b>";
echo "<p>The student id you selected is <b>".$_POST[sessid]."</b>";
}
}
replica
08-15-2006, 05:18 PM
I was just suggesting using the unique ID's from what you have named the `$result` result set as the indexes of an associative array so that you can cut down on iterating through it but it's not really necessary.
$counter = 1;
while ($i = mysql_fetch_assoc($result)){
echo "<form action='shoppingcart.php' method='post'>";
try moving the echo "<form..." outside the loop. I think this is creating a form for every record you're pullin in.
Good point thanks, I've mode the echo "<form>... outside the loop but that didn't make any difference.
I still get the result for one row only, even though 3 rows are ticked :(
By the way, the row that always get printed is the last row in the form. Does that mean something?
Sorry, I did a few more tests. The result printed is always the last row in the form (even if the checkox is not ticked for that row)
The form contains up to 3 rows depending on the user selection from previous page. I tested with one row, 2 rows and 3 rows. The one row form works fine. But if the form contains 2 or 3 rows, the last row always get displayed even if the checkbox for that row was not selected.
Help please - I am going mad over here. I have a major deadline and spent most of my time working on this issue - I still have a lot more to do
replica
08-15-2006, 06:17 PM
I notice a couple of things upon closer inspection.
1. you use the counter to make the <input /> name values unique but you aren't itterating it.
2. You're holding a lot of `session` data in those hidden fields. Why not just use the $_SESSION instead of passing them through $_POST?
Oh thanks, incrementing the counter now causes the display of as many rows as there are in the form. However, it just repeats the values for the last row. So if there are 2 rows in the form, the last row gets printed twice. If the form has 3 rows, the last row gets printed 3 times (even if the checkbox for it is not ticked)
About sessions, do you mean: s_start_time, s_date, session_id? This data is actually coming from tables they don't have anything to do with php session.
My system is a conference room booking system, each room has sessions associated with it - e.g., 9:00AM (s_start_time) to 12:00PM (s_end_time) on 2006-08-16 (s_date). That's why I had to create the relevant tables for those
Some progress....I think I found where the problem is but I am unable to fix it.
All the rows details from the form are displayed correctly except the session_id and s_start time (thes are the values that are repeated).
I think the problem is in my SQL query! Can you have a look please.
Basically the user selects up to 3 sessions (or none) in a previous page. My SQL query then gets the data for the sessions posted. This is then used in the form above. I think my problem is that the query checks that the session start time entered is ONE of 3, and it does not check if all 3 sessions are entered for example.
This is my code (the rest of it starts from my previously posted code).
if (!$_POST[s_time1] && !$_POST[s_time2] && !$_POST[s_time3]) {
$query = ("SELECT m_room.r_code, m_room.room_name, m_room_details.r_number, m_session.session_id,
s_date, s_start_time, session_end_time,
room_cost_per_session,
m_room_session.booking_indicator
FROM m_room, m_room_details,
m_room_session, m_session
WHERE m_room.r_code = m_room_details.r_code
AND m_room_session.r_code = m_room_details.r_code
AND m_room_session.r_number = m_room_details.r_number
AND m_room_session.session_id = m_session.session_id
AND m_session.s_date = '".$PostedDate."'
AND m_room.room_name= '".$_POST[s_room]."' ");
}
else {
$query = ("SELECT m_room.r_code, m_room.room_name, m_room_details.r_number, m_session.session_id,
s_date, s_start_time, session_end_time,
room_cost_per_session, m_room_session.booking_indicator
FROM m_room, m_room_details, m_room_session,
m_session
WHERE m_room.r_code = m_room_details.r_code
AND m_room_session.r_code = m_room_details.r_code
AND m_room_session.r_number = m_room_details.r_number
AND m_room_session.session_id = m_session.session_id AND m_session.s_start_time
IN ('".$_POST[s_time1]."', '".$_POST[s_time2]."', '".$_POST[s_time3]."')
AND m_session.s_date = '".$PostedDate."'
AND m_room.room_name= '".$_POST[s_room]."' ");
}
$query2 = ("SELECT student_id, m_module.module_code as
modcode, mod_points FROM m_module, m_student_module
WHERE m_module.module_code = m_student_module.module_code
AND m_student_module.student_id ='".$_SESSION[s_stud_id]."'");
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$result2 = mysql_query($query2) or die('Query failed miserably: ' . mysql_error());
$numfields2 = mysql_num_fields($result2);
if (!$result2) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query2;
die($message);
}
//else {
echo "<p><table border=1 bordercolor=blue cellspacing=0 cellpadding=3>";
echo "<tr><td><b>Room Name</b></td>
<td><b>room number</b></td>
<td><b>session id</b></td>
<td><b>booking date</b></td>
<td><b>session starts</b></td>
<td><b>session ends</b></td>
<td><b>points cost</b></td>
<td><b>booked?</b></td>
<td><b>select module</b></td>
<td><b>tick to book</b></td></tr>";
echo "<form action='shoppingcart.php' method='post'>";
Hi,
I've solved this at last. I've added the counter to each of the form fields and everything is displaying correctly.
i.e.
//hidden field to hold the session id
echo "<input type=hidden name='sessid".$counter."' value=".$i["session_id"]." >";
Many thanks for all your help
replica
08-16-2006, 12:13 PM
ahh good. I just got back into work and noticed you'd posted back. Well done.