Log in

View Full Version : table help



davidjmorin
01-22-2008, 08:19 PM
i have the following script and need help with some of the table work

id like to have the cells when new ones are added they go horizontal until they reach 4 colomns then drop down a row. how would i accomplish taht?


<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT name, date, Email, message FROM contact";
$result = mysql_query($query);
echo '<table border="5" BORDERCOLORLIGHT=YELLOW BORDERCOLORDARK=BLUE bgcolor="#6699ff" width="200px"><tr><td>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td>';
echo "Date : {$row['date']} <br>" .
"Name :{$row['name']} <br>" .
"Email : {$row['Email']} <br>" .
"Message : {$row['message']} <br><br>";
echo '</tr>';
echo '</td>';
}

echo '</table>';
include 'closedb.php';
?>
<html>
<body bgcolor="black">


</html>

Medyman
01-22-2008, 11:46 PM
Here is a PHP calendar which does the same thing after 7 rows, see what you can grab from it...


<?php
//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;

//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}

echo "</tr></table>";

?

davidjmorin
01-22-2008, 11:52 PM
i dont see how there doing it but thank you for tyhe post

howellsgs
01-23-2008, 04:09 AM
echo '<tr>';
echo '<td>';
echo "Date : {$row['date']} <br>" .
"Name :{$row['name']} <br>" .
"Email : {$row['Email']} <br>" .
"Message : {$row['message']} <br><br>";
echo '</tr>';
echo '</td>';


Lets take a look at this block from your current code...
and you want it to ouput: DATE:
NAME:
EMAIL:
MESSAGE: (format)
<line break>

rewrite the block to look like this:

echo <tr><td> Date: {$row['date']} </td></tr>
echo <tr><td> Name: {$row['name']} </td></tr>
echo <tr><td> Email: {$row['email']} </td></tr>
echo <tr><td> Message: {$row['message']} </td></tr>
echo <br/>

james438
01-23-2008, 03:34 PM
Heh, I'd use quotes in my echo lines, but that's just me ;) I figure the missing <?php ?> tags were implied.

boogyman
01-23-2008, 06:29 PM
echo '<tr>';
echo <tr><td> Date: {$row['date']} </td></tr>
echo <tr><td> Name: {$row['name']} </td></tr>
echo <tr><td> Email: {$row['email']} </td></tr>
echo <tr><td> Message: {$row['message']} </td></tr>
echo <br/>

First, james is correct in that your echo's should be quoted.
If you wish to use the same structure as you have there you would need double quotes, because single quotes do not allow for variable parsing. you also need to have semi-colon's at the end of each line because whitespace doesn't mean anything to a computer.


echo "<tr><td> Date: {$row['date']} </td></tr>";
echo "<tr><td> Name: {$row['name']} </td></tr>";
echo "<tr><td> Email: {$row['email']} </td></tr>";
echo "<tr><td> Message: {$row['message']} </td></tr>";


The other method would be to get rid of the curly braces, and use concantenation to assign the variable



echo "<tr><td> Date: ". $row['date'] ." </td></tr>";
echo "<tr><td> Name:". $row['name'] ." </td></tr>";
echo "<tr><td> Email: ". $row['email'] ." </td></tr>";
echo "<tr><td> Message: ". $row['message'] ." </td></tr>";