Forum Moderators: coopster

Message Too Old, No Replies

displaying database results

displaying database results

         

smorales

6:50 pm on Feb 22, 2008 (gmt 0)

10+ Year Member



I need some help please. I'm a very beginner when it comes to database programming.

The problem I'm having is when a location name is not entered a break occurs and pushes info down one line. If there is no location, I want to info to appear valigned top.

Here an example:
<snip>

and code:

$con = mysql_connect("#*$!xx","#*$!#*$!","#*$!#*$!");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("#*$!#*$!xx", $con);

$result = mysql_query("SELECT * FROM events ORDER BY date ASC");

echo "
<table border='1' cellspacing='0' summary='Event Calendar'id='cal'>
<tr>
<th scope='col' width='60'>Date</th>
<th scope='col' width='110'>Event</th>
<th scope='col' width='75'>Time</th>
<th scope='col' width='175'>Location</th>
<th scope='col' >Other</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td valign='top'>" . $row['date'] . "<br></td>";
echo "<td valign='top'>" . $row['name'] . "</td>";
echo "<td valign='top'>" . $row['begin'] . "-" . $row['end'] . "</td>";
echo "<td valign='top'>" . $row['location'] . "<br>" . $row['address'] . "<br>" . $row['city'] . ", " . $row['state'] . "<br>" . $row['country'] . "" . $row['zip'] . "</td>";
echo "<td>Hosted by:<br>" . $row['host1'] . "<br>" . $row['host1-phone'] . "<br>" . "<br>" . $row['host2'] . "<br>" . $row['host2-phone'] ."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

PLEASE HELP!
thank you sooo much

[edited by: eelixduppy at 7:32 pm (utc) on Feb. 22, 2008]
[edit reason] no URLs, please [/edit]

PHP_Chimp

8:48 pm on Feb 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only place you have $row['location'] that I am assuming is the location you are talking about is -
echo "<td valign='top'>" . $row['location'] . "<br>" . $row['address'] . "<br>" . $row['city'] . ", " . $row['state'] . "<br>" . $row['country'] . "" . $row['zip'] . "</td>";

So you need a little bit of if -

if (![url=http://uk2.php.net/manual/en/function.empty.php]empty[/url]($row['location'])) {
echo "<td valign='top'>" . $row['location'] . "<br>" . $row['address'] . "<br>" . $row['city'] . ", " . $row['state'] . "<br>" . $row['country'] . "" . $row['zip'] . "</td>";
}
else {
echo "<td valign='top'>" . $row['address'] . "<br>" . $row['city'] . ", " . $row['state'] . "<br>" . $row['country'] . "" . $row['zip'] . "</td>";
}

You may want to look at both empty [uk2.php.net] and also isset [uk2.php.net] as they are both useful for checking to see if variables are useful.

<edit>
Would help if I used not empty for the if loop...

[edited by: PHP_Chimp at 8:49 pm (utc) on Feb. 22, 2008]

smorales

9:33 pm on Feb 22, 2008 (gmt 0)

10+ Year Member



Thanks so much for the help, works great. I'm just getting used to the "logic" behind if statements.

Your help is appreciated!