How to style PHP output?

How to style PHP output?

  • 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("") ?>

  • Answer:

    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).

cohen at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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;

k102

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>

Bazzz

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 http://php.net/manual/en/function.mysql-num-rows.php

Nacereddine

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.