Help Formatting PHP MySql output

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
honkmaster
Forum Newbie
Posts: 12
Joined: Mon Feb 15, 2010 12:10 pm

Help Formatting PHP MySql output

Post by honkmaster »

Hi New to PHP and MySql. I have built the following PHP from a few different places. The script outputs all entries in my database to a table with headers. What I would like to do is format the output to make it look nice. This is where i need some guidance.

I would like the table Headers to be white text and black background and the date in the table to be 10 point Verdana with a rule between each entry.

I have pasted the code below. Any help would be great.

Cheers Chris

<?
$username="mydetails";
$password="fmydetails";
$database="mydetails";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM cbs WHERE name='$_POST[name3]'";
$result=mysql_query($query);
if (($result)||(mysql_errno == 0))
{
echo "<table width='100%'><tr>";
if (mysql_num_rows($result)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($result))
{
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";

//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center' >". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>Sorry No Results found!</td></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Help Formatting PHP MySql output

Post by rahulzatakia »

Hi,

I have make the chances in the code as you required..
And for date format you need to print each value in different <td> and in the <td> of date you can give class as i have given in <tr> of field names..






<style type="text/css">

.fontcolor{
color:#FFFFFF;
background-color:#000000; }

.datefont{
font-family:Verdana;
font-size:10px; }

</style>

<?
$username="mydetails";
$password="fmydetails";
$database="mydetails";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM cbs WHERE name='$_POST[name3]'";
$result=mysql_query($query);
if (($result)||(mysql_errno == 0))
{
echo "<table width='100%'><tr class="fontcolor">";
if (mysql_num_rows($result)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($result))
{
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";

//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center' >". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>Sorry No Results found!</td></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Help Formatting PHP MySql output

Post by Weiry »

Both of you need to read the Announcements in the forum..
especially THIS one:

Posting Code in the Forums
Post Reply