Page 1 of 2

Simple Question, from database into a table

Posted: Thu Jun 22, 2006 8:09 am
by phelpsa
Please could someone help me with this script:

Code: Select all

<?php

$conn = mysql_connect("localhost", "root", "");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("bugeye")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT Date, Event, Website
FROM ubb_events";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}


while ($row = mysql_fetch_assoc($result)) {
$date = $row["Date"];
$event = $row["Event"];
$website = $row["Website"];
}

foreach (***) {
echo '$date<br>$event<br>$website<br>'; };

mysql_free_result($result);

?>
Especially where I have gone wrong from line 31 onwards, and what needs to go in the foreach brackets.

Thanks in advance.

Adam

Posted: Thu Jun 22, 2006 8:23 am
by Kadanis
hi

what are you trying to achieve, if you can state your goal it might be easier for someone to help you.

Posted: Thu Jun 22, 2006 8:27 am
by phelpsa
I'm trying to take the date, event and website link from the table ubb_events and post them into a table.

Adam

Posted: Thu Jun 22, 2006 8:29 am
by Oren
Not sure how you want the table to look like.
Try replacing this:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
$date = $row["Date"];
$event = $row["Event"];
$website = $row["Website"];
}

foreach (***) {
echo '$date<br>$event<br>$website<br>'; };

mysql_free_result($result);
With this:

Code: Select all

echo '<table><tr><th>Date</th><th>Event</th><th>Website</th></tr>';

while ($row = mysql_fetch_assoc($result))
{
	echo '<tr><td>' . $row['Date'] . '</td><td>' . $row['Event'] . '</td><td>' . $row['Website'] . '</td></tr>';
}

echo '</table>';

mysql_free_result($result);

Posted: Thu Jun 22, 2006 8:31 am
by Kadanis
Try this. You need to replace both you while loop and foreach with this code. It will loop over all the records in the table in your database and then print them out in a html table on the page.

Code: Select all

echo "<table width=\"760\" align=\"center\">";
echo "<tr><td>Date</td><td>Event</td><td>Website</td></tr>";
while ($row = mysql_fetch_assoc($result)) {
	$date = $row["Date"];
	$event = $row["Event"];
	$website = $row["Website"];
	echo "<tr><td>$date</td><td>$event</td><td>$website</td></tr>";
}
echo "</table>";
Hope it helps

Posted: Thu Jun 22, 2006 8:33 am
by Kadanis
lol, Oren you beat me to it :lol:

Posted: Thu Jun 22, 2006 8:37 am
by phelpsa
Thats great! Thanks a lot.

Adam

Posted: Thu Jun 22, 2006 8:43 am
by Oren
Kadanis, I recommend you to change this:

Code: Select all

echo "<tr><td>Date</td><td>Event</td><td>Website</td></tr>";
Into this:

Code: Select all

echo '<tr><td>Date</td><td>Event</td><td>Website</td></tr>';
(I replaced the double quotes with single quotes)

From the php.net site: When a string is specified in double quotes or with heredoc, variables are parsed within it.
Since you don't have any variables on this line, there is no need to call to the parser :wink:

Posted: Thu Jun 22, 2006 10:06 am
by phelpsa
The script is used in a template, here is the script I've finally come up with. The template is loaded fine but it doesnt show the results.

Code: Select all

mysql_connect("localhost", "root", "");

mysql_select_db("bugeye");

$sql = "SELECT Date, Event, Website
FROM ubb_events
ORDER BY Date AS
LIMIT 5";

$result = mysql_query($sql);



while ($row = mysql_fetch_assoc($result))
{
        $events = '<tr><td>' . $row['Date'] . '</td><td><a href="' . $row['Website'] . '">' . $row['Event'] . '</a></td></tr>';
}

mysql_free_result($result);

eval('echo "'.template('Events').'";');
Template:

Code: Select all

<table><tr><th>Date</th><th>Event</th></tr>
$events
</table>
Could someone help me even further?

Posted: Thu Jun 22, 2006 10:12 am
by Oren
Try this:

Code: Select all

$events .= '<tr><td>' . $row['Date'] . '</td><td><a href="' . $row['Website'] . '">' . $row['Event'] . '</a></td></tr>';
Note the '.' (period) before the '=' sign.

Posted: Thu Jun 22, 2006 10:20 am
by phelpsa
Unfortunately that made no difference :(

It shows the table with the 'Dates' and 'Events', but not the results.

Adam

Posted: Thu Jun 22, 2006 10:21 am
by feyd
$events isn't output or given to anything to output.

Posted: Thu Jun 22, 2006 10:23 am
by phelpsa
Sorry, I'm new to this, please could you explain a bit further? :wink:

Adam

Posted: Thu Jun 22, 2006 10:42 am
by Oren
Template:

Code: Select all

echo '<table><tr><th>Date</th><th>Event</th></tr>' . $events . '</table>'

Posted: Thu Jun 22, 2006 10:52 am
by phelpsa
There can't be any PHP in the template, only variables.

Adam