kvnirvana Posted August 27, 2010 Share Posted August 27, 2010 Anyone knows why this pagination wont work? <html> <head> </head> <BODY> <?php /*------------------------------------------------------------------------ control codes ------------------------------------------------------------------------*/ if (isset($_POST['submit'])) { search(); //call the search function }else{ show_form(); //call the show form function }//end if /*------------------------------------------------------------------------ show the search form ------------------------------------------------------------------------*/ function show_form() { //call the dropdown function which creates an html string to build a select box for each element $be = dropdown('be','behan'); $omraede = dropdown('omraede','behan'); $pro = dropdown('pro','behan'); echo "<form name='search' action=".$_SERVER['PHP_SELF']." method='post'> <table width='50%' align='center' > <tr> <td colspan='2' align='center' style='font-family:verdana;font-size:130%;'><strong> </strong><br></br></td> </tr> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Behandler:</td><td><select name='be'style='font-size: 18px;'> ><option value='alle'>Alle</option> <option value='Fy'>Fy</option> <option value='Ki'>Ki</option> </td> </tr> </select> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Område:</td><td><select name='omraede' style='font-size: 18px;'><option value='alle'>Alle</option> <option value='F'>F</option> <option value='Sj'>Sj</option> <option value='jy'>Jy</option></td> </tr> </select> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Pro:</td><td><select name='pro' style='font-size: 18px;'><option value='alle'>Alle</option> <option value='yg'>yg</option> <option value='rm'>rm</option> <option value='f'>F</option> </td> </tr> </select> <td colspan='2' align='center'> </tr> <tr> <td colspan='2' align='center' ><input type='submit' name='submit' style='font-size: 15px;' value='Fin'></td> </tr> <tr> </td> <td style='position:absolute;right:18px;top:36px' 'tdimage' BACKGROUND='forside1.jpg' width='290' height='600'></td></tr> </table> </form>"; }//end function /*------------------------------------------------------------------------ run the search and show the results ------------------------------------------------------------------------*/ function search() { //Pagination mysql_connect("localhost", "***", "****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); $getcount = mysql_query ("SELECT * FROM behan"); $postnum = mysql_result($getcount,0); $limit = 3; if($postnum > $limit) { $tagend = round($postnum % $limit,0); $splits = round(($postnum - $tagend)/$limit,0); if($tagend == 0) { $num_pages = $splits; } else { $num_pages = $splits + 1; } if(isset($_GET['pg'])) { $pg = $_GET['pg']; } else { $pg = 1; } $startpos = ($pg*$limit)-$limit; $limstring = "LIMIT $startpos,$limit"; } else { $limstring = "LIMIT 0,$limit"; } ///////////////////////////////////// $sql = "SELECT * FROM behan WHERE 1=1"; //get the values from the form //NOTE: You should do way more valdation on the values before you attempt to process anything if ((!empty($_POST['be']))&&($_POST['be'] != 'alle')) { $sql .= " and be like '". mysql_real_escape_string($_POST['be'])."%' "; } if ((!empty($_POST['omraede']))&&($_POST['omraede'] != 'alle')) { $sql .= " and omraede like '". mysql_real_escape_string($_POST['omraede'])."%' "; } if ((!empty($_POST['pro']))&&($_POST['pro'] != 'alle')) { $sql .= " and pro = '". mysql_real_escape_string($_POST['pro'])."' "; } // ADD ORDER BY $sql .= " GROUP BY na order by total_value DESC $limstring "; print "<table border=1>"; //run query $result = conn($sql); if (!$result){ die("No results due to database error.<br>".mysql_error()); } if (mysql_num_rows($result)==0 && ($result)!='alle') { echo "<p>No results!</p>"; } else{ echo "<TABLE width=100% height=300 border='0' cellpadding='5' cellspacing='10'>"; $first_run = "true"; while ($rows= mysql_fetch_array($result)) { echo "<TR>"; if ($first_run == "true"){ echo "<TH colspan='5' scope='colgroup'><p style='font-family:verdana;font-size:130%;text-align:center;'>Results: <scope='row'>"; if (($_POST['be'] == 'alle')&&($_POST['omraede'] == 'alle')&&($_POST['pro'] == 'alle')) { echo "alle" ; } if ($_POST['be'] != 'alle') { echo $rows['be'] ; } echo " "; if ($_POST['omraede'] != 'alle') { echo $rows['omraede'] ;} echo " "; if ($_POST['pro'] != 'alle') { echo $rows['pro'];} echo "</P></h1></Th>"; $first_run = "false"; } if($_POST['pro'] != 'alle'){ echo "<TR>"; echo "<TH scope='col' abbr='be'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Be</p></TH>"; echo "<TH scope='col' abbr='rating'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Rating</p></TH>"; echo "<TH scope='col'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Ko</p></TH>"; echo "</TR>"; echo "<TR>"; echo "<TD ALIGN=justify width=25% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'>". $rows['be'] ."<br> ". $rows['na'] ."<br> ". $rows['ad'] ."<br> ". $rows['por'] .", ". $rows['y'] ."<br> ". $rows['tl'] ."</p></TD>"; echo "<TD ALIGN=justify width=50% height=100 scope='row'>". rating_bar($rows['id'],'6','static')."<p style='font-family:verdana;font-size:100%;'> <a href='bed.php?id={$rows['id']}&na={$rows['na']}&pro={$rows['pro']}&be={$rows['be']}&omraede={$rows['omraede']}&ad={$rows['ad']}&tl={$rows['tl']}&por={$rows['por']}'>Bed </a></p></TD>"; echo "<TD ALIGN=justify width=25% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='kom.php?na={$rows['na']}&id={$rows['id']}&be={$rows['be']}'>Larer</a></p> </TD>"; echo "<TABLE width=100% height=100 border='0' cellpadding='5' cellspacing='10'><hr>"; echo "</TR>"; } else { echo "<TR>"; echo "<TH scope='col' abbr='be'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:justify;'>Be</p></TH>"; echo "<TH scope='col' abbr='rating'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:center;'>Rating</p></TH>"; echo "<TH scope='col'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:justify;'>Ko</p></TH>"; echo "</TR>"; echo "<TR>"; echo "<TD ALIGN=justify width=20% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'>". $rows['be'] ."<br> ". $rows['na'] ."<br> ". $rows['ad'] ."<br> ". $rows['por'] .", ". $rows['y'] ."<br> ". $rows['tl'] ."</p></TD>"; echo "<TD ALIGN=center width=50% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='bed.php?id={$rows['id']}&na={$rows['na']}&pro={$rows['pro']}&be={$rows['be']}&omraede={$rows['omraede']}&ad={$rows['ad']}&tl={$rows['tl']}&por={$rows['por']}'>Bed </a></p> </TD>"; echo "<TD ALIGN=justify width=40% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='kom.php?na={$rows['na']}&id={$rows['id']}&be={$rows['be']}'>Larer</a></p> </TD>"; echo "<TABLE width=100% height=100 border='0' cellpadding='5' cellspacing='10'><hr>"; echo "</TR>"; } } } echo "</table>"; //////////////////////// Links for pagination if($postnum > $limit) { echo "<strong>Pages:</strong> "; $n = $pg + 1; $p = $pg - 1; $thisroot = $_SERVER['PHP_SELF']; if($pg > 1) { echo "<a href=\"$thisroot?pg=$p\"><< prev</a> "; } for($i=1; $i<=$num_pages; $i++) { if($i!= $pg) { echo "<a href=\"$thisroot?pg=$i\">$i</a> "; } else { echo "$i "; } } if($pg < $num_pages) { echo "<a href=\"$thisroot?pg=$n\">next >></a>"; } echo " "; } ///////////////// }//end function /*------------------------------------------------------------------------ create the drop downs ------------------------------------------------------------------------*/ function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; } elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field'>\n<option onClick='alert(\"warning\")' value='alle' disabled>Alle</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function ?> </body> It shows only 3 results like it is set to, but when I want to see the next 3 results and click next on the pagination link it goes back to the drop downs. Any help much appriciated Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/ Share on other sites More sharing options...
kvnirvana Posted August 29, 2010 Author Share Posted August 29, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1104928 Share on other sites More sharing options...
DavidAM Posted August 29, 2010 Share Posted August 29, 2010 In the beginning of your code you are checking to see if the SUBMIT button was POSTed. if (isset($_POST['submit'])) { search(); //call the search function The links for page 2 (and so forth) are NOT using POST, so this IF fails and you show the form. You can get past this by also checking to see if the page number is set in the GET array: if ( (isset($_POST['submit'])) or (isset($_GET['pg'])) ) { search(); //call the search function However Your search code is building the query from the POST array. That array will NOT exist on the second and subsequent pages, so the query will not work as intended. The solution to this problem is not so simple. You will have to store the POST values somewhere (maybe in a session) or add them the the page links so they showup in GET. Either way, you're going to have to rework most of that search() code. Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1104960 Share on other sites More sharing options...
kvnirvana Posted August 30, 2010 Author Share Posted August 30, 2010 Thanks for the answer. I'm pretty new to php coding, so if anyone could show me how this could be done it would be much appriciated. I've made a session like this if($postnum > $limit) { echo "<strong>Pages:</strong> "; $n = $pg + 1; $p = $pg - 1; $thisroot = $_SERVER['PHP_SELF']; foreach($_POST as $k=>$v) { $_SESSION[$k]=$v; } if($pg > 1) { echo "<a href=\"$thisroot?pg=$p\"><< prev</a> "; } for($i=1; $i<=$num_pages; $i++) { if($i!= $pg) { echo "<a href=\"$thisroot?pg=$i\">$i</a> "; } else { echo "$i "; } } if($pg < $num_pages) { echo "<a href=\"$thisroot?pg=$n\">next >></a>"; } echo " "; } ///////////////// But I don't know what to do next Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1105256 Share on other sites More sharing options...
DavidAM Posted August 30, 2010 Share Posted August 30, 2010 if (isset($_POST)) { foreach($_POST as $k=>$v) { $_SESSION[$k]=$v; } } If you move this loop up to the beginning of the search() function and add that if(isset()) before it; then change every reference below that from $_POST to $_SESSION; you should be OK. You will need a session_start() at the beginning of the script before you output anything <?php session_start(); ?> <html> <head> Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1105266 Share on other sites More sharing options...
kvnirvana Posted August 30, 2010 Author Share Posted August 30, 2010 Cool, thanks alot it works :0) But now I've got another problem. When I click the 'next' link it doesn't show any results. I've set the pagination to show 3 results per page. Total results are 8, so it should display the next 3. Any ideas why? Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1105275 Share on other sites More sharing options...
DavidAM Posted August 30, 2010 Share Posted August 30, 2010 I don't see anthing wrong with that part of the code except that these two lines could be causing problems in the browser: echo "<a href=\"$thisroot?pg=$p\"><< prev</a> "; echo "<a href=\"$thisroot?pg=$n\">next >></a>"; When you send "<" or ">" to a page, the browser expects to find a tag. Change those to the html entities "<" and ">": echo "<a href=\"$thisroot?pg=$p\"><< prev</a> "; echo "<a href=\"$thisroot?pg=$n\">next >></a>"; If it still does not work, post your code as it is now. Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1105287 Share on other sites More sharing options...
kvnirvana Posted August 30, 2010 Author Share Posted August 30, 2010 OK now it shows the results, I really appriciate your help, have been struggeling with this for a while. But now there is another problem. It shows the next 3 results, when clicking next, but then it doesn't show a link to the next page if I want to see the last results. As I mentioned I've got a total of 8 results. the first page shows the first 3 results, when clicking next it show the next 3 results, but then there is no next link to see the last results. This is the full code <?php session_start();?><html> <head> </head> <BODY> <?php /*------------------------------------------------------------------------ control codes ------------------------------------------------------------------------*/ if ( (isset($_POST['submit'])) or (isset($_GET['pg'])) ) { search(); //call the search function }else{ show_form(); //call the show form function }//end if /*------------------------------------------------------------------------ show the search form ------------------------------------------------------------------------*/ function show_form() { //call the dropdown function which creates an html string to build a select box for each element $be = dropdown('be','behan'); $omraede = dropdown('omraede','behan'); $pro = dropdown('pro','behan'); echo "<form name='search' action=".$_SERVER['PHP_SELF']." method='post'> <table width='50%' align='center' > <tr> <td colspan='2' align='center' style='font-family:verdana;font-size:130%;'><strong> </strong><br></br></td> </tr> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Behandler:</td><td><select name='be'style='font-size: 18px;'> ><option value='alle'>Alle</option> <option value='Fy'>Fy</option> <option value='Ki'>Ki</option> </td> </tr> </select> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Område:</td><td><select name='omraede' style='font-size: 18px;'><option value='alle'>Alle</option> <option value='F'>F</option> <option value='Sj'>Sj</option> <option value='jy'>Jy</option></td> </tr> </select> <tr> <td align='right' style='font-family:verdana;font-size:110%;'>Pro:</td><td><select name='pro' style='font-size: 18px;'><option value='alle'>Alle</option> <option value='yg'>yg</option> <option value='rm'>rm</option> <option value='f'>F</option> </td> </tr> </select> <td colspan='2' align='center'> </tr> <tr> <td colspan='2' align='center' ><input type='submit' name='submit' style='font-size: 15px;' value='Fin'></td> </tr> <tr> </td> <td style='position:absolute;right:18px;top:36px' 'tdimage' BACKGROUND='forside1.jpg' width='290' height='600'></td></tr> </table> </form>"; }//end function /*------------------------------------------------------------------------ run the search and show the results ------------------------------------------------------------------------*/ function search() { if (isset($_POST)) { foreach($_POST as $k=>$v) { $_SESSION[$k]=$v; }} //Pagination mysql_connect("localhost", "***", "****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); $getcount = mysql_query ("SELECT * FROM behan"); $postnum = mysql_result($getcount,0); $limit = 3; if($postnum > $limit) { $tagend = round($postnum % $limit,0); $splits = round(($postnum - $tagend)/$limit,0); if($tagend == 0) { $num_pages = $splits; } else { $num_pages = $splits + 1; } if(isset($_GET['pg'])) { $pg = $_GET['pg']; } else { $pg = 1; } $startpos = ($pg*$limit)-$limit; $limstring = "LIMIT $startpos,$limit"; } else { $limstring = "LIMIT 0,$limit"; } ///////////////////////////////////// $sql = "SELECT * FROM behan WHERE 1=1"; //get the values from the form //NOTE: You should do way more valdation on the values before you attempt to process anything if ((!empty($_SESSION['be']))&&($_SESSION['be'] != 'alle')) { $sql .= " and be like '". mysql_real_escape_string($_SESSION['be'])."%' "; } if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle')) { $sql .= " and omraede like '". mysql_real_escape_string($_SESSION['omraede'])."%' "; } if ((!empty($_SESSION['pro']))&&($_SESSION['pro'] != 'alle')) { $sql .= " and pro = '". mysql_real_escape_string($_SESSION['pro'])."' "; } // ADD ORDER BY $sql .= " GROUP BY na order by total_value DESC $limstring "; print "<table border=1>"; //run query $result = conn($sql); if (!$result){ die("No results due to database error.<br>".mysql_error()); } if (mysql_num_rows($result)==0 && ($result)!='alle') { echo "<p>No results!</p>"; } else{ echo "<TABLE width=100% height=300 border='0' cellpadding='5' cellspacing='10'>"; $first_run = "true"; while ($rows= mysql_fetch_array($result)) { echo "<TR>"; if ($first_run == "true"){ echo "<TH colspan='5' scope='colgroup'><p style='font-family:verdana;font-size:130%;text-align:center;'>Results: <scope='row'>"; if (($_SESSION['be'] == 'alle')&&($_SESSION['omraede'] == 'alle')&&($_SESSION['pro'] == 'alle')) { echo "alle" ; } if ($_SESSION['be'] != 'alle') { echo $rows['be'] ; } echo " "; if ($_SESSION['omraede'] != 'alle') { echo $rows['omraede'] ;} echo " "; if ($_SESSION['pro'] != 'alle') { echo $rows['pro'];} echo "</P></h1></Th>"; $first_run = "false"; } if($_SESSION['pro'] != 'alle'){ echo "<TR>"; echo "<TH scope='col' abbr='be'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Be</p></TH>"; echo "<TH scope='col' abbr='rating'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Rating</p></TH>"; echo "<TH scope='col'><p style='font-family:verdana;font-size:100%;text-align:justify;text-decoration:underline;'>Ko</p></TH>"; echo "</TR>"; echo "<TR>"; echo "<TD ALIGN=justify width=25% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'>". $rows['be'] ."<br> ". $rows['na'] ."<br> ". $rows['ad'] ."<br> ". $rows['por'] .", ". $rows['y'] ."<br> ". $rows['tl'] ."</p></TD>"; echo "<TD ALIGN=justify width=50% height=100 scope='row'>". rating_bar($rows['id'],'6','static')."<p style='font-family:verdana;font-size:100%;'> <a href='bed.php?id={$rows['id']}&na={$rows['na']}&pro={$rows['pro']}&be={$rows['be']}&omraede={$rows['omraede']}&ad={$rows['ad']}&tl={$rows['tl']}&por={$rows['por']}'>Bed </a></p></TD>"; echo "<TD ALIGN=justify width=25% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='kom.php?na={$rows['na']}&id={$rows['id']}&be={$rows['be']}'>Larer</a></p> </TD>"; echo "<TABLE width=100% height=100 border='0' cellpadding='5' cellspacing='10'><hr>"; echo "</TR>"; } else { echo "<TR>"; echo "<TH scope='col' abbr='be'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:justify;'>Be</p></TH>"; echo "<TH scope='col' abbr='rating'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:center;'>Rating</p></TH>"; echo "<TH scope='col'><p style='font-family:verdana;font-size:100%;text-decoration:underline;text-align:justify;'>Ko</p></TH>"; echo "</TR>"; echo "<TR>"; echo "<TD ALIGN=justify width=20% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'>". $rows['be'] ."<br> ". $rows['na'] ."<br> ". $rows['ad'] ."<br> ". $rows['por'] .", ". $rows['y'] ."<br> ". $rows['tl'] ."</p></TD>"; echo "<TD ALIGN=center width=50% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='bed.php?id={$rows['id']}&na={$rows['na']}&pro={$rows['pro']}&be={$rows['be']}&omraede={$rows['omraede']}&ad={$rows['ad']}&tl={$rows['tl']}&por={$rows['por']}'>Bed </a></p> </TD>"; echo "<TD ALIGN=justify width=40% height=100 scope='row'><p style='font-family:verdana;font-size:100%;'><a href='kom.php?na={$rows['na']}&id={$rows['id']}&be={$rows['be']}'>Larer</a></p> </TD>"; echo "<TABLE width=100% height=100 border='0' cellpadding='5' cellspacing='10'><hr>"; echo "</TR>"; } } } echo "</table>"; //////////////////////// Links for pagination if($postnum > $limit) { echo "<strong>Pages:</strong> "; $n = $pg + 1; $p = $pg - 1; $thisroot = $_SERVER['PHP_SELF']; ; if($pg > 1) { echo "<a href=\"$thisroot?pg=$p\"><< prev</a> "; } for($i=1; $i<=$num_pages; $i++) { if($i!= $pg) { echo "<a href=\"$thisroot?pg=$i\">$i</a> "; } else { echo "$i "; } } if($pg < $num_pages) { echo "<a href=\"$thisroot?pg=$n\">next >></a>"; } echo " "; } ///////////////// }//end function /*------------------------------------------------------------------------ create the drop downs ------------------------------------------------------------------------*/ function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; } elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field'>\n<option onClick='alert(\"warning\")' value='alle' disabled>Alle</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1105307 Share on other sites More sharing options...
kvnirvana Posted September 1, 2010 Author Share Posted September 1, 2010 Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1106072 Share on other sites More sharing options...
kvnirvana Posted September 2, 2010 Author Share Posted September 2, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/211903-pagination-doesnt-work/#findComment-1106576 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.