Jump to content

If then Else help please


johnber

Recommended Posts

Hi

 

Im quite new to php but have managed to create a page which reads records from a mysql database and present them in a table

 

one of the fields in each record is an image filename and I can get it to display the image - not every record has an image and so currently it will just show a broken image so I assume and if then else statement will fix this as if there is no image i can say No Image Available

 

my code looks like this

 

echo "<tr>
<td>{$row['event_id']}</td>
<td>{$row['user_ID']}</td>
<td>{$row['Lat']}</td>
<td>{$row['long']}</td>
<td>{$row['source']}</td>
<td>{$row['Time']}</td>
<td>{$row['Duration']}</td>
<td>{$row['snratio']}</td>
<td>{$row['adjfreq']}</td>
<td>{$row['doppler_Hz']}</td>
<td><a href='/mimages/{$row['image']}' target='_blank'><img src='/mimages/{$row['image']}' width='100' height='100'></a></td>";
echo "</tr>";
 
so its all around the last last line - pointers really appreciated
 
John

 

 

Link to comment
Share on other sites

You can't put an if/else inside a string so that means you have to end the one you've built up early.

echo "<tr>
<td>{$row['event_id']}</td>
<td>{$row['user_ID']}</td>
<td>{$row['Lat']}</td>
<td>{$row['long']}</td>
<td>{$row['source']}</td>
<td>{$row['Time']}</td>
<td>{$row['Duration']}</td>
<td>{$row['snratio']}</td>
<td>{$row['adjfreq']}</td>
<td>{$row['doppler_Hz']}</td>
<td>";
Then add in an if/else to output the image if one is present. Tip: you don't need an else.

 

Then finish the row.

echo "</td></tr>";
Link to comment
Share on other sites

im close after getting a few syntax issues I now have this

 

<td>{$row['doppler_Hz']}</td>";
 
echo "<td>";
if (is_null.$row['image'])
         echo "No data!";
     else
         echo "<td>{$row['image']}</td>";
echo "</td></tr>";
 
However it shows No data even if there is data, so this statement
 
 if (is_null.$row['image']) is wrong and its probably my understanding of null \ empty ?
 
John B
 
 

Link to comment
Share on other sites

is_null is a function like any other so you need to use it as such.

 

And apparently you don't have your environment set up properly for development. Find your php.ini, find and change two settings

display_errors = on
error_reporting = -1
then restart Apache/nginx. Try your code as it is now without the is_null fix, see what PHP says, then make the change.
Link to comment
Share on other sites

learning a lot so error reporting now on and after a few reported syntax errors which I fixed I have

echo "<td>";
if ($row['image'])
    echo "{$row['image']}</td>";
else
    echo "No data!";
echo "</td></tr>";
 
which works a treat - thanks for all the help. John
Link to comment
Share on other sites

I know people love to use the nuances of PHP's true/false capabilities.  But - especially since you are new - try and write Meaningful Code instead of these things you have to think about when seeing them.

 

 

if ($row['image'] == '')

 

is much more direct than the line you copied from somewhere.  Or even use the is_null function that you mis-used earlier.  Lines like that are so much clearer when reviewing lots of code and not having to stop and think about them.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.