Page 1 of 1

Generating "Prev 1 2 3 4 5 Next" links

Posted: Thu Jun 13, 2002 7:44 pm
by Agent
Hello. I'm trying to make a page with a whole bunch of tips for a game show Prev 1 2 3 4 5 Next type links, but I'm not having much luck. I really believe this is because I am a total newbie with PHP and MySQL. :oops:

Anyways, here's what my table looks like:

Code: Select all

CREATE TABLE tips (
  id int(4) NOT NULL auto_increment,
  author text,
  email text,
  url text,
  title text,
  text text,
  catid text,
  PRIMARY KEY  (id)
) TYPE=MyISAM;
Now, I would like to take the information from the table (the author, email, url, title, and text fields) and display it on my page, and, if there are more than... say 25 listings, display a set of links.

Can anyone help me with this? Thanks. :)

Posted: Thu Jun 13, 2002 11:00 pm
by chris12295
haha, I have answered this question before, but got no reply. The method i used is a kind of complicated, not hard to understand just long, one. If you are really serious about it, email me chris12295@aol.com and i will explain everything i did and help u set urs up.

Posted: Thu Jun 13, 2002 11:53 pm
by roninblade
here's something to get you started...

Code: Select all

<?php  
  $numrows = @mysql_result(@mysql_query("select * from $your_table), 0);
  $pagelinks = "&nbsp;";

  $rpp = 20;  // RESULTS TO BE DISPLAYED PER PAGE

  if ($page) &#123;
    $sqlstart = ($page - 1) * $rpp;
  &#125; else &#123;
    $sqlstart = 0;
    $page = 1;
  &#125;

  if ($numrows > $rpp && $numrows > 0)&#123;
    $numrows++;
    $pages = $numrows / $rpp;
    $pages = ceil($pages);
    
    if ($page == $pages) &#123;
      $to = $pages;
    &#125; elseif ($page == $pages-1) &#123;
      $to = $page+1;
    &#125; elseif ($page == $pages-2) &#123;
      $to = $page+2;
    &#125; else &#123;
      $to = $page+3;
    &#125;

    if ($page == 1 || $page == 2 || $page == 3) &#123;
      $from = 1;
    &#125; else &#123;
      $from = $page-3;
    &#125;

    $pagelinks .= 'Pages : <a href="yourpage.php?page=1"><<</a> ... ';
    for ($i = $from; $i <= $to; $i++) &#123;
      if ($i != $page) &#123;
        $pagelinks .= '<a href="yourpage.php?page='.$i.'>'.$i.'</a>';
      &#125; else &#123;
        $pagelinks .= '<b>'.$i.'</b>';
      &#125;
    &#125;

    $pagelinks .= ' ... <a href="?page='.$pages.'">>></a>';
  &#125;

  // DO THE QUERY FOR DISPLAYING THE RECORDS
  $query = "select * from $your_table limit $sqlstart, $rpp";
  $result = @mysql_query($query);
  while ($row = mysql_fetch_assoc($result)) &#123;
    // ... PRINT OUT YOUR RECORDS HERE ...
  &#125;
?>

Re: Generating "Prev 1 2 3 4 5 Next" links

Posted: Sun Jun 16, 2002 11:48 am
by Pekka
Agent wrote:Hello. I'm trying to make a page with a whole bunch of tips for a game show Prev 1 2 3 4 5 Next type links, but I'm not having much luck. I really believe this is because I am a total newbie with PHP and MySQL.
See http://marc.theaimsgroup.com/?l=php-gen ... 727432&w=2