Forum Moderators: coopster

Message Too Old, No Replies

"If" question

display picture ONLY if one is found

         

mcjohnson

1:54 pm on Sep 2, 2006 (gmt 0)

10+ Year Member



Hi All,

I have a simple bit of code where I am displaying articles and photos called from a MySQl db. I am interested in displaying the photo ONLY if a photo is found, otherwise, leaving it out so there's no "broken" picture icon.

I assume this requires an "if isset" command? rather than just an if/else type of command. I've looked in my two PHP/MySQl tutorials and am not finding the resolution. Here's the code:

<?PHP $query = "SELECT * FROM news_publish WHERE news_id = ".$_GET["news_id"];
$results=mysql_query($query) or die (mysql_error());
while ($rows=mysql_fetch_array($results)) {
extract ($rows);

echo stripslashes("<b><font color=#005488>$title</font></b>");
echo "<br>";
echo stripslashes($summary);
echo "</p>";
echo $content;
echo "<img src='http://www.domain_here.com/script_here/_files/news_here/$thumbnail' border=1>";
}?>

Any recommendations would be great. Thanks, as always!

Pat

barns101

2:01 pm on Sep 2, 2006 (gmt 0)

10+ Year Member



This should do the trick:

<?PHP $query = "SELECT * FROM news_publish WHERE news_id = ".$_GET["news_id"];
$results=mysql_query($query) or die (mysql_error());
while ($rows=mysql_fetch_array($results)) {
extract ($rows);

echo stripslashes("<b><font color=#005488>$title</font></b>");
echo "<br>";
echo stripslashes($summary);
echo "</p>";
echo $content;
if(isset($thumbnail))
{
echo "<img src='http://www.domain_here.com/script_here/_files/news_here/$thumbnail' border=1>";
}
else
{
// Do something else
}
// Destroy the $thumbnail variable so that it wont be automatically present in the next instance
unset($thumbnail);
}?>

dreamcatcher

4:36 pm on Sep 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also use file_exists [uk2.php.net] to check if the file actually exists on the server.

dc

mcjohnson

3:11 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



I am still stuck. Here is the language I am currently using:

<?PHP
// rows to return

$date = date("Y-m-d");
$title =$row['title'];
$summary =$row['summary'];
$news_id = $row['news_id'];
$thumbnail = $row['thumbnail'];
$filename = 'folder/_files/folder/$thumbnail';

$query = "SELECT * FROM news_publish WHERE news_id = ".$_GET["news_id"];
$results=mysql_query($query) or die (mysql_error());
while ($rows=mysql_fetch_array($results)) {
extract ($rows);
echo stripslashes("<b><font color=#005488>$title</font></b>");
echo "<br>";
echo stripslashes("<i>$summary</i>");
echo "</p>";
echo stripslashes ($content);
if(isset($filename))
{
echo "<img src='http://www.domain.com/folder/_files/folder/$thumbnail' border=1>";
}
else
{
echo "<img src='http://www.example.com/images/loc.gif' border=0>";
}
unset($filename);
}?>

What is happening is that the "else" is not being performed, it's just finding a broken image place holder.

$thumbnail is merely a row in the table that carries a NAME of an image. The script then goes into the proper folder to find and display it, if it exists. For some reason, however, when it is NOT present, I am unable to get the script above to recognize that fact.

Any help would be great. Thank yoU!

Pat

[edited by: jatar_k at 4:38 pm (utc) on Sep. 5, 2006]
[edit reason] examplified [/edit]

barns101

3:56 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



You have defined $filename right at the top, so it will be definitely be present in the first loop.

mcjohnson

9:51 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



I am not sure what the proper fix would be. I relocated the definition of the $filename variable, but that did not do the trick. Any suggesitons?

jatar_k

9:56 pm on Sep 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



we are focusing on 2 lines

$filename = 'folder/_files/folder/$thumbnail';

if(isset($filename))

as is the condition in the if statement wil always be true, let's go to the manual

[php.net...]

Returns TRUE if var exists; FALSE otherwise.

so, if you initialize the variable then it will always be true

my wild guess is that you are testing to make sure that $thumbnail had a value, this test might work

if(!empty($thumbnail))

though this may not be the case, if not

what is it that you are testing for?