Jump to content

[SOLVED] My code can`t count?


djfox

Recommended Posts

For whatever reason, it seems as though my code can`t count. I`ve set the offset to be at 50, and it is displaying every single item from the table rather than just the 50. I`ve copied and pasted the code form another file which uses the same system and works successfully, but it still seems to have trouble counting on this file I`m trying to get this to work for.

 

<a href="mlpcollection_main.php?g=<? echo "$g"; ?>&sort=newest">Newest</a> | <a href="mlpcollection_main.php?g=<? echo "$g"; ?>&sort=alpha">Alphabetical</a>
<p>
<?php 
  if (isset($_GET['sort']) && $_GET['sort'] == "newest") {
  if(!$offset) $offset=0;
  $recent = 0;
  $res = mysql_query("SELECT id, image, date, date2, name, descr, type, gen, cond, owner FROM ponycol WHERE owner='$g' ORDER BY id DESC")or die( mysql_error() );
  while( $row = mysql_fetch_row($res) ){
if( $recent >= $offset && $recent < ($offset + 50 )){
    echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>";
}
      }
$recent = $recent + 1;
}
?>
<p>
<table border=0 width=100%>
<tr>
<td><?php
if ($offset >= 50) {
?><b><a href="mlpcollection_main.php?g=<? echo $g ?>&sort=<? echo "$sort"; ?>&offset=<? echo $offset - 50 ?>"><img src="<? echo $butprev ?>" border=0></a></b>
<?php
}
?>
<td><div align=right>
<b><a href="mlpcollection_main.php?g=<? echo $g ?>&sort=<? echo "$sort"; ?>&offset=<? echo $offset + 50 ?>"><img src="<? echo $butnext ?>" border=0></a></b>
</div>
</table>

 

The Next/Previous counts show up properly, they add/subtract the correct number from the correct field.

 

Break-down:

Instead of showing just 50 items, it shows every single item in the table.

Link to comment
https://forums.phpfreaks.com/topic/106083-solved-my-code-cant-count/
Share on other sites

Two things.

 

1) A better way to limit your results to 50 is to use "LIMIT 50" in your query! Then ther is no need to synthetically limit the results in the PHP code.

 

2) If you break down the relevent code it shoud be obvious why it is not working as you intend

 

<?php
  //$offset is not already set so it will be set to 0
  if(!$offset) $offset=0;
  //$recent is set to 0
  $recent = 0;
  $res = mysql_query("SELECT id, image, date, date2, name, descr, type, gen, cond, owner FROM ponycol WHERE owner='$g' ORDER BY id DESC")or die( mysql_error() );

  while( $row = mysql_fetch_row($res) ){

      //$recent and $offeset never get modified (in the loop) so this will always be true
      if( $recent >= $offset && $recent < ($offset + 50 )){
          echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>";
      }
  }
  //This is outside the loop
  $recent = $recent + 1;

To see the page this is being done to: http://secrettrance.net/mlpcollection_main.php?g=97&sort=newest

 

2) If you break down the relevent code it shoud be obvious why it is not working as you intend

 

My first post:

I`ve copied and pasted the code form another file which uses the same system and works successfully

 

This copied and pasted code from another file works on the other file.

 

 

It`s still showing every single item in the table.

To see the page this is being done to: http://secrettrance.net/mlpcollection_main.php?g=97&sort=newest

 

I didn't supply "corrected" code. I added comments to show why it is not working as you intended. Here is a better way to do this:

 

<?php 
if (isset($_GET['sort']) && $_GET['sort'] == "newest") {

  $query = "SELECT id, image, date, date2, name, descr, type, gen, cond, owner
            FROM ponycol
            WHERE owner='$g'
            LIMIT 50
            ORDER BY id DESC";
  $res = mysql_query($query)or die( mysql_error() );

  while( $row = mysql_fetch_row($res) ){
    echo "<a href='mlpcollection.php?id=$row[0]'>$row[4]</a><br>";
  }
}
?>

Well, the full syntax for limit is:

 

LIMIT start, rows

 

In other words, LIMIT 20, 5, would show 20 rows (if they exist) starting from row 5.

 

Knowing that, you could make the 20 dynamic.  For example, you could have, ?p=1 on your URL to indicate page 1.

 

Example:

 


$per_page = 25;

$page = (isset($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1;


//you would want page 1 to start from 0, page 2 to start from $per_page and so on....  So, you would do:

$l = ($page-1)*$per_oage;

$query = mysql_query("SELECT something FROM sometable LIMIT {$l}, $per_page ORDER BY some_other_column_or_something");

//if rows exist, show them and what not
//if not, say no rows exist

//echo links for the previous/next page
//(do a check to make sure there is a previous/next page of course)



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.