Formatting Table for 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
digdigdig
Forum Newbie
Posts: 20
Joined: Thu Jun 23, 2011 11:10 am
Location: South Florida

Formatting Table for Output

Post by digdigdig »

Hello!

I am having a bit of trouble formatting my table that features my query results. Any formatting i include seems to be ignored.

Code: Select all

//build query 
$result = mysql_query("SELECT * FROM Centers where City like '$City'")
or die ('cannot select table:' .msql_error());


echo "<table border=\"1\" width=\"620\" >";
echo "<tr><th width=\"50\">Name</th>";
echo "<th width=\"50\">Address</th>";
echo "<th>Phone</th>";
echo "<th width=\"150\" class=\"wraptext\" >Email</th>";
echo "<th width=\"150\" class=\"wraptext\" >Website</th></tr>";


//display results
while ($row =mysql_fetch_array($result)){
	
	
		
	echo "<tr><td>";
	echo $row['Name'];
	echo "</td><td>";
	echo $row['Address'];
	echo "</td><td>";
	echo $row['Phone'];
	echo "</td><td>";
	echo $row['Email'];
	echo "</td><td>";
	echo $row['Website'];
	echo "</td></tr>";
	}
	echo "</table>";

?>			
Would appreciate any input. Thanks!!
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Formatting Table for Output

Post by phphelpme »

What exactly is your problem here as the above code works fine on my server?

Is there any other code that could be causing it that you have not displayed here?

What exactly do you see when you run the code?

Here is what I ran on my server:

Code: Select all

<?php
$host		=	'*****************';
$dbuser		=	'*************';
$dbpass		=	'****************';
$dbdatabase	=	'***********';

//This is used to connect to the MySQL Database
function database_connect()
	{
	global $host, $dbuser, $dbpass, $dbdatabase;
	mysql_connect($host,$dbuser,$dbpass) OR DIE ("Unable to connect to database");
	mysql_select_db($dbdatabase) OR DIE ("Unable To select database $dbdatabase within $host");
};

function database_close()
	{
	mysql_close();
};

database_connect();

$City = "**********";

//build query 
$result = mysql_query("SELECT * FROM Centers where City like '$City'")
or die ('cannot select table:' .msql_error());

echo "<table border=\"1\" width=\"620\" >";
echo "<tr><th width=\"50\">Name</th>";
echo "<th width=\"50\">Address</th>";
echo "<th>Phone</th>";
echo "<th width=\"150\" class=\"wraptext\" >Email</th>";
echo "<th width=\"150\" class=\"wraptext\" >Website</th></tr>";

//display results
while ($row =mysql_fetch_array($result)){
                      
        echo "<tr><td>";
        echo $row['Name'];
        echo "</td><td>";
        echo $row['Address'];
        echo "</td><td>";
        echo $row['Phone'];
        echo "</td><td>";
        echo $row['Email'];
        echo "</td><td>";
        echo $row['Website'];
        echo "</td></tr>";
        }
        echo "</table>";
?>         
This gives me the table formating that you have shown above.

Best wishes
Last edited by phphelpme on Wed Aug 17, 2011 10:25 am, edited 1 time in total.
digdigdig
Forum Newbie
Posts: 20
Joined: Thu Jun 23, 2011 11:10 am
Location: South Florida

Re: Formatting Table for Output

Post by digdigdig »

Thanks for your reply.

The table does not seem to format correctly. The last two fields (Email and Website) do not conform to the width instructions. Is the width information in the correct place?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Formatting Table for Output

Post by phphelpme »

That might be because you are inserting an email address and website address which have no spaces so the table can not wordwrap the content which means the table field width will become the width of the content so you see it all.

Try this and you will see:

Code: Select all

echo "<th width=\"50\" class=\"wraptext\" >Email</th>";
echo "<th width=\"50\" class=\"wraptext\" >Website</th></tr>";
I reduce the width of both but they will become with width of your content.

Best wishes
digdigdig
Forum Newbie
Posts: 20
Joined: Thu Jun 23, 2011 11:10 am
Location: South Florida

Re: Formatting Table for Output

Post by digdigdig »

Thanks for the reply.

Yes, i can see that the Email and Website data have no spaces and cannot wrap. Even including \"wraptext\" Is there a solution for this that will make the data wrap?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Formatting Table for Output

Post by phphelpme »

As an alternative, why not use the word-wrap css, for example:

Code: Select all

<p style="width: 5em; word-wrap: break-word">longemailaddress@longemailaddress.com</p>
Just an idea thats all.

Otherwise your stuck with a deformed layout.

Another option would be to show an email address name and its a link to the email address maybe.

Best wishes
Post Reply