Forum Moderators: coopster
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
<?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);
}?>
<?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]
$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?