Formating Tables

A

Anonymous

Guest
I am a newbie at PHP and I have created a form that takes input from a user and then throws that into a MySQL database, then have it print out into a table. However, when I print it out into this table its is not formated very well. For instance I have headings that are printing on 2 lines instead of 1, and data doing the same. I have included my code, its kinda messy, if anyone could help that would be greatly appreciated.

Code:
echo "<table border=2 cellpadding=15 cellspacing=0 width=100%>\n";
echo "<tr><td>Project Number</td><td>Project Name</td><td>Prepared By</td><td>Importance</td><td>Question 1<td>Question 2</td><td>Question 3</td><td>Question 4</td><td>Date Needed</td><td>Estimated Cost</td><td>Date Requested</td><td>Assigned To</td><td>Date Assigned</td><td>Assigned By</td><td>Completed By</td><td>Date Completed</td></td></tr>\n";
while ($myrow = mysql_fetch_row($result)) 
{
   printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow[0], $myrow[1], $myrow[2], $myrow[3], $myrow[4], $myrow[5], $myrow[6], $myrow[7], $myrow[8], $myrow[9], $myrow[10], $myrow[11], $myrow[12], $myrow[13], $myrow[14], $myrow[15]);
}
echo "</table>\n";

[Edited by Redcircle] Use code tags they are your friend. They won't bite.
 
using the printf is very confusting. I would reccomend using just a regular echo


use mysql_fetch_assoc() instread


Code:
echo '<tr><td>'.$myrow['project_name'].'</td><td>'.$myrow['prepared_by'].'</td><td>'.$myrow['importance'].'</td></tr>'."\n";

much easier to understand than a bunch of %s's
 
I agree 100% with Redcircle!

jahnks said:
when I print it out into this table its is not formated very well

Because maybe some of your table fields aren´t string as your passing with %s!
 
I have changed by code to reflect your suggestions, which does make it a lot easier to read. However my problem still exists. For instance the Column headers as well as the data is word-wrapping, which I do not want to occur. Here is a copy of my updated code to reflect your suggestions so far:

echo "<table border=2 cellpadding=15 cellspacing=0 width=100%>\n";
echo "<tr><td>Project Number</td><td>Project Name</td><td>Prepared By</td><td>Importance</td><td>Question 1<td>Question 2</td><td>Question 3</td><td>Question 4</td><td>Date Needed</td><td>Estimated Cost</td><td>Date Requested</td><td>Assigned To</td><td>Date Assigned</td><td>Assigned By</td><td>Completed By</td><td>Date Completed</td></td></tr>\n";

while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td><center><a href='edit.php?id=$row[project_number]'>$row[project_number]</a><dt><center><a href='delete.php?id=$row[project_number]'.'\'>Delete</a></td><td>$row[project_name]</td><td>$row[prepared_by]</td><td>$row[importance]</td><td>$row[question1]</td><td>$row[question2]</td><td>$row[question3]</td><td>$row[question4]</td><td>$row[date_needed]</td><td>$row[estimated_cost]</td><td>$row[date_requested]</td><td>$row[assigned_to]</td><td>$row[assigned_to_date]</td><td>$row[assigned_by]</td><td>$row[completed_by]</td><td>$row[date_completed]</td></tr>";
}
echo "</table>\n";

Thanks for all your help!
 
:help: Hmmm I have a question about tables too I was wondering if you guys could answer. It doesn't involve mysql though.

As gesf would know from the "Root directories" thread, since he helped me out so much in there, my basic file structure is like this:

file: Whatever

<?php include(...the top part of a file.php); ?>

<-- Content of file "Whatever" here -->

<?php include(...the bottom part of a file.php); ?>


In the topfile.php, it ends like so:

<td background="images/imagename.gif" height="530" width="530" allowtransparency="true>

(*those aren't the real dimensions btw, just a random number I picked for this thread)

So thats the ending of topfile, which means that the content of "Whatever", is gonna be inside that <td> tag (bottomfile.php starts with a "</td>" to end it).

The problem: I obviously use the "includes" in all my files, so that the pages look the same with only the content varying. My problem is that some of this content is over 530 pixels in height, which means the image gets distorted. What I need in my table (the td tag? or somewhere else?) is a scrolling or scrollbar type of code so that the table stays at 530 pixels but you can scroll up or down in it. I can do it with iframes, but I'd rather use php and everyone tells me iframes suck anyways, so I'd rather use php. Any solutions or other ideas? Thanks in advance for any help :/.
 
Hey jahnks, i´m not sure about what you want to do, anyway, try this code. For you to see what it does, use a simple query like the following one:
Code:
<?php

$this_result = mysql_query("SELECT * FROM table");

print ('<table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="#7F9DB9" align="center">');
while($row = mysql_fetch_row($this_result))
  { 
  for($i = 0; $i < mysql_num_fields($this_result); $i++)
   {	
      print ('<tr><td valign="middle" bgcolor="#F7F7F7" width="35%"><span class="style2">');
      print ('<b>'. mysql_field_name($this_result,$i) .'</b>: ');		
      print ('</span></td>');
      print ('<td valign="middle" bgcolor="#F7F7F7"><span class="style2">');
      print (htmlspecialchars($row[$i]));		
      print ('</span></td></tr>');
    }	
  }
  print ('</table>');
}

?>
 
Hey folken, i think is the content for the page witch goes inside that!
Well, i see you´re having some difficulties regarding php' include(), so take a took at php.net pages source!
 
Thanks for your help. I actually will probably use that later for a different area. However, here is a more detailed look at what is occuring. I want my table to look like this (the formating is not the best, but I think you might get the jist):
|ID|Question 1|Question 2|Question 3|
|1|2004-05-24|4|Yes|
|2|2004-05-23|3|No|


However the table it word-wrapping with the headers and the data automatcially which is what I do not want to occur.

|ID|Question|Question|Question|
| |1 |2 |3 |
|1|2004-05|4|Yes|
| |-24| | | |
|2|2004-05|3|No|
| |-23| | | |

The forum doesnt allow me to format it very well so hopefully you will understand. Again here is the code that I am using:

$result = mysql_query("SELECT * FROM $table $db);
echo "<table border=2 cellpadding=15 cellspacing=0 width=100%>\n";
echo "<tr><td>Project Number</td><td>Project Name</td><td>Prepared By</td><td>Importance</td><td>Question 1<td>Question 2</td><td>Question 3</td><td>Question 4</td><td>Date Needed</td><td>Estimated Cost</td><td>Date Requested</td><td>Assigned To</td><td>Date Assigned</td><td>Assigned By</td><td>Completed By</td><td>Date Completed</td></td></tr>\n";

while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td><center><a href='edit.php?id=$row[project_number]'>$row[project_number]</a><dt><center><a href='delete.php?id=$row[project_number]'.'\'>Delete</a></td><td>$row[project_name]</td><td>$row[prepared_by]</td><td>$row[importance]</td><td>$row[question1]</td><td>$row[question2]</td><td>$row[question3]</td><td>$row[question4]</td><td>$row[date_needed]</td><td>$row[estimated_cost]</td><td>$row[date_requested]</td><td>$row[assigned_to]</td><td>$row[assigned_to_date]</td><td>$row[assigned_by]</td><td>$row[completed_by]</td><td>$row[date_completed]</td></tr>";
}
echo "</table>\n";

Thanks for being patient with me and assisting me!
 
Just a note: Please use code tags when posting a code, ok!? ;)

Ok. Actually that´s exactly what i was thinking and that´s not the code i was planning to give you!

Anyway, this next code will print exactly a table like that:
Code:
<?php

$this_result = mysql_query("SELECT * FROM table"); 

print ('<table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="#7F9DB9" align="center">');
$a = 0;
while($row = mysql_fetch_row($this_result))
{ 
print ('<tr>');
if($a == 0)
{
for($i = $a; $i < mysql_num_fields($this_result); $i++)
{	
print ('<td valign="middle" bgcolor="#f0f0f0"><span class="style2"><strong>');
print (mysql_field_name($this_result,$i));		
print ('</strong></span></td>');
}
$a = 1;
print ('</tr><tr>');
}		
for($i = 0; $i < mysql_num_fields($this_result); $i++)
{	
print ('<td valign="middle" bgcolor="#FFFFFF">');
print (htmlspecialchars($row[$i]));		
print ('</td>');		
}
print ('</tr>');	
}	
print ('</table><br>');

?>
To help your work, the best you can do is turn this code into a function and than pass an array to it with all the question, to use instead of mysql_field_name($this_result,$i)!

Gook luck!
Gesf
 
Thanks for all your help. With some minor tweaking it looks just like I was trying to get. Thanks!
 
Back
Top