We help IT Professionals succeed at work.

Paging with PHP

narmi2
narmi2 asked
on
338 Views
Last Modified: 2008-01-09
Hi

I have the following php code which simply displays links on the page:

<?php
      if($_GET['id'] == "")
      {
            echo getdata("");
      }
      else
      {
            echo getdata("and link_group like '%".$_GET['id'].",%'");
      }

      function getdata($querystring)
      {
            $connection = mysql_connect('servername', 'password', '') or die (mysql_error());
            $db = mysql_select_db('database', $connection) or die (mysql_error());
            $query = "select * from link_info where archive = 0 and inactive = 0 and type = 'news' ".$querystring." order by display_date desc";
            $result = mysql_query($query) or die (mysql_error());
                  
            global $the_data;

            $the_data = '';
            while ($row = mysql_fetch_array($result))
            {
                  extract($row);
                  $new_date = date('F Y', strtotime($display_date));
                  
                  $the_data = $the_data."<div class='newsdate'>$new_date</div>";
                  $the_data = $the_data."<div class='newssubject'><a href='links/news/$file_name' title='Download $description link'><img src='images/linkicon_small.gif' alt='link' width='17' height='17' /> $description</a></div>";
                  $the_data = $the_data."<div class='clear'></div>";
            }            
            mysql_close($connection);
            
            return $the_data;
      }
?>

How do I create paging with this?  So it only displays 15 results at a time.  If they want to see the next 15, they have to click the page number at the bottom, i.e. Page 1,2,3,4.  The page number depends on how many links are returned,  so if <= 15 links are returned, it only displays page 1, if more > 15 and <= 30 it should display Page 1,2.  and so on.

Can this be done?
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm new to php, can you help integrate that code with my existing code?
Enterprise Web Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
AlexanderREnterprise Web Developer
CERTIFIED EXPERT

Commented:
Oh, that thing is a part of the function.  That means that
echo '<a href="'.$_SERVER['PHP_SELF'].'?recordposition='.$i+1 .'">'.$page."</a>,";

needs to be
$the_data = $the_data. '<a href="'.$_SERVER['PHP_SELF'].'?recordposition='.$i+1 .'">'.$page."</a>,";
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.