How to style PHP echo output with span

前端 未结 4 583
傲寒
傲寒 2021-01-23 16:32

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

Ideally I\'d like to use for each echo, but I\'m not too sure how to ach

相关标签:
4条回答
  • 2021-01-23 16:43

    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.

    0 讨论(0)
  • 2021-01-23 16:44

    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>';
    
    0 讨论(0)
  • 2021-01-23 16:48
    $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.

    0 讨论(0)
  • 2021-01-23 16:52

    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);
    
    0 讨论(0)
提交回复
热议问题