Jump to content

Can't get pagination to work properly


kvnirvana

Recommended Posts

The pagination shows the the first 3 results, and makes link to the next 3 results, but when clicking next,  it doesn't show a link to the next page if I want to see the last results. 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. Anybody knows why?

 



<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.