If Then Else - Help

hi

I have the following code which works well

echo "<center>";
echo "<table border='1'>";

echo "<tr>
	<th>S\N Ratio</th>
	<th>Doppler Estimate</th>
	<th>Doppler Hz</th>
	<th>Image</th>
</tr>";
    while($row = $result->fetch_assoc()) {

echo "<tr>";
echo "

<td>{$row['snratio']}</td>
<td>{$row['adjfreq']}</td>
<td>{$row['doppler_Hz']}</td>
<td><img src='/mimages/{$row['image']}' alt='HTML5 Icon' style='width:100px;height:100px;'></td>";

and so on

Sometimes the {$row[‘image’]} has an image and at other times it may be empty

I would like to wrap an if then else statement so if its empty it will say No Image available, i could not find an example so pointers really appreciated.

John B

Assuming the rows where there is no image contain a NULL value in that column a simple if will do it after the opening <td> for the image cell because the empty cell must still exist to maintain table structure.

if($row['image']){ echo '<img src="mimages/'.$row['image'].'" alt="HTML5 Icon" width="100" height="100"'>' ;}
echo '</td>';

[off-topic]

C’mon, really?

sorry am i missing something - I have included the code - am new to PHP and am looking for pointers

john B

I was missing that bit:-

if($row['image']){ echo '<img src="mimages/'.$row['image'].'" alt="HTML5 Icon" width="100" height="100"'>' ;}
else{ echo "No Image available" ;}
echo '</td>';

thank you - I will add this to my list of tips for future reference

Sorry but im have syntax error issues ?
John

What error message exactly?

I get an error 500 - entire code posted below

<?php
$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";

// Create connection
$conn1 = new mysqli($servername, $username, $password, $dbname);
$sql1 = "SELECT date FROM duration_matched_test WHERE groupm = 486";
$f = $conn1->query($sql1);
$row = $f->fetch_assoc();

echo "<center><h2>Consolidated Coincidence Report for "; 
echo $row["date"];
echo "</H2>";
echo "<h3>Radio Meteor Events</H3><br></center>";
$conn1->close();


///

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM duration_matched_test WHERE groupm = 486";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row




echo "<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
</style>";



echo "<center>";
echo "<table border='1'>";

echo "<tr>
	<th>Event ID</th>
	<th>Contributor</th>
	<th>RX Lat</th>
	<th>RX Lng</th>
	<th>TX Source</th>
	<th>Time</th>
	<th>Duration in Seconds</th>
	<th>S\N Ratio</th>
	<th>Doppler Estimate</th>
	<th>Doppler Hz</th>
	<th>Image</th>
</tr>";
    while($row = $result->fetch_assoc()) {



echo "<tr>";
echo "
<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>if{($row['image'])}{ echo '<img src="mimages/'.$row['image'].'" alt="HTML5 Icon" width="100" height="100"'>' ;}
else{ echo "No Image available" ;}
echo '</td>';



echo "</tr>";
}
echo "</center>";
echo "</table>";

//}
} else {
    echo "Query didn't return any result";
}

$conn->close();
?>

I see the problem. My code should start after the <td> for the image is opened. You need to end that echo and start the if in a new php command line.

echo "...
stuff...
...
<td>{$row['doppler_Hz']}</td>
<td>"; // The previous echo is terminated prior to the if condition
if{($row['image'])}{ echo '<img src="mimages/'.$row['image'].'" alt="HTML5 Icon" width="100" height="100"'>' ;}
else{ echo "No Image available" ;}
echo '</td>';

sorry still a 500 error

That’s a server error. When you said “syntax error” I assumed a PHP generated error which comes with a message and can be diagnosed.

Did you get these errors before making these modifications to the script?

sorry no the original is fine

Original - http://meteor.m81.co.uk/testing.php

Modded - http://meteor.m81.co.uk/test.php

The broken page does not have any clues for me.
Chrome just says:-

This page isn’t working
meteor.m81.co.uk is currently unable to handle this request.
HTTP ERROR 500

And Firefox just shows a blank page.

Do you have error reporting enabled in PHP?
That is essential for debugging.

I also recommend you brush-up/update your HTML skills before going further into PHP.
Browsers are very forgiving of invalid html, but the document going straight into obsolete elements, before any doctype declaration, head or body tags is not going to help things. I’m not sure if that can affect headers, but either way, it’s not good.

understood, thanks for your patience and help

I have installed editplus which is helpful I can see at which point the code goes green so am narrowing down the issue which is syntax related - I will let you know what I did to solve it. Thanks again

Are the single quotes around the closing > required?

alt="HTML5 Icon" width="100" height="100"'>' ;}

Surely just the one after it?

1 Like

Well spotted. I must have left a quote in when I edited the img element to use html attributes for height and width instead of in-line styling.

Error reporting should have shown that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.