1

I'm trying to style the output of each echo.

Ideally I'd like to use <span class=""> </span> for each echo, but I'm not too sure how to achieve this.

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
    echo $row['Date'];
    echo $row['Title'];
    echo $row['Message'];
    echo "<img src='".$row['Image']."'/>";
}

mysql_close($con);
Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
BigOrinj
  • 243
  • 2
  • 6
  • 14

4 Answers4

5
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
    {
    echo "<span class=\"myclass\">$row['Date']</span>";
    echo "<span class=\"myclass\">$row['Title']</span>";
    echo "<span class=\"myclass\">$row['Message']</span>";
    echo "<img src='".$row['Image']."'/>";
    }

mysql_close($con);

or, much nicer, in a table:

$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result))  {
    echo "<tr>"
    echo "<td>$row['Date']</td>";
    echo "<td>$row['Title']</td>";
    echo "<td>$row['Message']</td>";
    echo "<td><img src='".$row['Image']."'/></td>";
    echo "</tr>"
}
echo "</table>"

mysql_close($con);

You then can style each row and column with a class.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
4

Try this:

$prepend = "<span class=''>";
$append  = "</span>";

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
    {
    echo $prepend.$row['Date'].$append;
    echo $prepend.$row['Title'].$append;
    echo $prepend. $row['Message'].$append;
    echo $prepend."<img src='".$row['Image']."'/>".$append;
    }

mysql_close($con);
John V.
  • 4,652
  • 4
  • 26
  • 26
3

I'd create a function that does this:

function decorated_echo($text) {
    echo '<span class="myclass">' . $text . '</span>';
}

This way, you don't have to repeat this everytime you want this behaviour.

Ikke
  • 99,403
  • 23
  • 97
  • 120
1

You are guessing right, just add required html in the echo:

echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';

or you can just put inline style if no css file is loaded:

echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';
luigif
  • 544
  • 4
  • 8