Jump to content

[SOLVED] Implementing CV’s Basic Pagination to Eric’s News Articles?


iQue

Recommended Posts

OK I’ve been trying for a few days now on trying to implement CV’s Basic Pagination to and existing code written by Eric?

 

Both codes work perfect, but separately…what I would like to achieve, is to get the Pagination to work with the News Article Code so that it will display on separate pages.

 

Both are writing slightly different so I’m not sure where to put the Pagination code as the News Article Code uses cases switches. I have played around with the code and managed to get it to a point on where it would show the News Article Code fine but did not show any data???

 

  :confused:

 

If anyone can help or point me in the right direction I would be most great full.

 

 

 

The following code will be replaced with the articles code on the News Articles Page but because I don’t know much about the code or switches so I'm not sure what needs to be changed?

 

// while there are rows to be fetched...
echo "<table>";
while ($list = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td><font color='green'>" . $list['article_id'] . "</font></td>";
echo "<td><font color='red'>" . $list['article_title'] . "</font></td>";
echo "<td><font color='blue'>" . $list['article_date'] . "</font></td>";
echo "</tr>";
} // end while
echo "</table>";

?>

 

 

 

 

CV's Pagination Code

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM news_articles";
$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;

footer();
  break; 

// get the info from the db 
$sql = "SELECT article_id, article_title, date_format(article_date, '%M %D, %Y') as article_date  
FROM news_articles 
ORDER BY article_id 
DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);


// while there are rows to be fetched...
echo "<table>";
while ($list = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td><font color='green'>" . $list['article_id'] . "</font></td>";
echo "<td><font color='red'>" . $list['article_title'] . "</font></td>";
echo "<td><font color='blue'>" . $list['article_date'] . "</font></td>";
echo "</tr>";
} // end while
echo "</table>";


if ($result > 0) { 

/******  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 ******/
}

 

 

 

 

News Article Code


switch($_REQUEST['req']){
   case "read":
      $sql = mysql_query("SELECT *, 
      date_format(article_date, '%M %D, %Y') as article_date 
      FROM news_articles
      WHERE article_id='{$_GET['article_id']}'");
      
      mysql_query("UPDATE news_articles
      SET article_read_count = 
      (article_read_count +1) WHERE
      article_id = '{$_GET['article_id']}'");

      $row = mysql_fetch_assoc($sql);
      stripslashes(extract($row));
      $cat_name = mysql_result(mysql_query("SELECT cat_name
            FROM news_categories
            WHERE cat_id='$cat_id'"),0);
      
      myheader("$article_title");
      ?>   

      	<table width="100%" border="0" cellpadding="2" cellspacing="0">
      	<tr>
      	<td valign="bottom">
      	<font size="4" face="Verdana, Arial, Helvetica, sans-serif">
      	<strong><?=stripslashes($cat_name)?> <?=$article_title?></strong>
      	</font>
      	</td>
</tr>
</table>
      
      	<table width="100%" border="0" cellpadding="2" cellspacing="0">
      	<tr>
      	<td valign="bottom"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><?=$article_date?> by <em><?=$article_author?></em></font>
      	</td>
</tr>
      	<tr>
      	<td valign="top"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Views: <?=$article_read_count?></font>
      	</td>
      	</tr>
      	</table>

      	<table width="100%" border="0" cellpadding="2" cellspacing="0"> 
      	<tr>
      	<td valign="top">       
      	<table width="100%" border="0" cellpadding="2" cellspacing="0"> 
      	<tr>
      	<td valign="top"><p align="justify"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><?=$article_caption?></font>
      	</p>  
      	</td>
      	</tr>
      	</table>
      	</td>
      	</tr>
      	</table>
<hr size="1">
      	<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
      	<a href="/articles.php">Back to News</a><br />
      	<a href="/articles.php?req=category&cat_id=<?=$cat_id?>">
      	View Articles From <?=stripslashes($cat_name)?> Category</a>
      	</font>
      
<?php
     	footer();
   	break;
   
   	case "category":
      	$cat_name = mysql_result(mysql_query("SELECT cat_name
        FROM news_categories
        WHERE cat_id='{$_GET['cat_id']}'"),0);
      
      	myheader("News Articles ".stripslashes($cat_name));
      	?>
   
      <!-- Articles Begin-->
      <table width="100%" border="0" cellpadding="2" cellspacing=0>
      <tr>
      <td>
      <font size="4" face="Verdana, Arial, Helvetica, sans-serif">
      <strong><?=stripslashes($cat_name)?></strong></font>
      </td>
      </tr>
      <tr>
      <td>
   
      <?php
      $sql = mysql_query("SELECT *,
         date_format(article_date, '%M %D, %Y') as article_date 
         FROM news_articles
         WHERE cat_id='{$_GET['cat_id']}' 
         ORDER BY article_id 
         DESC");
      	
      while($row = mysql_fetch_array($sql)){
         stripslashes(extract($row));
         $cat_name = mysql_result(mysql_query("SELECT cat_name
               FROM news_categories
               WHERE cat_id='$cat_id'"),0);
        ?>
      <table width="100%" border="0" cellpadding="2" cellspacing="0">
      <tr>
      <td valign="bottom"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><?=$article_date?> by <em><?=$article_author?></em></font>
      </td></tr>
     <tr>
      <td valign="bottom">
      <font size="3" face="Verdana, Arial, Helvetica, sans-serif">
      <a href="/articles.php?req=read&article_id=<?=$article_id?>">
       <?=stripslashes($cat_name)." ".$article_title?></a>
      </font><br />
      </td>
      </tr>
      <tr>
      <td valign="top">
         <table width="100%" border="0" cellpadding="2" cellspacing="0"> 
      <tr>
      <td valign="top"><p align="justify">
      <font size="2" face="Verdana, Arial, Helvetica, sans-serif">
      <?=$article_caption?>
      </font>
      </p>  
      </td>
      </tr>
      </table>
      </td>
      </tr>
      <tr>
      <td valign="top">
      <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
      Views: <?=$article_read_count?></font>
      </td>
      </tr>
      </table>
         <hr size="1">
      <?php
      }
      ?>
      </td>
      </tr>
</table>
      <!-- Articles End-->
      
      <?php
      footer();
  break; 
   
   default:
   myheader("News");
   ?>

   <!-- Articles Begin-->
   <table width="100%" border="0" cellpadding="2" cellspacing=0>
   <tr>
   <td>
   <font size="4" face="Verdana, Arial, Helvetica, sans-serif">
   <strong>Latest News</strong></font></td>
   </tr>
   <tr>
   <td>

   <?php
   $sql = mysql_query("SELECT *,
      date_format(article_date, '%M %D, %Y') as article_date 
      FROM news_articles
      ORDER BY article_id 
      DESC");
   	
   while($row = mysql_fetch_array($sql)){
      stripslashes(extract($row));
      $cat_name = mysql_result(mysql_query("SELECT cat_name
            FROM news_categories
            WHERE cat_id='$cat_id'"),0);
     ?>
      <table width="100%" border="0" cellpadding="2" cellspacing="0">
      <tr>
      <td valign="bottom"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><?=$article_date?> by <em><?=$article_author?></em></font>
      </td></tr>
     <tr>
      <td valign="bottom">
      <font size="3" face="Verdana, Arial, Helvetica, sans-serif">
      <a href="/articles.php?req=read&article_id=<?=$article_id?>">
       <?=stripslashes($cat_name).": ".$article_title?></a>
      </font><br />
      </td>
      </tr>
      <tr>
      <td valign="top">
         <table width="100%" border="0" cellpadding="2" cellspacing="0"> 
      <tr>
      <td valign="top"><p align="justify">
      <font size="2" face="Verdana, Arial, Helvetica, sans-serif">
      <?=$article_caption?>
      </font>
      </p>  
      </td>
      </tr>
      </table>
      </td>
      </tr>
      <tr>
      <td valign="top">
      <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
      Views: <?=$article_read_count?></font>
      </td>
      </tr>
      </table>
      <hr size="1">
   <?php
   }
   ?>
   </td>
   </tr>
   </table>
   <!-- Articles End -->
   
   <?php
   footer();
  break; 
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.