kvnirvana Posted September 21, 2010 Share Posted September 21, 2010 The following code shows results generated from three drop down lists. When clicking search the new page shows the first 3 results as it should, but it also shows 17 page links. There are only a total of 8 results so it should only be displaying 3 page links The next 14 pages just says ‘no results’ <?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 COUNT(*) 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 Share on other sites More sharing options...
Psycho Posted September 21, 2010 Share Posted September 21, 2010 Ok, I'm not going to read through ALL of that code. It is appreciated if you post just the relevant code. But, I think the problem is that you are using a count of ALL the records to determine the number of pages to display. But, the records are filtered according to what the user has selected. You need to determine the total i]relevant[/i] records based on the filter (i.e. the same WHERE clause used on the select query to get the current page records without the LIMIT clause). So, right now you are getting page links as if you had no filtering criteria. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 21, 2010 Author Share Posted September 21, 2010 ok, I see the problem, but how should I do that I, really have no idea. This is the relevant code sorry //Pagination mysql_connect("localhost", "***", "****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); $getcount = mysql_query ("SELECT COUNT(*) 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 21, 2010 Share Posted September 21, 2010 As I said, use the same WHERE clause conditions to get your total record count. Then determine the total pages on that. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 21, 2010 Author Share Posted September 21, 2010 Ok so now it looks like this $getcount = mysql_query ("SELECT COUNT(*) FROM behan WHERE 1=1 GROUP BY na order by total_value DESC"); But now it shows 4 page links., even on pages where the search only returns 2 results. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 21, 2010 Share Posted September 21, 2010 Well, if I am understanding your code correctly, the code that selects the records has a lot more complexity in determining the where clause: $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 "; The query to get all the records will need the same logic, otherwise you are basing the total records on a different subset than the query you are using to get the page records. I don't have the time to do a complete rewrite, but just create one process to generate the where clause and then use that for both queries. Here is an example: //Parse input for where clause $whereParams = array(); //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')) { $beSearch = mysql_real_escape_string(trim($_SESSION['be'])); $whereParams[] = "`be` LIKE '{$beSearch}%'"; } if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle')) { $omraedeSearch = mysql_real_escape_string(trim($_SESSION['omraede'])); $whereParams[] = "`omraede` LIKE '{$omraedeSearch}%'"; } if ((!empty($_SESSION['pro']))&&($_SESSION['pro'] != 'alle')) { $proSearch = mysql_real_escape_string(trim($_SESSION['pro'])); $whereParams[] = "`pro` = '{proSearch}'"; } //Create where clause $WHERE_CLAUSE = (count($whereParams)>0) ? " WHERE " . implode(' AND ', $whereParams) : ''; //Run queries to get total count and records for current page $getcountQry = "SELECT COUNT(*) FROM `behan` {$WHERE_CLAUSE} GROUP BY `na`"; $recordsQry = "SELECT * FROM `behan` {$WHERE_CLAUSE} GROUP BY na ORDER BY total_value DESC {$limstring}"; Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 21, 2010 Author Share Posted September 21, 2010 Ok, thanks I really appriciate your help, unfortunately i'm a newbie so i'm not very good at this as you probably noticed :=) I tried to use your code like this //Parse input for where clause $whereParams = array(); //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')) { $beSearch = mysql_real_escape_string(trim($_SESSION['be'])); $whereParams[] = "`be` LIKE '{$beSearch}%'"; } if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle')) { $omraedeSearch = mysql_real_escape_string(trim($_SESSION['omraede'])); $whereParams[] = "`omraede` LIKE '{$omraedeSearch}%'"; } if ((!empty($_SESSION['pro']))&&($_SESSION['pro'] != 'alle')) { $proSearch = mysql_real_escape_string(trim($_SESSION['pro'])); $whereParams[] = "`pro` = '{proSearch}'"; } //Create where clause $WHERE_CLAUSE = (count($whereParams)>0) ? " WHERE " . implode(' AND ', $whereParams) : ''; //Run queries to get total count and records for current page $getcountQry = "SELECT COUNT(*) FROM `behan` {$WHERE_CLAUSE} GROUP BY `na`"; $postnum = mysql_result($getcountQry,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"; } ///////////////////////////////////// $recordsQry = "SELECT * FROM `behan` {$WHERE_CLAUSE} GROUP BY na ORDER BY total_value DESC {$limstring}"; But have no clue if it's the right way Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 23, 2010 Author Share Posted September 23, 2010 OK, so I've got this right now function search() { if (isset($_POST)) { foreach($_POST as $k=>$v) { $_SESSION[$k]=$v; }} //base sql mysql_connect("localhost", "***", "***") or die(mysql_error()); mysql_select_db("****") or die(mysql_error()); //Parse input for where clause $whereParams = array(); //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')) { $beSearch = mysql_real_escape_string(trim($_SESSION['be'])); $whereParams[] = "`be` LIKE '{$beSearch}%'"; } if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle')) { $omraedeSearch = mysql_real_escape_string(trim($_SESSION['omraede'])); $whereParams[] = "`omraede` LIKE '{$omraedeSearch}%'"; } if ((!empty($_SESSION['pro']))&&($_SESSION['pro] != 'alle')) { $proSearch = mysql_real_escape_string(trim($_SESSION['pro'])); $whereParams[] = "`pro` = '{proSearch}'"; } //Create where clause $WHERE_CLAUSE = (count($whereParams)>0) ? " WHERE " . implode(' AND ', $whereParams) : ''; //Run queries to get total count and records for current page $getcount = mysql_query ("SELECT COUNT(*) FROM `behan` {$WHERE_CLAUSE} GROUP BY `na`"); $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_CLAUSE} GROUP BY na ORDER BY total_value DESC {$limstring}"; And this is the part that creates the page links Pagination links //////////////////////// if($postnum > $limit) { echo "<strong>Sider:</strong> "; $n = $pg + 1; $p = $pg - 1; $thisroot = $_SERVER['PHP_SELF']; ; if($pg > 1) { echo "<a href=\"$thisroot?pg=$p\"><< Forrige</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\">Næste >></a>"; } echo " "; } ///////////////// }//end function The problem right now is that it doesn't display any page links Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 23, 2010 Share Posted September 23, 2010 To be honest, I have lost interest in this post. I only provided generic answers because your code is a little disorganized and would take me longer to rework than I am willing to invest. Plus, you haven't provided any information as to any debugging you have done to find the errors yourself. Not saying you haven't done any, but it would be helpful if you provided information such as I have verified that the $page_count variable is being calculated as 4 but 7 page links are being created. Also, as a side note, I would highly suggest giving your variables descriptive names pertaining to what they really are. I *think* the $splits variable has something to do with calculating the total number of pages, but I shouldn't have to read through the code to figure that out. For the first block of code, I would use the following (assuimg the $WHERE_CLAUSE has already been defined): //Define the max records on a page $records_per_page = 3; //Run query to get total record count (for current filter) $query = "SELECT COUNT(*) FROM `behan` {$WHERE_CLAUSE} GROUP BY `na`"; $getcount = mysql_query($query) or die(mysql_error()); $total_records = mysql_result($getcount,0); //Calculate number of pages (for current filter) $num_pages = ceil($total_records/$records_per_page); //Determine the page to display $current_page = (isset($_GET['pg'])) ? (int) $_GET['pg'] : 1; if($current_page<1 || $current_page>$num_pages) { $current_page = 1; } //Define the limit start position for the current page of records (for current filter) $limitstart = (($current_page-1)*$records_per_page); //Create query for the page of records to be displayed $query = "SELECT * FROM `behan` {$WHERE_CLAUSE} GROUP BY na ORDER BY total_value DESC LIMIT $limitstart, $records_per_page"; Then in the code to disply the page links: if($num_pages>1) { $thisroot = $_SERVER['PHP_SELF']; echo "<strong>Sider:</strong> "; //Create link to navigate to last page if($current_page > 1) { echo "<a href=\"{$thisroot}?pg=" . ($current_page-1) . "\"><< Forrige</a> \n"; } //Create individual page links for($page=1; $page<=$num_pages; $page++) { echo ($page!= $current_page) ? "<a href=\"{$thisroot}?pg={$page}\">$page</a>" : $page; echo " "; } //Create link to next page if($current_page < $num_pages) { echo "<a href=\"{$thisroot}?pg=" . ($current_page+1) . "\">Næste >></a>"; } } Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 27, 2010 Author Share Posted September 27, 2010 If I echo "$total_records"; it says 2 but it shows 3 results, but it should say 8 when I echo "$total_records";, and show 8 results with 3 results per page, because I know there are a total of 8 results, shouldn’t it? I’m totally lost, can’t really see what is wrong Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 27, 2010 Share Posted September 27, 2010 I think the problem is with the usage of the group by. Can you show the data in question and the WHERE paramaters that you are using? Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 27, 2010 Author Share Posted September 27, 2010 Do you mean this? function search() { if (isset($_POST)) { foreach($_POST as $k=>$v) { $_SESSION[$k]=$v; }} //base sql mysql_connect("****", "****", "***") or die(mysql_error()); mysql_select_db("****") or die(mysql_error()); //Parse input for where clause $whereParams = array(); //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')) { $beSearch = mysql_real_escape_string(trim($_SESSION['be'])); $whereParams[] = "`be` LIKE '{$beSearch}%'"; } if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle')) { $omraedeSearch = mysql_real_escape_string(trim($_SESSION['omraede'])); $whereParams[] = "`omraede` LIKE '{$omraedeSearch}%'"; } if ((!empty($_SESSION['pro']))&&($_SESSION['pro'] != 'alle')) { $proSearch = mysql_real_escape_string(trim($_SESSION['pro'])); $whereParams[] = "`pro` LIKE '{$proSearch}%'"; } //Create where clause $WHERE_CLAUSE = (count($whereParams)>0) ? " WHERE " . implode(' AND ', $whereParams) : ''; //Define the max records on a page $records_per_page = 3; //Run query to get total record count (for current filter) $query1 = "SELECT COUNT(*) FROM `behan` {$WHERE_CLAUSE} GROUP BY `na`"; $getcount = mysql_query($query1) or die(mysql_error()); $total_records = mysql_result($getcount,0); echo "$total_records"; //Calculate number of pages (for current filter) $num_pages = ceil($total_records/$records_per_page); //Determine the page to display $current_page = (isset($_GET['pg'])) ? (int) $_GET['pg'] : 1; if($current_page<1 || $current_page>$num_pages) { $current_page = 1; } //Define the limit start position for the current page of records (for current filter) $limitstart = (($current_page-1)*$records_per_page); //Create query for the page of records to be displayed $query = "SELECT * FROM `behan` {$WHERE_CLAUSE} GROUP BY na ORDER BY total_value DESC LIMIT $limitstart, $records_per_page"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 27, 2010 Share Posted September 27, 2010 No, show the output from the query. Attach as a file if it is large. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted September 29, 2010 Author Share Posted September 29, 2010 Actually somehow I made it work, thanks for your help :=) 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.