Jump to content

Indexing Pages


ChompGator

Recommended Posts

Hello,

 

I have a real simple question, I have a php script that displays news articles on a page...Im curious as to what would I add to this script to tell it after every 4 articles, add a new page - and Id just want it to start creating page numbers like:

 

Page: [1], [2], [3]

 

<?php
$con = mysql_connect("***","***","****") or die('Could not connect: ' . mysql_error());
mysql_select_db("***", $con);
$result = mysql_query("SELECT * FROM elections");

while($row = mysql_fetch_assoc($result)){

echo "ID: ".$row['id']." - ".$row['articlename']." --   ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>";

}
?><

 

 

Any help is appreciated - thanks!

 

 

 

 

Link to comment
Share on other sites

 

Hey there,

 

Ok tried out this tutorial, customized it to my db...and applied it to my page, but its not displaying any

news articles at all. Im not getting any errors from the script, my page is just coming up blank..

 

:(

 

You did that tutorial in 15 minutes, man that's quick.

 

Do you have error reporting turned on? 

Do you echo out variables and sql errors?

Can we see your code?

Link to comment
Share on other sites

Hey,

 

Yeah I quickly read through the tutorial to get an idea of what it is that needs to be done...Then I applied it to my code..I believe error reporting is turned...But here is the code, if you want to check it out

 

 

	  <?php
$con = mysql_connect("","","") or die('Could not connect: ' . mysql_error());
mysql_select_db("legion", $con);
$result = mysql_query("SELECT * FROM elections");

while($row = mysql_fetch_assoc($result)){

echo "ID: ".$row['id']." - ".$row['articlename']." --   ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>";

} // end if

// find out how many rows are in the table    
$sql = "SELECT COUNT(*) FROM elections";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
$r = mysql_fetch_row($result);   
$numrows = $r[0];   
  
// number of rows to show per page   
$rowsperpage = 10;   
// find out total pages   
$totalpages = ceil($numrows / $rowsperpage);   
  
// get the current page or set a default   
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {   
   // cast var as int   
   $currentpage = (int) $_GET['currentpage'];   
} else {   
   // default page num   
   $currentpage = 1;   
} // end if   
  
// if current page is greater than total pages...   
if ($currentpage > $totalpages) {   
   // set current page to last page   
   $currentpage = $totalpages;   
} // end if   
// if current page is less than first page...   
if ($currentpage < 1) {   
   // set current page to first page   
   $currentpage = 1;   
} // end if   
  
// the offset of the list, based on current page    
$offset = ($currentpage - 1) * $rowsperpage;   
  
// get the info from the db    
$sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
  
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) {   
   // echo data   
   echo $list['id'] . " : " . $list['number'] . "<br />";   
} // end while   
  
/******  build the pagination links ******/  
// range of num links to show   
$range = 3;   
  
// if not on page 1, don't show back links   
if ($currentpage > 1) {   
   // show << link to go back to page 1   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
   // get previous page num   
   $prevpage = $currentpage - 1;   
   // show < link to go back to 1 page   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";   
} // end if    
  
// loop to show links to range of pages around current page   
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {   
   // if it's a valid page number...   
   if (($x > 0) && ($x <= $totalpages)) {   
      // if we're on current page...   
      if ($x == $currentpage) {   
         // 'highlight' it but don't make a link   
         echo " [<b>$x</b>] ";   
      // if not current page...   
      } else {   
         // make it a link   
     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";   
      } // end else   
   } // end if    
} // end for   
            
// if not on last page, show forward and last page links       
if ($currentpage != $totalpages) {   
   // get next page   
   $nextpage = $currentpage + 1;   
    // echo forward link for next page    
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";   
   // echo forward link for lastpage   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";   
} // end if   
?>  

 

Link to comment
Share on other sites

Ok, done,

 

Now its displaying the news articles, but its not creating the pagination, here are the errors its returning:

The weird thing is this script is only 100 lines, it doesn't go to 208

 

Notice: Undefined variable: conn in D:\hosting\member\264legion\site1\elections\index.php on line 208

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\hosting\member\264legion\site1\elections\index.php on line 208

 

Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 208

PHP Notice: Undefined variable: conn in D:\hosting\member\264legion\site1\elections\index.php on line 208 PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\hosting\member\264legion\site1\elections\index.php on line 208 PHP Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 208

Link to comment
Share on other sites

ou onl,y need to use

mysql_query($sql);

 

since mysql_query will use the current open conection.

 

you ca use if(isset($variable)){ /* do somethign with this variable */ }

to rid yourself of those "PHP Notice: Undefined variable" errors.

 

 

I fixed that portion, the error is still appearing though :(

Link to comment
Share on other sites

Here is the new code, double-check it to make sure I did what you said properly...

I made that replacement, but Im still getting the Fatal Error:

 

<?php
$con = mysql_connect("***","***","***") or die('Could not connect: ' . mysql_error());
mysql_select_db("legion", $con);
$result = mysql_query("SELECT * FROM elections");

while($row = mysql_fetch_assoc($result)){

echo "ID: ".$row['id']." - ".$row['articlename']." --   ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>";

} // end if

ini_set ("display_errors", "1");
error_reporting(E_ALL);

// find out how many rows are in the table    
$sql = "SELECT COUNT(*) FROM elections";   
$result = mysql_query($sql) or die(mysql_error());   
$r = mysql_fetch_row($result);   
$numrows = $r[0];   
  
// number of rows to show per page   
$rowsperpage = 10;   
// find out total pages   
$totalpages = ceil($numrows / $rowsperpage);   
  
// get the current page or set a default   
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {   
   // cast var as int   
   $currentpage = (int) $_GET['currentpage'];   
} else {   
   // default page num   
   $currentpage = 1;   
} // end if   
  
// if current page is greater than total pages...   
if ($currentpage > $totalpages) {   
   // set current page to last page   
   $currentpage = $totalpages;   
} // end if   
// if current page is less than first page...   
if ($currentpage < 1) {   
   // set current page to first page   
   $currentpage = 1;   
} // end if   
  
// the offset of the list, based on current page    
$offset = ($currentpage - 1) * $rowsperpage;   
  
// get the info from the db    
$sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage";   
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);   
  
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) {   
   // echo data   
   echo $list['id'] . " : " . $list['number'] . "<br />";   
} // end while   
  
/******  build the pagination links ******/  
// range of num links to show   
$range = 3;   
  
// if not on page 1, don't show back links   
if ($currentpage > 1) {   
   // show << link to go back to page 1   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
   // get previous page num   
   $prevpage = $currentpage - 1;   
   // show < link to go back to 1 page   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";   
} // end if    
  
// loop to show links to range of pages around current page   
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {   
   // if it's a valid page number...   
   if (($x > 0) && ($x <= $totalpages)) {   
      // if we're on current page...   
      if ($x == $currentpage) {   
         // 'highlight' it but don't make a link   
         echo " [<b>$x</b>] ";   
      // if not current page...   
      } else {   
         // make it a link   
     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";   
      } // end else   
   } // end if    
} // end for   
            
// if not on last page, show forward and last page links       
if ($currentpage != $totalpages) {   
   // get next page   
   $nextpage = $currentpage + 1;   
    // echo forward link for next page    
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";   
   // echo forward link for lastpage   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";   
} // end if   
/****** end build pagination links ******/  
?> 

Link to comment
Share on other sites

Ok, I got it all fixed...

The problem is now, its showing the page numbers ie:

[1], [2] etc..

 

But its showing them like this:

3:3

4:4

5:5

 

And its still displaying all the news articles on one page, not 4 on each page.

 

Thanks everyone fior all the input, any more help would be great

Link to comment
Share on other sites

your script is only 100 lines long but it's saying the error is on line 242... if that was the complete code you posted, is this file being included in some other file? If not, then perhaps you're trying to run the wrong script. 

 

Hey,

 

thanks for your input so far, the script is only infact 100 lines long...

So why its saying there is an error on line 242, is beyond me.

 

Ive since gotten all the page numbers to appear and all the errors to disappear, the only thing Im fighting with now, is its still showing all the news articles in the database on one page, I need it to only show 4 per page, and right now its showing all the articles in the database on one page, then when you click to page two, it shows all the articles again, and so on..

 

But the page numbers are showing and they are working so thats a step forward

Link to comment
Share on other sites

thanks for your input so far, the script is only infact 100 lines long...

So why its saying there is an error on line 242, is beyond me.

 

Well is this file, "site1\elections\index.php"?

 

Is it an include?

Link to comment
Share on other sites

Nope, the script is running on the index.php

 

it shouldn't be an include, Ill find it and remove it

 

Heres the most updated code if you want to take a look

 

<?php

$con = mysql_connect("","","") or die('Could not connect: ' . mysql_error());

mysql_select_db("", $con);

$result = mysql_query("SELECT * FROM elections");

 

while($row = mysql_fetch_assoc($result)){

 

echo "ID: ".$row['id']." - ".$row['articlename']." --  ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>";

 

 

} // end if

 

ini_set ("display_errors", "1");

error_reporting(E_ALL);

 

// find out how many rows are in the table   

$sql = "SELECT COUNT(*) FROM elections"; 

$result = mysql_query($sql) or die(mysql_error()); 

$r = mysql_fetch_row($result); 

$numrows = $r[0]; 

 

// number of rows to show per page 

$rowsperpage = 1; 

// find out total pages 

$totalpages = ceil($numrows / $rowsperpage); 

 

// get the current page or set a default 

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { 

  // cast var as int 

  $currentpage = (int) $_GET['currentpage']; 

} else { 

  // default page num 

  $currentpage = 1; 

} // end if 

 

// if current page is greater than total pages... 

if ($currentpage > $totalpages) { 

  // set current page to last page 

  $currentpage = $totalpages; 

} // end if 

// if current page is less than first page... 

if ($currentpage < 1) { 

  // set current page to first page 

  $currentpage = 1; 

} // end if 

 

// the offset of the list, based on current page   

$offset = ($currentpage - 1) * $rowsperpage; 

 

// get the info from the db   

$sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage"; 

$result = mysql_query($sql) or die(mysql_error()); 

 

// while there are rows to be fetched... 

while ($list = mysql_fetch_assoc($result)) { 

  // echo data 

  echo $list['id'] . " : " . $list['number'] . "<br />"; 

} // end while 

 

/******  build the pagination links ******/ 

// range of num links to show 

$range = 3; 

 

// if not on page 1, don't show back links 

if ($currentpage > 1) { 

  // show << link to go back to page 1 

  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; 

  // get previous page num 

  $prevpage = $currentpage - 1; 

  // show < link to go back to 1 page 

  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; 

} // end if   

 

// loop to show links to range of pages around current page 

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { 

  // if it's a valid page number... 

  if (($x > 0) && ($x <= $totalpages)) { 

      // if we're on current page... 

      if ($x == $currentpage) { 

        // 'highlight' it but don't make a link 

        echo " [<b>$x</b>] "; 

      // if not current page... 

      } else { 

        // make it a link 

    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; 

      } // end else 

  } // end if   

} // end for 

           

// if not on last page, show forward and last page links     

if ($currentpage != $totalpages) { 

  // get next page 

  $nextpage = $currentpage + 1; 

    // echo forward link for next page   

  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; 

  // echo forward link for lastpage 

  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; 

} // end if 

/****** end build pagination links ******/ 

?>

Link to comment
Share on other sites

Echo the variables make sure the are what you want them to be.

 

i'd start with the query (would be why your getting every row rather than the 4 you want):

 

echo("SQL QUERY: ".$sql."<br />");
$sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage";   
$result = mysql_query($sql) or die(mysql_error());  

 

Then with the variables that deal with the page numbers.

-----

 

Also when you add code, highlight it and click the hash button ( # ), just above the emoticons (smileys).

Link to comment
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.