Forum Moderators: coopster

Message Too Old, No Replies

Session Variables

         

mikejrtt

8:55 pm on May 15, 2009 (gmt 0)

10+ Year Member



Hi

Please can anyone help. I am learning PHP at this stage and I am faced with a coding issue on a site that I have been asked to update.

My problem is as follows. I have the page displaying a table with information returned from a mySQL DB and the last 2 columns display radio boxes. These radio boxes are selected by the user as the user moves dow the rows in the table and the user may move to the next page to select further records as the result of the returned DB information displays over multiple pages.

My problem here is once the user selects the radio boxes on the one page and then moves to the next page and returns to the previous page the radio boxes are no longer selected. I need these radio boxes to remain selected all through the movement to the different pages and only once the user clicks the submit button should they no longer remain selected. I know you can do this with a Session Variable but I am not at all clued up about this and how to do it.

My code is as follows:

//function for uplifts page
function upliftheader ( $docType='V' ) {

$func = "upliftheader";
if ( $docType == 'P' ) {
$func = "upliftheader";
}
write_header(True);
# $string = preg_replace("/\r\nŠ\n\rŠ\nŠ\r/", "<BR>", $string);

// search the records using the order no
$search = "";
if (!isset($_REQUEST["search"]))
{
$search_st = "";
}
else
{
$search_st = "&search=" . $_REQUEST["search"];
$search = " AND `order_no` like '%" . $_REQUEST["search"] ."%' ";
}

if (!isset($_REQUEST["store_no"]))
{
$store_no_st = "";
}
else
{
$store_no_st = "&store_no=" . $_REQUEST["store_no"];
$search .= " AND `store_no` like '%" . $_REQUEST["store_no"] . "%' ";
}

//Sets the page size
if (!isset($_GET["start"])) { $start = 0; } else { $start = $_GET["start"]; }
if (!isset($_GET["page_size"])) { $page_size = 15; } else { $page_size = $_GET["page_size"]; }
if ($page_size != 15) { $page_size_st = "&page_size=" . $page_size; } else { $page_size_st = " "; }

//link to database
$link = mysql_connect(db_host, db_user, db_pass) or die("Could not connect");
mysql_select_db(database) or die("Could not select database");

//query the database

$query= "SELECT `id`, `principal` , `region` , `order_no` , `order_date` , `store_no` , `proc_date`, `proc_time`, `merge_date`, `merge_time`, `in_file`, `cases` " .
"FROM `order`".
"WHERE `principal` = ". principal ." ".
"AND `region` = '" . region . "' " . $search . " " .
"AND `doc_type` = 'P'";

//show the results
$result = mysql_query($query) or die( "Query failed: " . $query );
$num_rows = mysql_num_rows($result);

if ($start >= $num_rows) { $start = $num_rows - 1; }
$start = $start - ($start % $page_size);

//show the buttons at top of form
echo "<CENTER>";

echo "<FORM name=\"TheForm\">";

echo "<TABLE border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"40%\">\n";
echo "<TR><TD valign=\"middle\" align=\"right\">";
if ($start != 0)
{ $new_start = $start;
$new_start -= $page_size;
if ($new_start < 0) $new_start = 0;
echo "<A href=\"orders.php?func=$func&start=" . $new_start . $page_size_st . $search_st . $store_no_st . "\"><IMG src=\"images/nav_prev.gif\" alt=\"Previous\" border=\"0\" width=\"51\" height=\"17\"></A>\n";
}
else
{ echo "<IMG src=\"images/invis.gif\" alt=\"\" width=\"51\" height=\"17\">\n";}

echo "</TD><TD valign=\"middle\" align=\"center\">";

if ($num_rows >= $page_size)
{
echo "<SELECT class=\"input\" name=\"page\" onChange=\"document.location.href='orders.php?func=$func&start=' + this.options[this.selectedIndex].value + '". $page_size_st . $search_st . $store_no_st . "';\">\n";
for ($i=1; $i < ($num_rows / $page_size) + 1; $i++)
{
if (floor($start/$page_size) == ($i-1))
{
echo "<OPTION value=\"\" selected >Page " . $i . "</OPTION>\n";
}
else
{
echo "<OPTION value=\"" . (($i-1) * $page_size) . "\">Page " . $i . "</OPTION>\n";
}
}
echo "</SELECT>\n";
}
echo "</TD><TD valign=\"middle\" align=\"left\">";

if ($start < ($num_rows - $page_size))
{ echo "<A href=\"orders.php?func=$func&start=" . ($start + $page_size) . $page_size_st . $search_st . $store_no_st . "\"><IMG src=\"images/nav_next.gif\" alt=\"Next\" border=\"0\" width=\"51\" height=\"17\"></A>\n"; }
else
{ echo "<IMG src=\"images/invis.gif\" alt=\"\" width=\"51\" height=\"17\">";}

//show the form and table headers
echo "</TD></TR>\n";
echo "</TABLE>\n";
echo "</FORM>";

// keep the info that has been selected through page selections so it can be submitted to the database
if (isset( $_REQUEST[ "check"])) { $_SESSION[ "check" ] = $_REQUEST[ "check" ];}

echo "<FORM name=\"TheSearchForm\" action=\"orders.php?func=$func&start=" . $start . $page_size_st . "\" method=\"post\">\n";
echo "<TABLE border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"80%\" class=\"blackframe\">\n";
echo "<TH rowspan=\"2\" class=\"blackborder\" width=\"6%\">Order&nbsp;No.<BR><INPUT name=\"search\" value=\"". $_REQUEST["search"] . "\" size=\"7\" type=\"text\" class=\"input\" style=\"font-size: 11px;\"></TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"10%\">Date</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Store&nbsp;Name</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"6%\">Cases</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Processed<br />Date</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Processed<br />Time</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Authorised<br />Date</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Authorised<br />Time</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Incoming<br />File</TH>"
."<TH rowspan=\"2\" class=\"blackborder\" width=\"60%\">Outgoing<br />File</TH>"
."<TH colspan=\"2\" class=\"blackborder\" width=\"6%\">Authorise</TH>"
."<tr>"
."<TH class=\"blackborder\">Yes</TH>"
."<TH class=\"blackborder\">No</TH>"
."</tr>";

//set the row color variable
$class = "treven";

//set the paramaters and content of the rows in the table
$i = 0;
$j = 0;
$arr_y_n = array ("<IMG src=\"images/tick_yes.gif\" alt=\"Yes\" width=\"12\" height=\"12\">",
"<IMG src=\"images/tick_no.gif\" alt=\"No\" width=\"12\" height=\"12\">");

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

if ($j++ >= $start) {

printf ("<TR class=\"%s\"><TD class=\"greyborder\"><A href=\"orders.php?func=upliftdetails&id=%s\">%s</A></TD>"//[id] and [order_no] and $class
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[order_date]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>" //[store_no]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[proc_date]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[proc_time]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[merge_date] - Auth_date
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[merge_time] - auth_time
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[in_file]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[out_file]//[cases]
."<TD class=\"greyborder\"><NOBR>%s</NOBR></TD>"//[cases]
."<TD align='center' class=\"greyborder\"><NOBR>%s</NOBR><input type=\"radio\" name=\"check_" . $i . "[]\" value=\"1\">"//[auth_yes]
."<TD align='center' class=\"greyborder\"><NOBR>%s</NOBR><input type=\"radio\" name=\"check_" . $i . "[]\" value=\"2\"></TD></TR>\n",//[auth_no]
$class,
$line["id"],
$line["order_no"],
$line["order_date"],
$line["store_no"],
$line["cases"],
$line["proc_date"],
$line["proc_time"],
$line["merge_date"],
$line["merge_time"],
$line["in_file"],
$line["out_file"],
$line["auth_yes"],
$line["auth_no"]);

if ($class == "treven") { $class = "trodd"; } else { $class = "treven"; }
if ($i++ >= ($page_size-1)) break;
}
}

if ($num_rows == 0) {
echo "<TR><TD colspan=\"16\" class=\"greyborder\"><I style=\"color: #9B0000;\">No records listed</I></TD></TR>\n";
}

//show the submit button
echo "</TABLE><BR>\n";
echo "<INPUT class='greenbutton' type=\"submit\" value=\"Submit\">";
echo "</FORM>\n";
echo "</CENTER>\n";

echo "<SCRIPT>document.TheSearchForm.search.focus();</SCRIPT>";
echo "<P><BR></P>";

//show the footer info when records were last updated
$query= "SELECT * FROM `variable` WHERE name = 'last updated'";
$result = mysql_query($query) or die( "Query failed: " . $query );
if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<P>Data Last Updated On: " . $line["value"] . "<BR><BR></P>\n";
}
write_footer();

//free the results obtained
mysql_free_result($result);

//close the database link
mysql_close($link);
}
#------------------------------------------------------------------------------
function upliftdetails () {

write_header(True);

$arr_y_n = array ("<IMG src=\"images/tick_yes.gif\" alt=\"Yes\" width=\"12\" height=\"12\"> Yes",
"<IMG src=\"images/tick_no.gif\" alt=\"No\" width=\"12\" height=\"12\"> No");
$link = mysql_connect(db_host, db_user, db_pass) or die("Could not connect");
mysql_select_db(database) or die("Could not select database");

$query= "SELECT * FROM `order` WHERE (principal = " . principal . ") AND id = " . $_GET["id"];
$result = mysql_query($query) or die( "Query failed: " . $query );

if ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<BR>\n";
echo "<BR>\n";
echo "<TABLE border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"80%\">\n";
echo "<TR><TD width=\"50%\" valign=\"top\">\n";

echo "<TABLE border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
printf ("<TR align=\"right\"><TH width=\"20%%\">%s</TH><TD align=\"left\" width=\"80%%\">%s</TD></TR>\n", "Order No", $line["order_no"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Order&nbsp;Date", $line["order_date"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Store&nbsp;Name", $line["store_no"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Processed&nbsp;Date", $line["proc_date"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Processed&nbsp;Time", $line["proc_time"]);

echo "</TABLE>\n";

echo "</TD><TD width=\"30%\" valign=\"top\">\n";

echo "<TABLE border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
printf ("<TR align=\"right\"><TH width=\"20%%\">%s</TH><TD align=\"left\">%s</TD></TR>\n", "Authorised&nbsp;Date", $line["merge_date"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Authorised&nbsp;Time", $line["merge_time"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Incoming&nbsp;File", $line["in_file"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Outgoing&nbsp;File", $line["out_file"]);
printf ("<TR align=\"right\"><TH>%s</TH><TD align=\"left\">%s</TD></TR>\n", "Authorised", str_replace(array ("T", "F"), $arr_y_n, $line["auth_no"]));

echo "</TABLE>\n";

/* if ($line["pod"] == "T") {
echo "<BR>\n";
$filename = order_pic . principal . "_" . $line["order_no"] . ".tif";
$found = False;
if (file_exists($filename)) {
echo "<A href='?func=picdownload&order=" . $line["order_no"] . "'><U>Download Original Document</U></A><BR>\n";
$found = True;
}
$filename = principal . "_" . $line["order_no"] . ".gif";
if (file_exists($filename)) {
echo "<A href='?func=picdownload&order=" . $line["order_no"] . "'><U>Download Original Document</U></A><BR>\n";
$found = True;
}

// if (!$found) {
// echo "<A href='mailto:email@example.com?subject=Request Original Document #" . $line["order_no"] . "'><U>Request Original Document</U></A><BR>\n";
// }
}
*/
echo "</TD></TR>\n";
echo "</TABLE>\n";
echo "<BR>\n";
$query = "SELECT * FROM `detail`"
."LEFT JOIN `order` ON `detail`.`id` = `order`.`id`"
."WHERE `detail`.`id` = " . $_GET["id"] . " "
."ORDER BY page, line";

$result = mysql_query($query) or die( "Query failed: " . $query );

echo "<TABLE border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"80%\">\n";
echo "<TH width=\"10%\">Product&nbsp;Code</TH>"
."<TH width=\"60%\">Product&nbsp;Description</TH>"
."<TH width=\"10%\">Cases</TH><TR>\n";

while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
printf ("<TR><TD><NOBR>%s</NOBR></TD>"
."<TD><NOBR>%s</NOBR></TD>"
."<TD align=\"left\" class=\"money\">%s</TD>"
."</TR>\n",
$line["product_code"],
$line["product_description"],
$line["cases"]);
}

echo "</TABLE>\n";
echo "<p><a href=\"orders.php?func=upliftheader&id=%s\">Back</a></p>";
echo "<P><BR></P>";

$query= "SELECT * FROM `variable` WHERE name = 'last updated'";
$result = mysql_query($query) or die( "Query failed: " . $query );
if ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<P>Data Last Updated On: " . $line["value"] . "<BR><BR></P>\n";
}
}
write_footer();
mysql_free_result($result);
mysql_close($link);
}

?>

[edited by: dreamcatcher at 5:54 am (utc) on May 16, 2009]
[edit reason] Exemplified e-mail address. [/edit]

dreamcatcher

6:00 am on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mikejrtt, welcome to Webmaster World. :)

Large code dumps tend to put people off responding, so next time you may want to post only relevant code.

I think you are getting confused with radio buttons and checkboxes. The [] array operator is for checkboxes, so I think what you should be using is those. You can`t group radio buttons using the same name value. Only one can be selected in any similar naming convention.


<input type="checkbox" name="checkboxname[]" value="1" />
<input type="checkbox" name="checkboxname[]" value="2" />
<input type="checkbox" name="checkboxname[]" value="3" />

You`ll need to create a session array var and store checked boxes in this:

So, when you form is actioned:


$_SESSION['boxes'] = array();
if (!empty($_POST['checkboxname'])) {
foreach ($_POST['checkboxname'] AS $box) {
$_SESSION['boxes'][] = $box;
}
}

The session array var now holds the values of the checked boxes. When you go to a previous page, see if the array has the relevant value and check the box:


<input type="checkbox" name="checkboxname[]" value="1" <?php echo (!empty($_SESSION['boxes']) && in_array(1,$_SESSION['boxes']) ? 'checked="checked"' : ''); ?> />
<input type="checkbox" name="checkboxname[]" value="2" <?php echo (!empty($_SESSION['boxes']) && in_array(2,$_SESSION['boxes']) ? 'checked="checked"' : ''); ?> />
<input type="checkbox" name="checkboxname[]" value="3" <?php echo (!empty($_SESSION['boxes']) && in_array(3,$_SESSION['boxes']) ? 'checked="checked"' : ''); ?> />

Hope that helps and gets you started.

dc

mikejrtt

8:00 am on May 19, 2009 (gmt 0)

10+ Year Member



Thanks dreamcatcher. I really appreciate the advice and help. First time in this forum.

Cheers

dreamcatcher

11:52 am on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem, let me know how you go. Hope you find our forum useful. Don`t be afraid to post about any issues you have.

dc