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
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
Link to comment
Share on other sites

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
Share on other sites

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
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.