Forum Moderators: coopster

Message Too Old, No Replies

PHP question about ignoring empty database table

         

DavidA

6:43 am on Apr 25, 2005 (gmt 0)

10+ Year Member



Hello,

I am just learning to use php and welecome any comments on my first attempt. The following is a table which uses css and also accesses a mysql database. It appears to work correctly, except for one issue:

I need to know how to properly write an "if" statement so that the mysql data base is checked for data and ignores the following request for a picture, otherwise returns the listed image.

<img src='images/pics/$datas[pics]' width='90' height='90'> correctly pulls the image, but also return a blank box if the data table is empty.

Thank you in advance for any help.

$cn .= "<table width='610' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td><img src='/images/foundation/shim.gif' width='10' height='1'>
<td class='vendorname_left_c'><img src='/images/foundation/shim.gif' width='100' height='1'></td>
<td colspan='2' class='vendorname_middle_c'><div class='vendorname'><a href ='$datas[url]'>$datas[title]</a></div></td>
<td class='vendorname_right_c' width='256'>&nbsp;</td></tr>
<tr valign='top'>
<td width='14'><img src='/images/foundation/shim.gif' height='10' width='1'></td>
<td width='100' valign='bottom' class='vendor_thumbnail_c'><img src='images/pics/$datas[pics]' width='90' height='90'></td>
<td class='vendor_details_left_c' width='218'>
<div class='local_vendor_city'>$datas[city],&nbsp;$state</div>
<div class='local_vendor_address'>$datas[street]</div>
<div class='local_vendor_tel'>$datas[phone]<span class='vendor_tel'> </span></div>

<div class='vendor_url'><a href ='$datas[url]'><img src='/images/foundation/website_link.gif' alt='Website' width='70' height='20' border='0'></a></div>
&nbsp;&nbsp;&nbsp;
<img src='/images/foundation/shim.gif' height='10' width='1'></td>
<td colspan='2' class='vendor_details_right_c'>$datas[description]</td></tr>
</table><br>";

jatar_k

4:53 pm on Apr 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld DavidA,

well let's see then, I only have this to go on

<img src='images/pics/$datas[pics]' width='90' height='90'>

so I assume you are pulling data from the db into an array $datas, we would need to go back a step. There are a couple ways to do this but using mysql_num_rows [php.net] is probably the most straight forward.

you would execute your query using mysql_query then run the returned result resource through mysql_num_rows, check how many rows were returned and if it is equal to 0 or less then you can skip over the whole image output.

DavidA

5:20 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Sorry, I guess I should have been clearer.

This in an optional field. All the other fields are mandatory, but the [pics] field is optional. Right now, if nothing is in the pics field, it returns an empty image box.

How do I check this field and ignore it if there is no entry? A example to give me an idea would be great as I am just a newbie.

<img src='images/pics/$datas[pics]' width='90' height='90'>

This is what preceeds my table.

$rst = mysql_query($sq,$db) or die(mysql_error());
$datas = mysql_fetch_array($rst);

$cn = "<h2 align='center'> " .str_replace("-"," ",$state). ("&nbsp;") .str_replace("-"," ",$category). "</h2><p>";

if(mysql_numrows($rst) >=1){

do{
$state = str_replace ("-", " ", "$state");
$cn .= "<table width='610' border='0' cellspacing='0' cellpadding='0'>

jatar_k

6:08 pm on Apr 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, ok

then I assume $datas[pics] will be empty or it will have a string/filename in there

try this one

if (!empty($datas[pics])) {
echo "<img src='images/pics/" . $datas[pics] . "' width='90' height='90'>";
} else {
echo '&nbsp;';
}

something like that you mean?