do While inside a variable

1

I'm creating a search system where the user will only place the month and year that they want to search. but I'm not getting the code is this

$output='<h2 class="text-center mb-4" style="color:red">Elementos</h2>
        <div class="table-responsive">          
            <table class="table">
                <thead>
                    <tr class="jumbotron">
                        <th>Nº Interno</th>
                        <th>Nome</th>
                        <th>SV</th>
                    </tr>
                </thead>
                '.do{.' 
                    <tbody style="border-bottom:2px solid red;">
                        <td>'.$row_elementos['N Interno'].'</td>
                        <td>'.$row_elementos['Nome'].'</td>
                        <td>'.$total.'</td>
                    </tbody>'.
                }while ($row_elementos = mysql_fetch_assoc($elementos)).'
            </table>
        </div>';

Then print the base of the research done below with the following code

<?php print("$output");?>

the errors are in {do (syntax error, unexpected 'do' (T_DO))} and not {while (syntax error, unexpected '}'}. How do I fix the errors?

    
asked by anonymous 24.01.2018 / 12:18

2 answers

2

Try to concatenate by parts do not mix everything in a cake. Example:

$output='<h2 class="text-center mb-4" style="color:red">Elementos</h2>
    <div class="table-responsive">          
        <table class="table">
            <thead>
                <tr class="jumbotron">
                    <th>Nº Interno</th>
                    <th>Nome</th>
                    <th>SV</th>
                </tr>
            </thead><tbody style="border-bottom:2px solid red;">';
            do{ 
                $output .= '<td>'.$row_elementos['N Interno'].'</td>
                    <td>'.$row_elementos['Nome'].'</td>
                    <td>'.$total.'</td>';
            }while ($row_elementos = mysql_fetch_assoc($elementos));
$output .= '</tbody></table></div>';
    
24.01.2018 / 12:25
1

First close the string, you can not concatenate it with a repeat loop. Use a while convecional if you use do-while in case the query returns empty some undefined index will be generated because they do not exist and because the verification is done only at the end of the loop and not at the beginning.

$output='<h2 class="text-center mb-4" style="color:red">Elementos</h2>
        <div class="table-responsive">          
            <table class="table">
                <thead>
                    <tr class="jumbotron">
                        <th>Nº Interno</th>
                        <th>Nome</th>
                        <th>SV</th>
                    </tr>
                </thead>';

                while ($row_elementos = mysql_fetch_assoc($elementos)){
                    $output .= '<tbody style="border-bottom:2px solid red;">
                        <td>'.$row_elementos['N Interno'].'</td>
                        <td>'.$row_elementos['Nome'].'</td>
                        <td>'.$total.'</td>
                    </tbody>';
                }   


            $output.= '</table></div>';
    
24.01.2018 / 12:26