Next page, Previous page....PHP script

Next page, Previous page....PHP script

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

I m making site for let's say computer news.Every news are saved in the database.Now what i need.
In the first page i need to give me the last five news that i wroted and at the bottom of the page i need Links for other posts.Here is example:
<< |1-5| >>
if i click on >> to give me next 5 news ( from 5 to 10 ) and so on...

Any suggestion for this, link for tutorial....



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Blood  Male
  • Ugledni građanin
  • Pridružio: 26 Jul 2003
  • Poruke: 384
  • Gde živiš: Beograd

I have wroted this earlier, but since you dont understand serbian, here it go again:

1. First you must calculate number of all records
$query = mysql_query("SELECT COUNT(*) AS `all` FROM `table`;"); $row = mysql_fetch_array($query); $all = $row['all'];
2. Then you need to set how much records you want to be displayed by one page
$no_per_page = 30;
3. Then we calculate how much pages will be.
$no_pages = ceil($all / $no_per_page);

4. Printing records
if($_GET['page']) {  $page = $_GET['page'] - 1; } if(!$page) {   $page = 0; } $first_record = $page * $no_per_page; $query = mysql_query("SELECT * FROM `table` LIMIT ".$no_per_page.",".$first_record.";"); while...
now you have printed records, and now you need to print links to pages
$i = 1; while($i <= $no_pages) {   if($i == $page) {     print ' $i';   }   else {     print '<a href="somepage.php?page='.$i.'"> $i</a>';   }   $i++; }
thats it..



offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

i dont understand the code, i saw the same code on another page(i dont know the web site).
This thing is not working.
What should $page variable do?
Another thing, "ceil($all / $no_per_page)" should be function or something else?

here is my code
<?php         $connection=mysql_connect('localhost', 'root', '***') or die(mysql_error());        mysql_select_db("csp");        $queryALL = "SELECT COUNT(*) AS 'all' FROM posts";        $resultALL = mysql_query($queryALL);        $row = mysql_fetch_array($resultALL);        $all = $row['all'];         $no_per_page = 5;         $no_pages = ceil($all / $no_per_page);                 if($_POST['page'])         {         $page = $_POST['page'] - 1;         }         if(!$page) {         $page = 0;                 }         $first_record = $page * $no_per_page;         $query = "select * from posts limit ".$no_per_page.",".$first_record." ";         $result = mysql_query($query); ?> .......html code..... <?php while($row = mysql_fetch_array($result)) { echo $row['Topic']; echo $row['Description']; echo $row['ID']; echo $row['date_auto']; } ?> </body> </html>

this gaves me nothing, i m confused

offline
  • Blood  Male
  • Ugledni građanin
  • Pridružio: 26 Jul 2003
  • Poruke: 384
  • Gde živiš: Beograd

The page variable sets the first record who will be displayed, and the ceil() is a php function

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

OK i found example something like this and i solve the problem

for everyone who have problem like mine here is the code

this is the code for setting the number of the rows per page
<?php        $posts_per_page = 5;             (!$_GET['start']) ? $start = 0 : $start = $_GET['start'];             $connection = mysql_connect('localhost', 'root', '***') or die (mysql_error());       mysql_select_db('name_of_database');       $query = "SELECT COUNT(*) FROM 'table' ";       $result = mysql_query($query) or die (mysql_error());       $row = mysql_fetch_row($result);       $total_records = $row[0];                          if (($total_records > 0) && ($start < $total_records))                      {                      $query = "SELECT * FROM 'table' ORDER BY 'ID' desc LIMIT $start, $posts_per_page";                      $result = mysql_query($query) or die (mysql_error()); ?>

this code is extracting every row from the table
<?php while($row = mysql_fetch_array($result)) { echo $row['first_column']; echo $row['second_column']; echo $row['third_column']; echo $row['fourth_colum']; } } ?>

This code is printing the links something like this 0 - 5 >> then << 5 - 10 >> and so on.
<?php echo '<div id="NextPrevious">'; if ($start >= $posts_per_page) { echo "<a href=".$_SERVER['PHP_SELF']."?start=".($start-$posts_per_page)."> << $start </a>"; } if ($start+$posts_per_page < $total_records && $start >= 0) {   $sum;   $sum = $start + 5;   if ($start == 0){   echo "$start - <a href=".$_SERVER['PHP_SELF']."?start=".($start + $posts_per_page)."> $sum >> </a>";   }   else   {    echo " -<a href=".$_SERVER['PHP_SELF']."?start=".($start + $posts_per_page)."> $sum >> </a>";   } } echo '</div>'; ?>

offline
  • igor86  Male
  • Stručni saradnik
    Web programiranje
  • Pridružio: 24 Maj 2006
  • Poruke: 1633

Mislim da bi bilo pozeljno da se postovi sa RutiX-om pisu na Srpskom, posto je covjek rekao da razumije isti, iz razloga da drugi mogu sadrzaje koristiti, koji ne znaju engleski, naravno.

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

Yes that's right

Ko je trenutno na forumu
 

Ukupno su 660 korisnika na forumu :: 43 registrovanih, 11 sakrivenih i 606 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: A.R.Chafee.Jr., amaterSRB, babaroga, Cufo, Darko8, Davoli, FOX, Hipnotizer, Hoegaarden, ikan, Insan, ivan979, ivica976, kovinacc, Kristian_KG, Mar_ko, MB120mm, meelosh64, menges, Mercury, micoboj, milimoj, mushroom, Nekicoveculjak, nemkea71, neutralal.com, perun32, raketaš, RecA, Recce, rodoljub, rovac, royst33, sakota79, samsung, sevenino, Simon simonović, sombrero, ssekir75, Toni, vathra, yrraf, Zi0mek