Php formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barffy
Forum Newbie
Posts: 11
Joined: Mon Aug 30, 2010 6:15 am

Php formatting

Post by barffy »

Hi,

Im trying to learn PHP at the minute and have hit a problem. I am able to contact my database, add records to it and display the contents of it using the echo function but i have since added some html to try and output the information formatted nicley into a table but to no avail. All that shows up in the browser are the column headings and the page title. i would be extremly grateful if somebody could enlighten me as to what i am doing wrong, which will be something very simple and embarassing im sure!

regards

Barffy



<?php
$con = mysql_connect("localhost","root","root");
if (!$con)

{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);


$query="SELECT * FROM contacts ";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();


echo "<b><center>Database Output</center></b><br><br>";

?>


<table border ="0" cellspacing ="2" cellpadding ="1">
<tr>
<th><font face ="Verdana, Arial, sans-serif">Name</font></th>
<th><font face ="verdana, Arial, sans-serif">Phone</font></th>
<th><font face ="Verdana, Arial, sans-serif">Mobile</font></th>
<th><font face ="Verdana, Arial, sans-serif">Fax</font></th>
<th><font face ="Verdana, Arial, sans-serif">Email</font></th>
<th><font face ="Verdana, Arial, sans-serif">Web</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

?>



<tr>
<td><font face ="Verdana, Arial, sans-serif"><?echo $first."".$last; ?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $phone; ?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $Mobile; ?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $fax; ?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $fax;?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $email;?></font></td>
<td><font face ="Verdana, Arial, sans-serif"><?echo $web;?></font></td>
</tr>




<?php
$i++;
}

?>
User avatar
iijb
Forum Commoner
Posts: 43
Joined: Wed Nov 26, 2008 11:34 pm

Re: Php formatting

Post by iijb »

Hi,
I think the short tags cause you worry. Instead of <?...?> use <?php echo $first."".$last; ?>. And also notice there is a space between '<?php' and 'echo' statement.

Also should not use close mysql
ie, mysql_close(); after the $num=mysql_num_rows($result); line. And there is a underscore symbol missing in your mysql_numrows();

Regards
iijb
barffy
Forum Newbie
Posts: 11
Joined: Mon Aug 30, 2010 6:15 am

Re: Php formatting

Post by barffy »

You Sir, are a genius!

Thanks for your help. As im still learning i knew it would be something simple!

Thanks again

Barffy
Post Reply