is there a better way to combine the html code with php than the following?

0
<thead>
            <tr>
                <th>nombre</th>
                <th>apellido</th>
                <th>marca</th>
                <th>matricula</th>
            </tr>
        </thead>
        <tbody>
        <?php 
            if($blogs){
                foreach($blogs as $blog){
        ?>
            <tr>
                <td><?php echo $blog->nombre; ?></td>
                <td><?php echo $blog->apellido; ?></td>
                <td><?php echo $blog->marca; ?></td>
                <td><?php echo $blog->matricula; ?></td>
                <td>
                    <button>editar</button>
                    <button>eliminar</button>
                </td>
            </tr>
        <?php
                }
            }
        ?>
        </tbody>
    
asked by Rangel 27.09.2018 в 22:13
source

2 answers

1

What you could do to improve a bit is change the if by

<?php  if(condition): ?>
--aqui va tu codigo html

--opcional--
<?php else: ?>


<?php elseif(condition): ?>
--aqui va mas codigo html


<?php endif ?> 

You can also do it with the foreach in the same way

    
answered by 27.09.2018 / 22:53
source
0

You can get the same result like this:

<?php 
    $html = '<thead>
        <tr>
            <th>nombre</th>
            <th>apellido</th>
            <th>marca</th>
            <th>matricula</th>
        </tr>
    </thead>
    <tbody>';
    if($blogs){
        foreach($blogs as $blog){        
            $html .= '<tr>
            <td>' . $blog->nombre . '</td>
            <td>' . $blog->apellido . '</td>
            <td>' . $blog->marca . '</td>
            <td>' . $blog->matricula . '</td>
            <td>
                <button>editar</button>
                <button>eliminar</button>
            </td>
        </tr>';        
        }
    }        
    $html .= '</tbody>';
    echo $html;

This method is very effective when you have to intermix the two many times, since it saves you from opening and closing the php tag multiple times.

    
answered by 27.09.2018 в 22:19