cyprus Posted September 26, 2006 Share Posted September 26, 2006 I have a series of checkboxes on a form/page. I use the code below which maintains there settings on page re submission. [code]<? $products = array ( 1 => 'Digital Betacam', 2 => 'Betacam SP', 3 => 'DVCPro', 4 => 'HDCAM', 5 => 'Mini DV' ); foreach ($products as $id =>$prod) { if ($_POST['product']) { // was value of id in those posted? $chk = in_array($id, $_POST['product']) ? 'checked' : ''; } else $chk = '';?> [/code]However, now I am using page pagination, the checkbox's lose there checked/unchecked status when paging. Any idea how I prevent this happening. Many thanks Quote Link to comment Share on other sites More sharing options...
mewhocorrupts Posted September 26, 2006 Share Posted September 26, 2006 [quote author=cyprus link=topic=109587.msg441907#msg441907 date=1159295970]I have a series of checkboxes on a form/page. I use the code below which maintains there settings on page re submission. [code]<? $products = array ( 1 => 'Digital Betacam', 2 => 'Betacam SP', 3 => 'DVCPro', 4 => 'HDCAM', 5 => 'Mini DV' ); foreach ($products as $id =>$prod) { if ($_POST['product']) { // was value of id in those posted? $chk = in_array($id, $_POST['product']) ? 'checked' : ''; } else $chk = '';?> [/code]However, now I am using page pagination, the checkbox's lose there checked/unchecked status when paging. Any idea how I prevent this happening. Many thanks[/quote]I'm fairly certain that your issue might lie in how your referencing your $_POST items. $_POST['product'] should be $_POST[$prod], shouldn't it? Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 Thanks. However I tried it out and I lose my checkbox settings if I submit the page back to itself. Its all been working fine until I introduced page pagination, and its when I change the pages I lose my checkbox settings.Don't know if something in here would be the reason?[code]if ($pagenumber == 1) { echo " First Previous ";} else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>First</a> "; $prevpage = $pagenumber-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>Previous</a> ";} echo " ( Page $pagenumber of $lastpage ) ";if ($pagenumber == $lastpage) { echo " Next Last ";} else { $nextpage = $pagenumber+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>Last</a> ";}[/code]Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 The code you have in your first post is for processing a form, which is normally involving a single record.You say you are paging which is for process selections involving many records, screenful by screenful.I'm having difficulty reconciling what's going on that requires both techniques. Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 Thanks, I will try and explain. The page/form has six checkboxes on representing 6 product types contained within a table containing archived orders. An HTML table displays the contents of the order table. However a user may wish to show only one or selected product types in the table, so they leave checkboxes checked or unchecked, and press submit which resubmits the same form back, the contents of the checkboxes perform a creation of the WHERE statement used in the database query. The status of checkboxes did remain in the conditions they were set, however selecting further pages in pagnitation cause the checkboxes to drop out and the query reverts to all records when page 2 of the table is shown.Hope you are with me so far. So when it was just the same page returned there were no problems, but in adding extension pages for long HTML tables I have this problem. Regards Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 Thought I would post the complete code in case it helps. Regards[code]<?session_start();$_SESSION['Dayte']=date("F j, Y, g:i a");?><html><head><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Date</title></head><form action="" method="POST"><BR> <input type="submit" value="Submit Data" name="X1" ><input type="submit" value="Clear Data" name="X2" ><input type="submit" value="Exit" name="X3" ><BR> // Offer product selection checkboxes. If page returned from submission then hold values<div align="left"><table border="1" width="90%" id="table1" bgcolor="#FFFFCC" bordercolor="#000000" cellspacing="0"><? $products = array ( 1 => 'Digital Betacam', 2 => 'Betacam SP', 3 => 'DVCPro', 4 => 'HDCAM', 5 => 'Mini DV' ); foreach ($products as $id =>$prod) { if ($_POST['product']) { // was value of id in those posted? $chk = in_array($id, $_POST['product']) ? 'checked' : ''; } else $chk = '';?> <TD> <? echo "<input type='checkbox' name='product[]' value='$id' $chk>$prod<br>"; }?></TD></TABLE> <BR> // If page being resubmitted to itself, use posted values to create select WHERE statement<? if (isset($_POST['X1'])) { if (count($_POST['product'])) { $prodlist = join("','", $_POST['product']); $where = "WHERE GGroup IN ('$prodlist')"; } }if (isset($_GET['pagenumber'])) { $pagenumber = $_GET['pagenumber'];} else { $pagenumber = 1;} // Connect to database, run query, setup multiple pages for viewing HTML table$username="root";// $password="password";$database ="xxx";//connect to MySQLmysql_connect(localhost,$username);mysql_select_db($database) or die( "Unable to select database"); //$query = "SELECT count(*) FROM Orders";$query = "select Orderdate as Ordered,product as Description,item as Code,Duration,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal from orders $where order by Orderdate,Pindex"; $result = mysql_query($query) or die('Problem with query: ' . $query . ' '. mysql_error());$query_data = mysql_fetch_row($result);$numrows = $query_data[0];$rows_per_page = 10;$lastpage = ceil($numrows/$rows_per_page);$pagenumber = (int)$pagenumber;if ($pagenumber < 1) { $pagenumber = 1;} elseif ($pagenumber > $lastpage) { $pagenumber = $lastpage;} $limit = 'LIMIT ' .($pagenumber - 1) * $rows_per_page .',' .$rows_per_page;//$query = "SELECT * FROM Orders $where $limit";$query = "select Orderdate as Ordered,product as Description,item as Code,Duration,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal from orders $where order by Orderdate, PIndex $limit"; $result = mysql_query($query) or die('Problem with query: ' . $query . ' '. mysql_error());if (($result)||(mysql_errno == 0)) { echo "<table border='1' bordercolor='#000000' bgcolor='#FFFFCC' cellspacing='0' width='90%'><tr>"; if (mysql_num_rows($result)>0) { //loop thru the field names to print the correct headers $i = 0; while ($i < mysql_num_fields($result)) { echo "<td align='center'><b><font face='Arial' size='1'>". mysql_field_name($result, $i) . "</font></b></td>"; $i++; } echo "</tr>"; //display the data while ($rows = mysql_fetch_assoc($result)) { echo "<tr>"; foreach ($rows as $k => $data) { switch ($k) { case 'Unit': case 'SubTotal': $data = '£ '.$data; break; case 'Quantity': $data = $data. 'off '; break; case 'OrderDate': $data = date("j-M-Y", strtotime($data)); } echo "<td align='center'><b><font face='Arial' size='1'>". $data . "</font></b></td>"; } } }else{ echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; } echo "</table>"; }else{ echo "Error in running query :". mysql_error(); } // PAGINATION BITSif ($pagenumber == 1) { echo " First Previous ";} else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>First</a> "; $prevpage = $pagenumber-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>Previous</a> ";} echo " ( Page $pagenumber of $lastpage ) ";if ($pagenumber == $lastpage) { echo " Next Last ";} else { $nextpage = $pagenumber+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>Last</a> ";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 When you go to the next page you are using the query strings to pass the data to the page so you need to include the groups to be selected in the link HREF data Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 Many thanks, can you enlighten me a bit more on that as I haven't a clue from here on. Do you mean the select query needs to be passed on, or product groups. !! Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 When you first process the check boxes you end with something like "1,3,5" as the selected GGroups.So where you currently have echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>Next</a> ";Add the ggroup data to thatecho " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage&where=$where'>Next</a> "; Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 I tried the following, but with same results, could very well have misinterpreted suggestion:[code]if ($pagenumber == 1) { echo " First Previous ";} else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1&where=$where'>First</a> "; $prevpage = $pagenumber-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage&where=$where'>Previous</a> ";} echo " ( Page $pagenumber of $lastpage ) ";if ($pagenumber == $lastpage) { echo " Next Last ";} else { $nextpage = $pagenumber+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage&where=$where'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage&where=$where'>Last</a> ";}[/code]Modified all href lines. Thanks again Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 Are you picking up the "where" value from the querystring and using it in the query? Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 26, 2006 Author Share Posted September 26, 2006 Thanks. Yes the WHERE string is being passed to the SELECT query, This part works okay after first pass after checkboxes set, and on repetitive submissions.if (isset($_POST['X1'])) { if (count($_POST['product'])) { $prodlist = join("','", $_POST['product']); $where = "WHERE GGroup IN ('$prodlist')"; } }The WHERE statement gets added in as:.....from orders [b]$where [/b] order by Orderdate, PIndex $limit"; If I didn't have to extend my page to accomodate longer HTML tables being displayed, then all the code worked fine. I was about to add in To/From order dates to complete the page before I then realised my HTML tables could end up quite long. Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 27, 2006 Author Share Posted September 27, 2006 Sitting here, tearing the last strands of hair from my head, I just wonder why I am going down the page pagination route to show a long broken up HTML table when I am submitting the same page back to me. Is there a way to just refil the table each time with ascending/descending query limts, values held in session variables, which simply get counted up/down? Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2006 Share Posted September 27, 2006 I can't see where you pick up the value fron the [b]querystring[/b]Change[code]<?phpif (isset($_POST['X1'])) { if (count($_POST['product'])) { $prodlist = join("','", $_POST['product']); $where = "WHERE GGroup IN ('$prodlist')"; }}?>[/code]to[code]<?phpif (isset($_GET['where'])) { $where = $_GET['where'];} elseif (isset($_POST['X1'])) { if (count($_POST['product'])) { $prodlist = join("','", $_POST['product']); $where = "WHERE GGroup IN ('$prodlist')"; }}?> [/code] Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 27, 2006 Author Share Posted September 27, 2006 Many thanks for still being in there. I tried the code but it rejected the SQL select statement when trying to goto the next page, it was rejecting the where statement. I know the statement is being built as I have echoed it. Why is life so easy? Thanks again Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2006 Share Posted September 27, 2006 Can you echo the rejected sql statement so we can see the code Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 27, 2006 Author Share Posted September 27, 2006 I can paste the where statement for the first submission:WHERE GGroup IN ('1','2')however on selecting the next page, I cannot get anything other than:WHERE GGroup IN (Problem with query: select Orderdate as Ordered,product as Description,item as Code,Duration,Qty as Quantity,unitprice as Unit,runningtotal as SubTotal from orders WHERE GGroup IN ( You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1Also in moving to page 2 the checkbox drops out of being checked, so maybe that causes the error. If I just keep hitting submit, in other words just keep returning the form/page while on page 1, there are no errors. Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2006 Share Posted September 27, 2006 Do you want to attach a dump of your orders table and I'll load it up at my end so I can test the script? Quote Link to comment Share on other sites More sharing options...
cyprus Posted September 27, 2006 Author Share Posted September 27, 2006 If its of help then yes that could be done. I exported it to a file called orders.sql (I dont know if that contains the information you want or do you also want the structure export. How do I get it over to you. Many thanks Quote Link to comment 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.