Jump to content

Recommended Posts

I was told that this code works but I have no idea how to use it

 

SQL code

DROP TABLE IF EXISTS `news`;

CREATE TABLE `news` (

`id` int(255) NOT NULL auto_increment,

`title` text NULL default '',

`story` text NULL,

`author` varchar(255) NOT NULL default '',

PRIMARY KEY  (`id`),

) ENGINE=MyISAM; 

 

PHP code

 

 

      <?php
   2.
   3.
      $host = "your_host"; //your sql host, usually 'localhost'
   4.
      $user = "username"; //username to connect to database
   5.
      $pass = "password"; //password to connect to database
   6.
      $db = "your_database"; //the name of the database
   7.
   8.
      mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
   9.
      mysql_select_db($db) or die("ERROR DB:".mysql_error());
  10.
  11.
  12.
      $max = 25; //amount of articles per page. change to what to want
  13.
      $p = $_GET['p'];
  14.
      if(empty($p))
  15.
      {
  16.
      $p = 1;
  17.
      }
  18.
      $limits = ($p - 1) * $max;
  19.
      //view the news article!
  20.
      if(isset($_GET['act']) && $_GET['act'] == "view")
  21.
      {
  22.
      $id = $_GET['id'];
  23.
      $sql = mysql_query("SELECT * FROM news WHERE id = '$id'");
  24.
      while($r = mysql_fetch_array($sql))
  25.
      {
  26.
      $title = $r['title'];
  27.
      $story = $r['story'];
  28.
      $author = $r['author'];
  29.
      echo "<div><p>$title</p><p>$author</p><p>$story</p></div>";
  30.
      }
  31.
  32.
      }else{
  33.
  34.
      //view all the news articles in rows
  35.
      $sql = mysql_query("SELECT * FROM news LIMIT ".$limits.",$max") or die(mysql_error());
  36.
      //the total rows in the table
  37.
      $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM news"),0); 
  38.
      //the total number of pages (calculated result), math stuff...
  39.
      $totalpages = ceil($totalres / $max);
  40.
      //the table
  41.
      echo "<table><tr><td>Title</td><td>Author</td></tr><tr>";
  42.
      while($r = mysql_fetch_array($sql))
  43.
      {
  44.
      $id = $r['id'];
  45.
      $title = $r['title'];
  46.
      $author = $r['author'];
  47.
      echo "<td><a href='news.php?act=view&id=$id'>$title</a></td><td>$author</td>";
  48.
      }
  49.
      //close up the table
  50.
      echo "</tr></table>";
  51.
      for($i = 1; $i <= $totalpages; $i++){
  52.
      //this is the pagination link
  53.
      echo "<a href='news.php?p=$i'>$i</a>|";
  54.
      }
  55.
      }
  56.
  57.
      ?>

Link to comment
https://forums.phpfreaks.com/topic/51079-php-pagination/#findComment-251708
Share on other sites

Yes it does work.... I made a couple changes though. I spaced out the pagination links, and I moved a <tr> & </tr> tag so each article appears in a seperate row.

 

Set the host, user & password variables, run the sql code. I attached a txt file with the sql dump. There are 4 rows as filler data. I have set the max per page to 1 so  I could see if the pagination shows properly. It does, set the 1 to however many you want to show per page.

 

Beyond that, you will have to do some research and learn how to make the code for adding news items, modifying news items and displaying the full news item page. Those are not short 2 or 3 line answers.

 

 

<?php
   
      $host = "localhost"; //your sql host, usually 'localhost'
      $user = "username"; //username to connect to database
      $pass = "password"; //password to connect to database
      $db = "news"; //the name of the database
   
      mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
   
      mysql_select_db($db) or die("ERROR DB:".mysql_error());
  
  

      $max = 1; //amount of articles per page. change to what to want
  
      $p = $_GET['p'];
  
      if(empty($p))
      {
      $p = 1;
      }
      $limits = ($p - 1) * $max;
      //view the news article!
      if(isset($_GET['act']) && $_GET['act'] == "view")
      {
      $id = $_GET['id'];
      $sql = mysql_query("SELECT * FROM news WHERE id = '$id'");
      while($r = mysql_fetch_array($sql))
      {
      $title = $r['title'];
      $story = $r['story'];
      $author = $r['author'];
      echo "<div><p>$title</p><p>$author</p><p>$story</p></div>";
      }
      }else{
      //view all the news articles in rows
      $sql = mysql_query("SELECT * FROM news LIMIT ".$limits.",$max") or die(mysql_error());
      //the total rows in the table
      $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM news"),0); 
      //the total number of pages (calculated result), math stuff...
      $totalpages = ceil($totalres / $max);
      //the table
      echo "<table><tr><td>Title</td><td>Author</td></tr>";
      while($r = mysql_fetch_array($sql))
      {
      $id = $r['id'];
      $title = $r['title'];
      $author = $r['author'];
      echo "<tr><td><a href='news.php?act=view&id=$id'>$title</a></td><td>$author</td></tr>";
      }
      //close up the table
      echo "</table>";
      for($i = 1; $i <= $totalpages; $i++){
      //this is the pagination link
      echo "<a href='news.php?p=$i'>$i</a> | ";
      }
      }
      ?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/51079-php-pagination/#findComment-251789
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.