Page 1 of 1

If No Value Do Something Else

Posted: Thu Aug 21, 2008 4:24 pm
by CoolAsCarlito
This is a simple questiion. I have a script that I have a a few things pulled from a database table and what I want to happen is if ' . $row['contender1'] . ' returns a value of nothing/null I want it to put "No Contender to display." but also with that I have another image that's in this script that has img src"/images/' . $row['champion'] . '" returns as no image there I want it to always put the image located at images/nopic.jpg.

What do I need to do to accomplish this?

Re: If No Value Do Something Else

Posted: Thu Aug 21, 2008 4:35 pm
by nowaydown1
Greetings. Probably something pretty close to:

Code: Select all

 
if(empty($row['contender1'])) {
    echo "No contender to display.";
}
 
And

Code: Select all

 
$imageFilename = $row['champion'];
if(empty($imageFilename)) {
    $imageFilename = 'nopic.jpg'
}
 
Hope that helps.

Re: If No Value Do Something Else

Posted: Thu Aug 21, 2008 4:54 pm
by CoolAsCarlito
Thank you heres a one more thing. What if I have multiple fields that in case are empty I want that message displayed.

Can I do something like: if(empty($row['contender1']), ($row['contender2']),($row['contender3'])) {
echo "No contender to display.";
}

I didn't want to try it and have it be an error. Can you just separate them with a comma?

Re: If No Value Do Something Else

Posted: Thu Aug 21, 2008 4:56 pm
by onion2k
CoolAsCarlito wrote:I didn't want to try it and have it be an error.
Why not? You'll learn this stuff a lot faster if you try things for yourself and learn from the error messages.

Re: If No Value Do Something Else

Posted: Thu Aug 21, 2008 7:17 pm
by CoolAsCarlito
Well it does still help me however I'm trying to put it into different places to get it to post right but I can't get it to work correctly with my code. Maybe I'm putting it in the wrong spot.

Re: If No Value Do Something Else

Posted: Thu Aug 21, 2008 7:24 pm
by CoolAsCarlito
Sorry forgot to post my code.

Code: Select all

 
<?php echo "<body bgcolor=\"black\" text=\"white\" link=\"red\" vlink=\"red\">"; ?>
 
 
<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());
 
if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}
    
//Define the query
$query = "SELECT *, DATE_FORMAT(`datewon`, '%M %e, %Y') AS datewon FROM titles";
 
 
if ($r = mysql_query ($query)){ // Run the query. 
 
print '<table border=0 cellspacing="0" cellpadding=3 width=575><tr><td background="images/bg_bar4.gif" height=35><font color="white" size="4">&nbsp;Champions</font></td></tr><tr><td></td></tr></table>';
// Retrieve and print every record
while ($row = mysql_fetch_array ($r)){
print '<table border=0 width=575>';
print '<tr><td background="images/bg_bar3.gif" height=35 colspan=3>&nbsp;<font size="4" color="black">'.$row['titlename'].'</font></td></tr>';
print '<tr><td valign=top width=200><a href=titlehistory.php?id=' . $row['id'] . ' title="View KOW '.$row['titlename'].' History"><img src="/images/' . $row['titleimage'] . '" width=200 height=200 border=0 alt="View KOW '.$row['titlename'].' History"></a></td>';
print '<font size="2"><td valign=top width=160><a href="bio.php?id=' . $row['id'] . '" title="View History for ' . $row['champion'] . '" target="_blank"><img src=/images/' . $row['champion'] . ' height=200 width=160 border=0></a></td>';
print '<td valign=top><font size="2"><b>Current Champion</b><br><a href="bio.php?id=' . $row['id'] . '" target="_blank"><b>' . $row['champion'] . '</b></a><p><b>Date Won</b><br>'.$row['datewon'].'<p><b>Contenders<br>1. <a href="bio.php?id=' . $row['id'] . '" target="_blank">' . $row['contender1'] . '</a><br>2. <a href="bio.php?id=' . $row['id'] . '" target="_blank">' . $row['contender2'] . '</a><br>3. <a href="bio.php?id=' . $row['id'] . '" target="_blank">' . $row['contender3'] . '</a><br></b></font></td></tr>';
print '</table>';
print '<img src=images/spacer.gif>';
print '</table>';
}
 
} else {
die ('<p>Could not retrieve the data because <b>' . mysql_error() . '</b>. The query was $query.</p>');
} //End of query IF 
?>
 
 
 

Re: If No Value Do Something Else

Posted: Fri Aug 22, 2008 4:23 pm
by CoolAsCarlito
Thanks for all your help but working on it for a while I finally got it to work.