-1

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

Syystole
  • 5
  • 2
  • I don't see a `
    ` nor do I see the *blank space* you're referring to. You need to post all relevant code and better describe your problem with examples.
    – mferly May 02 '16 at 15:24
  • So what's not working as expected? – WillardSolutions May 02 '16 at 15:24
  • Instead of echoing while you're iterating through your database results, simply concatenate to a string. `$html .= 'AdopterID: ' . $row['AdopterID'] . '
    ';` and then echo out your HTML wherever you want.
    – David Wyly May 02 '16 at 15:26
  • You probably have to allow PHP to be run in HTML files. You can do this by most likely adding this into your `.htaccess` files. [Add PHP to HTML](http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-file-to-html-html-files) – lovermanthing May 02 '16 at 15:26
  • @lovermanthing _"...since it is always displaying this information at the top literally above my page..."_ - The code is executing fine. It's just not output to the desired location within the page. – War10ck May 02 '16 at 15:28
  • Sorry I'm new at this, updated the post to where I would like it to be in my HTML section – Syystole May 02 '16 at 15:28
  • 2
    You can't be sticking a `
    ` inside the ``. That's why it's outputting "*literally at the top of the page*".
    – mferly May 02 '16 at 15:29
  • You probably want to http GET your php data and append that data in your div ... – Joum May 02 '16 at 15:29
  • @Joum Can you elaborate on this?... – War10ck May 02 '16 at 15:31
  • @War10ck instead of `echo`ing with PHP, OP could instead use AJAX and simply have a php file server side which returns, ie, JSON and then place that JSON in his HTML asynchronously... Example here: http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php – Joum May 02 '16 at 15:35
  • But now that I see what OP is trying to do, I'm not sure if this might help or might confuse OP even more... just my 2 cents, anyway... – Joum May 02 '16 at 15:36

2 Answers2

2

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

Gumma Mocciaro
  • 1,135
  • 8
  • 22
  • Thanks for this, I just tried it and it does indeed place it in the desired area, but it only displays one record from my database instead of all of them? – Syystole May 02 '16 at 15:32
  • 1
    i think you added the ```$myvar = "";``` inside the ```while```, so everytime the while starts a new cycle, it will override the variable. Just put it before the ```while``` statement ```$myvar = "";``` ````while($row=mysqli_fetch_assoc($result)){```` (i edited the main thread) – Gumma Mocciaro May 02 '16 at 15:34
1

This is what you want. You need to place your output within the <body> for it to display as intended.

<!doctype html>
<html>
    <head>
        <title> PHP UPDATE DATA </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div class="title">
            <?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>';
                }
            }
            ?>
        </div>
    </body>
</html>
mferly
  • 1,586
  • 1
  • 11
  • 18