Jump to content

MySQL Query with Limit 12 when there isn't 12 results to display, there is less


TeddyKiller

Recommended Posts

I have a piece of code, which will display 12 avatars at random.

So far if there isn't 12 results, there is less.. it'll only display the results found.

This is because of the mysql_num_rows.

It needs changing so that if lets say there is 4 results to display, to display those 4, with a default nopic for the rest till there is 12 results.

 

<?php
$sql = mysql_query("SELECT Avatar FROM users WHERE Activated = '1' ORDER BY RAND() LIMIT 12");
if (mysql_num_rows($sql) > 0) {
  while ($item = mysql_fetch_array($sql)) {
    if ($item['Avatar'] > 0) {
      echo "<img src=\"".$item['Avatar']."\" alt=\"user\" />";
    }
  }
}
// The following echo is the default pic that you'll need to use.
echo '<img src="profile/photos/default/default.jpg" alt="default" />';
?>

Link to comment
Share on other sites

I'm not really sure why you'd want to repeat the default pic over and over, but whatever.

 

<?php
$num_printed = 0;
$sql = mysql_query("SELECT Avatar FROM users WHERE Activated = '1' ORDER BY RAND() LIMIT 12");
if (mysql_num_rows($sql) > 0) {
  while ($item = mysql_fetch_array($sql)) {
    if ($item['Avatar'] > 0) { // <-- I'M NOT REALLY SURE WHAT THE INTENT OF THIS LINE IS
      echo "<img src=\"".$item['Avatar']."\" alt=\"user\" />";
      $num_printed += 1;
    }
  }
}
// The following echo is the default pic that you'll need to use.
while( $num_printed < 12 ) {
    echo '<img src="profile/photos/default/default.jpg" alt="default" />';
    $num_printed += 1;
}
?>

Link to comment
Share on other sites

Ah, it was an issue with the photo.

Ok I have sorted it out now, although it's not displaying any results from the database.

What it looks like it does, if there isn't 12 results, to display 12 of the default photo.

It's suppose to display what is in the database aswell..

Any help?

Link to comment
Share on other sites

Well I didn't do anything to your code that would stop it from displaying results from the database.  So if it's not doing that then you have a problem with your mysql_query(), mysql_fetch() code.

 

What does this output:

<?php
$num_printed = 0;
$sql = mysql_query("SELECT Avatar FROM users WHERE Activated = '1' ORDER BY RAND() LIMIT 12");
var_dump( $sql );echo "<br />";//REMOVE ME LATER
if (mysql_num_rows($sql) > 0) {
  while ($item = mysql_fetch_array($sql)) {
  echo "<pre>" . var_export( $item, true ) . "</pre>";//REMOVE ME LATER
    if ($item['Avatar'] > 0) { // <-- I'M NOT REALLY SURE WHAT THE INTENT OF THIS LINE IS
      echo "<img src=\"".$item['Avatar']."\" alt=\"user\" />";
      $num_printed += 1;
    }
  }
}
// The following echo is the default pic that you'll need to use.
while( $num_printed < 12 ) {
    echo '<img src="profile/photos/default/default.jpg" alt="default" />';
    $num_printed += 1;
}
?>

Link to comment
Share on other sites

I'm guessing that $item['Avatar'] is a string.  In your if-statement you were comparing a string to an int.  When this occurs, PHP will attempt to convert the string to an int.  When you convert a string to an int, if the first character is not a digit then the string is converted to 0.  When that happens, your if-statement became:

if( 0 > 0 )

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.