Jump to content

database Queries with PHP and paging


dannybrazil

Recommended Posts

Hello

i have this php code and after it passes 3 posts it suppose to open a new page(paging)

its all good but after the first page it doesnt continue to pass the variable of the city and

category and it doesnt show anything any more

<script type="text/javascript">
  function doToggle ( ele_id ) {
    var ele = document.getElementById(ele_id);
    if(ele)
      ele.style.display = (ele.style.display == 'block') ? 'none' : 'block';
    return false;
  }
</script>

<?php 
$category=$_GET['category'];
$city=$_GET['city'];
$city_lower = strtolower($city);

//title of page
$home_link="http://www.classifieds-4free.net";
$city_link="http://www.classifieds-4free.net/post-categories.php?city=$city";

echo '<a href="'.$home_link.'".>'.'Home'.'</a>';
echo ">";
echo '<a href="'.$city_link.'".>'.$city.'</a>';
echo ">";
echo $category;

echo "<p>";
echo "<p>";
echo "<p>";
echo "<p>";
echo "<p>";
echo "<p>";
echo "<p>";
echo "<p>";

echo '<h1 align="center">'.'<u>'.$city."-".$category.'</u>'.'</h1>'; 
echo '&nbsp'.'&nbsp'.'['.'<a href="http://www.classifieds-4free.net/addform.html">'.'Post'.'</a>'.']';
  



// Connects to your Database 
mysql_connect("localhost", "*******", "********") or die(mysql_error()); 
mysql_select_db("********_*******") or die(mysql_error()); 

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT  * FROM _Form_Nr_4 WHERE category='$category' AND city='$city' ORDER BY post_time DESC ") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 3; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT  * FROM _Form_Nr_4 WHERE category='$category' AND city='$city' ORDER BY post_time DESC $max") or die(mysql_error()); 

//displaying the results

// keeps getting the next row until there are no more to get
echo "<table border='1'>";
echo '<tr style="background-color:#FFE4B5;">'.' <th style="width:110px;">'.'Date'.'</th>'.' <th style="width:250px;">'.'Title'.'</th>'.'<th style="width:80px;">'.'Price'.'</th>'.'<th style="width:120px;">'.'Area'.'</th>'.' </tr>';

$num = 0;

while($info = mysql_fetch_array( $data_p)) {
// Print out the contents of each row into a table
$key = 'unique_key_'.$num;

$id=$info['Record_Nr'];

$link = "http://www.classifieds-4free.net/view_posting.php?id=$id";
echo '<tr style="background-color:#FFFFE0;">'.'<td align="center">'; 
echo $info['Submission_Date'];
echo "</td><td>";
$title=$info['posting_title'];
echo '<a href="'.$link.'".>'.substr($title, 0, 30).'...</a>';
//check if pic was uploaded
  if($info['upload']!==NULL)
   {
    echo '&nbsp'.'&nbsp'.'(pic)';
   }

echo '</td>'.'<td align="center">';

$price=$info['price'];
if($price==NULL)
   {
    echo "N/A";
   }
elseif(!$price==NULL)
  {
     echo $info['price']; 
   }
echo '</td>'.'<td align="center">';
echo '&nbsp'.$info['living_area'];
echo "</td></tr>"; 
$num++;
} 

echo "</table>";


echo "<p>";


// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer

// This shows the user what page they are on, and the total number of pages


echo "[Page $pagenum of $last]";


//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?> 

 

thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/100742-database-queries-with-php-and-paging/
Share on other sites

Hi,

 

I believe the problem is that you aren't setting an offset in the sql query. For example, if you're showing 10 items on the first page, to show the second page you would add "LIMIT 10 OFFSET 10" to the query string. I didn't look at it too thoroughly, but I think that's the problem.

 

Hope this helps!

you would have to pass the page number with a link.

mypage.php?page=2

 

Then you would change this

if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

 

To something like this

if(isset($_GET['page'])){
$pagenum = $_GET['page'];
} else {
$pagenum = 1;
}

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.