Xtremer360 Posted August 22, 2008 Share Posted August 22, 2008 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. I came up with this but don't where to enter these into it.: if(empty($row['contender1'])) { echo "No contender to display."; } and $imageFilename = $row['champion']; if(empty($imageFilename)) { $imageFilename = 'nopic.jpg' } Here's my regular script code: <?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"> 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> <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 ?> Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 <?php $contender = $row['contender']; $image = $row['champion']; $imagedir = 'images/champions/'; if(!$contender){ //an empty string returns 0, thus adding the NOT logical operator returns 1 which triggers the statement. If not empty, it will return 0 echo 'No contender to display'; } if(file_exists($imagedir . $image)){ //check file existence echo "<img src='" . $imagedir . $image . "' />"; } else{ echo "<img src='" . $imagedir . "nopic.jpg' />"; } ?> Quote Link to comment Share on other sites More sharing options...
Xtremer360 Posted August 22, 2008 Author Share Posted August 22, 2008 looks good now does it really matter where I place that? Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 What matters is your script flow and logic. You can place those wherever you like, just be sure you're checking the values after you've run the query and fetched the array (inside the while for example). Quote Link to comment Share on other sites More sharing options...
Xtremer360 Posted August 22, 2008 Author Share Posted August 22, 2008 Here's what I have now and it'll put it in the right place however I just went into my database table and put a name and even it having a contender for one of the contenders it still puts the No contender to display label even if there is a contender. <?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. $contender = $row['contender1']; $image = $row['champion']; $imagedir = 'images/champions/'; print '<table border=0 cellspacing="0" cellpadding=3 width=575><tr><td background="images/bg_bar4.gif" height=35><font color="white" size="4"> 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> <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.'; if(!$contender){ //an empty string returns 0, thus adding the NOT logical operator returns 1 which triggers the statement. If not empty, it will return 0 echo 'No contender to display'; print ' <a href="bio.php?id=' . $row['id'] . '" target="_blank">' . $row['contender1'] . '</a>'; } print'<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 ?> Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 It makes no sense. You are printing the image first, then checking if contender is empty!! The image should be printed based on the if() statement. Try echo $contender to be sure it prints out a value and if it prints but the "no contender to display" shows again, try: <?php if($contender == ''){ echo 'No contender to display'; } //or about the same thing, choose which you like if(strlen($contender) == 0){ echo 'No contender to display'; } ?> Quote Link to comment Share on other sites More sharing options...
Xtremer360 Posted August 22, 2008 Author Share Posted August 22, 2008 Well to explain my layout let me show you the actual page itself. http://kansasoutlawwrestling.com/champions.php Quote Link to comment Share on other sites More sharing options...
jrm Posted August 22, 2008 Share Posted August 22, 2008 Try this, it works for me great: <?php error_reporting(E_ALL); // To report any errors that happen Comment out when finished with coding $Query = "Select ....."; $QueryResults = mysql_query($query, $link) or die("\n Could not run Query : " . $Query . " : " . mysql_error()); if(mysql_num_rows($QueryResults) == 0) { // run some code here } else { while($QueryRow = msyql_fetch_array($QueryResults, MYSQL_ASSOC)) { // run some code here } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.