0

What is the best practice to "echo" generated html code? I have to display a set of data through a loop. In this case, the html code has to generate inside the function apply_task(). Once the code finished, it will jump back to index.html. If $count = 0 then display "generated html code".

It works fine but debugging it is annoying. I have to jump from index.html to the function.php file see what happen then jump back to index.html to follow the code. I found this tiring!!

Do you guys have the same problem? If not, how would you do better structure?

Index.html

<?php   
$count = check_user($link, $taskid, $taskerid);
If ($count == 0){
    apply_task($link, $taskid);
}else{
    echo "you can't add yourself to your own work";
    echo "<br>";
};
?> 

function.php

function apply_task($link, $task_id){//change name
//echo $session[$id] = $id;
$sql = "select * from wuuk where id = '$task_id';";
$result = mysql_query($sql, $link);

$row = mysql_fetch_array($result, MYSQL_NUM);
echo "<br>";
echo "<h1>Workk information:</h1>";
echo "<table class='apply_table'>";
echo "<tr class='blank_row'><td colspan='2'></td></tr>";

echo "<tr><td>Name:</td><td>$row[2]</td></tr>";
echo "<tr><td>Price:</td><td>$row[4]</td></tr>";

echo "</table>";
echo "<br>";

};

4 Answers 4

3

Always make sure to follow MVC in your application. Keep HTML separate from PHP functions. Here is how you can render html in index.html

<?php $row = apply_task($link, $taskid); ?>
    <?php if($row !== false) { ?>
        <h1>Workk information:</h1>
        <table class='apply_table'>
        <tr class='blank_row'><td colspan='2'></td></tr>

        <tr><td>Name:</td><td>$row[2]</td></tr>
        <tr><td>Price:</td><td>$row[4]</td></tr>

        </table>
        <br>
    <?php } else { ?>
        <p>you can't add yourself to your own work</p>
<?php } ?>

update function.php as bellow:

function apply_task($link, $task_id){
    $sql = "select * from wuuk where id = '$task_id';";
    $result = mysql_query($sql, $link);

    $row = mysql_fetch_array($result, MYSQL_NUM);
    $count = check_user($link, $taskid, $taskerid);
    if($count == 0) {
        return $row;
    } else {
        return false;
    }
}
0

It's a bad thing to show your content with multiple echo.

If you want to show various lines of content, use one echo.

function apply_task($link, $task_id) {

    //change name
    //echo $session[$id] = $id;

    $sql = "SELECT
                *
            FROM
                wuuk 
            WHERE
                id = '".$task_id."'";

    $result = mysql_query($sql, $link);

    $row = mysql_fetch_array($result, MYSQL_NUM);

    echo "  <br>
            <h1>Workk information:</h1>
            <table class='apply_table'>
                <tr class='blank_row'>
                    <td colspan='2'></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td>".$row[2]."</td>
                </tr>
                <tr>
                    <td>Price:</td>
                    <td>".$row[4]."</td>
                </tr>
            </table>
            <br>";

}
1
  • 1
    or just stringify it then echo Dec 2, 2015 at 8:21
0
<table>
<tr>
 <th></th>
 <th></th>
 <th></th>
</tr>
<?php 
$query = "select * from table_name";
$res = mysql_query($query);
while($data = mysql_fetch_array($res) ){ ?>
<tr>
 <td><?= $data['column_name'] ?></td>
</tr>

<?php }
?>
</table>
0

As Zafar said it's always good to keep the concerns of presentation and logic separate where possible. I think with any modern site that isn't static, the best way to handle this is through templates. This will allow you to prepare all the data in the back end, pass the data to the page and then you can work with that data in a view. Using your code as an example, assuming you have a get_tasks() function to return your tasks could look like this with Twig as your template engine:

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader);

echo $twig->render('index.html', array('rows' => get_tasks()));

index.html

{% if rows %}
    <br>
    <h1>Workk information:</h1>
    <table class='apply_table'>
    <tr class='blank_row'><td colspan='2'></td></tr>

    <tr><td>Name:</td><td>$row[2]</td></tr>
    <tr><td>Price:</td><td>$row[4]</td></tr>

    </table>
    <br>
{% else %}
    you can't add yourself to your own work
    <br>
{% endif %}

For more information on templates and why we use them see the documentation at PHP the right way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.