PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 8:09 am
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
Kadanis
Forum Contributor
Posts: 180 Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:
Post
by Kadanis » Thu Jun 22, 2006 8:23 am
hi
what are you trying to achieve, if you can state your goal it might be easier for someone to help you.
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 8:27 am
I'm trying to take the date, event and website link from the table ubb_events and post them into a table.
Adam
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Thu Jun 22, 2006 8:29 am
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);
Kadanis
Forum Contributor
Posts: 180 Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:
Post
by Kadanis » Thu Jun 22, 2006 8:31 am
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
Kadanis
Forum Contributor
Posts: 180 Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:
Post
by Kadanis » Thu Jun 22, 2006 8:33 am
lol, Oren you beat me to it
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 8:37 am
Thats great! Thanks a lot.
Adam
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Thu Jun 22, 2006 8:43 am
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
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 10:06 am
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?
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Thu Jun 22, 2006 10:12 am
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.
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 10:20 am
Unfortunately that made no difference
It shows the table with the 'Dates' and 'Events', but not the results.
Adam
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Jun 22, 2006 10:21 am
$events isn't output or given to anything to output.
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 10:23 am
Sorry, I'm new to this, please could you explain a bit further?
Adam
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Thu Jun 22, 2006 10:42 am
Template:
Code: Select all
echo '<table><tr><th>Date</th><th>Event</th></tr>' . $events . '</table>'
phelpsa
Forum Commoner
Posts: 48 Joined: Thu Feb 17, 2005 1:05 pm
Post
by phelpsa » Thu Jun 22, 2006 10:52 am
There can't be any PHP in the template, only variables.
Adam