newb Posted August 8, 2010 Share Posted August 8, 2010 Ok, here's my code: How do i make it so that it outputs a maximum of only 5 results from the query where the img file exists. When I add LIMIT to the sql query it doesnt work so I guess its something else, but I have no clue. Can anyone help? $query = mysql_query("SELECT * FROM table WHERE date >= '$now' ORDER BY max(date) desc"); while ($row = mysql_fetch_assoc($query)) { $cid = $row['cat_id']; $title = $row['name']; $seoname = $row['seourl']; $img = 'images/'.$seoname.'.jpg'; if (!file_exists($img)) { $img = ''; } else { $img = 'images/'.$seoname.'.jpg'; } if (!empty($img)) { echo '<a href="images/'.$seoname.'/"><img src="'.$img .'" style="width:528px;" alt="" /></a>' . "\n"; } } Thanks. Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/ Share on other sites More sharing options...
newb Posted August 8, 2010 Author Share Posted August 8, 2010 can anyone help Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096700 Share on other sites More sharing options...
newb Posted August 8, 2010 Author Share Posted August 8, 2010 anyone? Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096725 Share on other sites More sharing options...
TOA Posted August 8, 2010 Share Posted August 8, 2010 LIMIT should work. What happens? Or doesn't? And change this line $query = mysql_query("SELECT * FROM table WHERE date >= '$now' ORDER BY max(date) desc"); to $sql = "SELECT * FROM table WHERE date >= '$now' ORDER BY max(date) desc"; $query = mysql_query($sql) or die(mysql_error()); while testing because the answer is never it doesnt work Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096727 Share on other sites More sharing options...
newb Posted August 8, 2010 Author Share Posted August 8, 2010 i think LIMIT will limit the files that dont exist also or smthing so it doesnt work...can anyone help? Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096754 Share on other sites More sharing options...
newb Posted August 8, 2010 Author Share Posted August 8, 2010 nvmd fixed, heres the code i used to fix if anyone cares. $query = mysql_query("SELECT * FROM table WHERE date >= '$now' ORDER BY max(date) desc"); $i = 0; while ($row = mysql_fetch_assoc($query)) { $cid = $row['cat_id']; $title = $row['name']; $seoname = $row['seourl']; $img = 'images/'.$seoname.'.jpg'; if (!file_exists($img)) { $img = ''; } else { $img = 'images/'.$seoname.'.jpg'; } if (!empty($img)) { $i++; } if (!empty($img) && $i <= 5) { echo '<a href="images/'.$seoname.'/"><img src="'.$img .'" style="width:528px;" alt="" /></a>' . "\n"; } } Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096766 Share on other sites More sharing options...
Rohlan Posted August 8, 2010 Share Posted August 8, 2010 I'm going to assume this is what's happening: When you use LIMIT = 5 you get 5 results but then because their images don't exit, you simply get no output because of your conditions. If that is the case, I would use a simple counter, like this: $query = mysql_query("SELECT * FROM table WHERE date >= '$now' ORDER BY max(date) desc"); $cid = $row['cat_id']; $title = $row['name']; $seoname = $row['seourl']; $img = 'images/'.$seoname.'.jpg'; $counter = 0; while ($row = mysql_fetch_assoc($query)) { if (!file_exists($img)) { $img = ''; } else { $img = 'images/'.$seoname.'.jpg'; } if (!empty($img)) { echo '<a href="images/'.$seoname.'/"><img src="'.$img .'" style="width:528px;" alt="" /></a>' . "\n"; $counter++; //$counter becomes $counter+1 } //when counter reaches 5, break off the loop //where 5 is your result "limit" if ($counter == 5) { break; } } I'm pretty sure there's a more direct way to do this but this is how I would do it until something better is suggested. Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096768 Share on other sites More sharing options...
Rohlan Posted August 8, 2010 Share Posted August 8, 2010 I posted a reply right after you hehe... glad you fixed it. Link to comment https://forums.phpfreaks.com/topic/210142-echo-results-only-if-file-exists/#findComment-1096769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.