TeddyKiller Posted March 10, 2010 Share Posted March 10, 2010 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 https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/ Share on other sites More sharing options...
roopurt18 Posted March 10, 2010 Share Posted March 10, 2010 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 https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024436 Share on other sites More sharing options...
TeddyKiller Posted March 10, 2010 Author Share Posted March 10, 2010 Thanks. Although it only displays the alt description and not the actual image. It did that before, but yeah. Can you help? Link to comment https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024437 Share on other sites More sharing options...
TeddyKiller Posted March 10, 2010 Author Share Posted March 10, 2010 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 https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024450 Share on other sites More sharing options...
roopurt18 Posted March 11, 2010 Share Posted March 11, 2010 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 https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024490 Share on other sites More sharing options...
TeddyKiller Posted March 11, 2010 Author Share Posted March 11, 2010 It's alright now thanks. All I did was remove "if ($item['Avatar'] > 0) {" with the closing bracket too. Need the resize now, but there is a seperate thread for that. Link to comment https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024492 Share on other sites More sharing options...
roopurt18 Posted March 11, 2010 Share Posted March 11, 2010 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 https://forums.phpfreaks.com/topic/194825-mysql-query-with-limit-12-when-there-isnt-12-results-to-display-there-is-less/#findComment-1024494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.