I have stupid problem with the html/php rules. I'm trying to show an image from an apache
server with this code using a table:
<?php
//code
while($row = mysqli_fetch_array($result)) {
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src=\"<?php echo $url; ?>\"/></td>
</tr>';
}
//code
?>
But obviusly the inner php script is considered as normal text and no run!
Don't echo large blocks of HTML in PHP like that. Its bad practice. No, actually, its horrible practice. Instead learn to open and close the PHP tag as needed, like:
<?php
//code..code...code...
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row['x']; ?> </td>
<td> <?php echo $row['y']; ?> </td>
<td> <?php echo $row['z']; ?> </td>
<td> <?php echo $row['f']; ?> </td>
<td> <?php echo $row['g']; ?> </td>
<td> <?php echo $row['d']; ?> </td>
<td><img src="<?php echo $url; ?>"/></td>
</tr>
<?php
}
//code..code...code
?>
There are several benefits to this, including that its less likely to break syntax highlighting and your code is not defaced with as many \"
all over the place.
Just change this:
<td><img src=\"<?php echo $url; ?>\"/></td>
to:
<td><img src="' . $url .'"/></td>
You're already inside PHP - you shouldn't open another <?php
scope:
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="' .$url . '"/></td>
</tr>';
Try it:
<?php
while($row = mysqli_fetch_array($result)){
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="'.$url.'"/></td>
</tr>';
}
?>
It just a problem in concatenation, you need just combine those strings, not set another 'echo script'.
you can simply close the php section and put plain html, then reopen php when needed like this :
<?php
$test = array( "a test","also","a","test");
$itteraror = 0;
$url = "#";
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
</head>
<body>
<table>
<?php
while($itteraror<sizeof($test)) {
?>
<tr>
<td> <?php echo $test[$itteraror] ; ?></td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td><img src="<?php echo $url ; ?>" alt="my image"></td>
</tr>
<?php
$itteraror++;
}
?>
</table>
</body>
</html>
i did make some modification to your code in order for it to be standalone for testing purpose.