Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-22-2005, 11:16 AM   #1
Resident Slacker
 
homer15's Avatar
 
Join Date: Dec 2001
Location: Suisun City, California (i know, where the hell is that?!?!?)
Posts: 2,620
looping a mysql query with php

hey everyone,

i'm still pretty new to using sql with php and i need some help.

what i need is to have some html tables built automatically off of two mysql tables.

mysqltable_1 includes dates and some basic header information for each date
mysqltable_2 has events that occurred on each date, multiple events for each date

i want the html table to contain the header information for each date, and the rows beneath it to list the event, and i want it to keep building tables until the first mysqltable_1 no longer has any new dates.
example:
Date 1
event 1event 1 info
event 2event 2 info

Date 2
event 3event 3 info
event 4event 4 info


what i thought might work follows (replace the ^ with ><'s):
Quote:
^?php
$host = "localhost";
$db = "local_events";

mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());

// Querying the table
$query = mysql_query("SELECT * FROM `mysqltable_1`");
while($row=mysql_fetch_array($query))

{

$DateVal=$row['Date'];
?^

^table^
^tr^^td^ ^?echo $row['Date']; ?^ ^/td^^/tr^

^?php

// Querying the table
$query = mysql_query("SELECT * FROM `mysqltable_2` where `Date`=$DateVal");
while($row=mysql_fetch_array($query))

{

?^
^tr^
^td^ ^?echo $row['Event']; ?^ ^/td^^td^ ^?echo $row['EventInfo']; ?^ ^/td^
^/tr^

^?
}
?^

^/table^

^/td^

^/tr^
^/table^
^?
}
?^
this builds only one table, but it builds it perfectly. now, i need it to repeat for every date that is in mysqltable_1. i know this probably isn't the most effecient (and obviously not the correct) way to do this. can anyway help me along to get to where i want?
__________________
Friends help you move. REAL friends help you move bodies. - me
quite possibly the best book ever written... by me
homer15 is offline   Reply With Quote
Old 06-22-2005, 05:03 PM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
If I understand your post correctly, what you want to do can be achieved by joining the two tables, something like:
Code:
SELECT * FROM mysqltable1, mysqltable2 WHERE mysqltable1.date = mysqltable2.date
More info on joins:
http://dev.mysql.com/doc/mysql/en/join.html

I may be able to help more if you post the structure of the tables.

HTH

EDIT:
BTW you can use [ php ] [/ php ] tags to post PHP code without having to replace < and >.

Last edited by aym; 06-22-2005 at 05:06 PM.
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2