Jump to content

echo results only if file exists


newb

Recommended Posts

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

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

  :P

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";
}
}

 

 

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.

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.