[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

[SOLVED]

Post by Wldrumstcs »

For some reason, this code I borrowed from PHP Freaks isn't working. The page numbers that I click on ie "1 2 3" don't all show up, only "1" despite there being multiple pages. Also, the "NEXT" link doesn't show up at all. I used a limit of 1 per page just to get this thing working. I will switch it to something like 30 once I do. Anywho, here's my code:

Code: Select all

<?
$color1 = "#FF6103";  
$color2 = "#FFA54F";  
$row_count = 0; 

mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$limit = 1;                
$query_count = "SELECT count(*) FROM assignments";     
$result_count = mysql_query($query_count);     
$totalrows = mysql_num_rows($result_count); 

    if(empty($page))&#123; 
        $page = 1; 
    &#125; 
         
    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM assignments $order LIMIT $limitvalue, $limit";         
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

while($row = mysql_fetch_array($result))&#123; 
$teacher=$row&#1111;"teacher"];
$subject=$row&#1111;"subject"];
$date = date("m-d-Y",$row&#1111;date]);
$assignment=$row&#1111;"assignment"];


    $row_color = ($row_count % 2) ? $color1 : $color2; 

	echo "
		<tr onMouseOver="this.bgColor = '#E8E8E8'"    onMouseOut = "this.bgColor = '$row_color'"    bgcolor='$row_color'>
				<td align='center' width='25%'>
				$teacher</td>
				<td align='center' width='25%'>
				$subject</td>
				<td align='center' width='25%'>
				$date</td>
				<td align='center' width='25%'>
				$assignment</td>
</tr>
					
			";
			    $row_count++; 

&#125;



?>			
</table>
<br><br><p align="center">
<?
if($page != 1)&#123; 
        $pageprev = $page-1; 
         
        echo("<a href="homework.php?page=$pageprev&orderby=date&arrangement=DESC">PREV".$limit."</a>&nbsp;"); 
    &#125;else&#123; 
        echo(""); 
    &#125; 

    $numofpages = $totalrows / $limit; 
     
    for($i = 1; $i <= $numofpages; $i++)&#123; 
        if($i == $page)&#123; 
            echo($i."&nbsp;"); 
        &#125;else&#123; 
            echo("<a href="homework.php?page=$i&orderby=date&arrangement=DESC">$i</a>&nbsp;"); 
        &#125; 
    &#125; 


    if(($totalrows % $limit) != 0)&#123; 
        if($i == $page)&#123; 
            echo($i."&nbsp;"); 
        &#125;else&#123; 
            echo("<a href="homework.php?page=$i&orderby=date&arrangement=DESC">$i</a>&nbsp;"); 
        &#125; 
    &#125; 

    if(($totalrows - ($limit * $page)) > 0)&#123; 
 
$pagenext   = $page++; 
          
       echo("<a href="homework.php?page=$pagenext&orderby=date&arrangement=DESC">NEXT".$limit."</a>"); 
&#125;else&#123;
        echo(""); 
    &#125; 
     
    mysql_free_result($result); 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

there is no point on doing this

Code: Select all

$query_count = "SELECT count(*) FROM assignments";      
$result_count = mysql_query($query_count);      
$totalrows = mysql_num_rows($result_count);
your query is already counting the total number of rows....
so no point on num_rows
Post Reply