2

I am learning php and made a simple application to save peoples names to a database then pull all the saved names into a list lower down on the page.

this is the code that is getting the names: echo "<div class=\"results\"> $row[Name] </div>";

I put a div with the class of results so that I could style it. At this point all I wanted to do was center the results. After styling it with css to be centered, looking at the page source each name that was echo'ed has a div around it with the class of results. They are all centered like I wanted which is good but this does not seem like the best way to do it because there will be a new div created for every new name in the database (right now there are 18 divs. Way too many!).I was hoping only one div would be created but I was obviously wrong about that.

So, is it possible to pre-create a div in the html markup with nothing in and echo the names into the div? Or do you think there would be a way to make it all output at once so it was all within the one div?

Here is the rest of the code so you get an idea of whats happening:

<?php 
$connect = mysql_connect("localhost","user","0123");
if(!$connect){
    die("Failed to connect: " . mysql_error());
}

if(!mysql_select_db("Test")) {
    die("Failed to select database: " . mysql_error());
}

$results = mysql_query("SELECT * FROM Friends");

while($row = mysql_fetch_array($results)){
    echo "<div class=\"results\"> $row[Name] </div>";
}

if ($_POST[name] !="") { $sql="INSERT INTO Friends 
(name) VALUES('$_POST[name]')";

if (!mysql_query($sql,$connect))
  {
  die('Error: ' . mysql_error());
  }
echo "<div id=\"added\"> $_POST[name] added to the database </div>";
}
else die("")

?>

4 Answers 4

4

Since you output the div inside your while loop, you will get a new div for each result. Just move the div outside the loop:

echo "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
    echo $row['Name'];
}
echo "</div>";

Note that you should always quote your keys. Use $row['Name'] instead of $row[Name] (obviously not if Name is a defined constant).

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thanks. Exactly what I was looking for. Also with the name being in inverted commas $row['Name'] When I had that to start with the page would not load (there was a server error) but without them it loaded fine. Now it works with them and I am thinking that is because your code does not have the html markup in the same statement. Would this be correct and if it is do you know why this happens?
2

i think it's better to have some variable to store the output:

$out .= "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
  $out .= $row[Name]."<br />";
}
$out .= "</div>";

echo $out;

3 Comments

>some variable to store the output. Why?
@Boann in that case its much easier to print everyting in right order. eg: if you need to put something in the beginning of the output, but this something can be calculated only at the end of the script. some "total" value etc.
Also an interesting way to get around it thanks. I will also try this way out.
2

Don't let php output html tags. It looks bad in code and your editor will not recognise it as html and make a big mess in colouring it. Generally I would advise to do all your php processing first and then make the HTML with pieces of php output in it. Something like this:

Note that I replaced your div with an ul, as your list of persons seems better suited as an ul than a div. Obviously you can use any tag you'd like in the HTML

<?php
 // do all php stuff here
 $persons = /* get collection from database here */
?>

<html>
 <head>
 </head>
 <body>
  <ul id="ListOfPersons">
   <? foreach ($persons as $person): ?>
    <li>
      <?= $person["Name"] ?>
    </li>
   <? endforeach ?>
  </ul>
 </body>
</html>

1 Comment

Thanks I will try this after messing around with what the first guy said. This like a good option as I did want a list of the names.
0

You can separate the div creation from the results echoing :

echo '<div class="results">';
while($row = mysql_fetch_array($results)){
    echo $row['Name'].'<br/>';
}
echo '</div>';

and if you don't want the div if there are no results (no friends) you can use mysql_num_rows

3 Comments

maybe use echo $row['Name'].'<br>'; to add line breaks after each result
@BillyMoon I missed that. Thanks.
Thanks! I will definitely put that into my app so it says "The list is empty" or something similar when there are 0 rows in the table.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.