Page 1 of 1

Formatting Table for Output

Posted: Wed Aug 17, 2011 9:09 am
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!!

Re: Formatting Table for Output

Posted: Wed Aug 17, 2011 10:22 am
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

Re: Formatting Table for Output

Posted: Wed Aug 17, 2011 10:24 am
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?

Re: Formatting Table for Output

Posted: Wed Aug 17, 2011 10:32 am
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

Re: Formatting Table for Output

Posted: Wed Aug 17, 2011 11:31 am
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?

Re: Formatting Table for Output

Posted: Wed Aug 17, 2011 11:42 am
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