Syystole Syystole - 7 months ago 9
PHP Question

Echo PHP in HTML

I'm trying to show my PHP in a div in the HTML section of my web page,
here is the PHP

$result = mysqli_query($connect,"SELECT * FROM adopter");
if(mysqli_num_rows($result)>0){
while($row=mysqli_fetch_assoc($result)){
echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';


I would like this to be displayed in my div in the HTML since it is always displaying this information at the top literally above my page instead of the blank space I want in my website design.

<div class="title">
</div>
<title> PHP UPDATE DATA </title>
<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">



</head>

</div>
</div>


I'm trying to put it into this section of the DIV in HTML

Answer

There is a problem about the understanding of the timing between the actions.

PHP is a server side language, so it will be processed ALWAYS before the client side language (likehtml and css)

so, if you cast echo is normal that PHP will put it on the top of the page (because is processed first, so printed first)

The way to do that is to assign the output to a variable and echo that variable is the point you need.

So instead of

echo 'AdopterID: '. $row['AdopterID'] . '<br>';
echo 'Name: '. $row['Name'] . '<br>';
echo 'Address: '. $row['Address'] . '<br>';
echo 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br> <br>';

You should

$myvar = "";
while (youcode) {
    $myvar .= 'AdopterID: '. $row['AdopterID'] . '<br>';
    $myvar .= 'Name: '. $row['Name'] . '<br>';
    $myvar .= 'Address: '. $row['Address'] . '<br>';
    $myvar .= 'TelephoneNumber: '. $row['TelephoneNumber'] . '<br><br>';
}

than in some point you will have your HTML:

<div>
    <?php echo $myvar; ?>
</div>

i hope i was clear enough

Comments