Jump to content
xisto Community
Sign in to follow this  
shadowx

Php: Help With Some Sql/php And Nested While Loops Please! [resolved] how do i make nested while loops work?

Recommended Posts

Hi all, as part of a project i need to include data from two different tables in mysql in the following way:


data from DB1

data from DB2

data from DB1

...
...
...

and so on. nw the best way i can think of this bearing in mind each table might have several rows which i need to pull, is to use a while loop for each query and i have one query for each table. that way i can pull as many rows as i need to, with me so far?! Thought not but ill carry on anyway!

Now ive got eh data pulled from the database i need to somehow put it in the order shown in the quotes, the only way i could think to do this was to nest the second While loop so i end up with this structure:

while (pull data from table1){
echo data from table one

while(pull data from table TWO){
echo table TWO data
}

}



the problem is it doesnt seem to run the second while loop, why is this? a problem with my code (see below) or just because nested While loops dont work?



CODE:

while($row_client = mysql_fetch_array($result_client)){	$client_ref = $row_client['reference'];	$client_subject = $row_client['subject'];	$client_question = $row_client['question'];	//while there are rows in ether table echo them.	echo "	<table bgcolor=#99ccff border=1 style=\"border-collapse:collapse;  border-color:blue;\" width=49%>	<TR><TD>	<u><b>Client ref:</b> $client_ref <BR><BR> <b>Subject:</b> $client_subject </u>	</TR></TD>	<TR><TD>	$client_question	</TR></TD>	</table>";		echo "1";		}		While($row_team = mysql_fetch_array($result_team)){						$team_ref = $row_team['unique_ref'];		$team_respondent = $row_team['respondent'];		$team_response = $row_team['response'];		echo "		<BR><BR>		<table bgcolor=#ccffcc border=1 style=\"border-collapse:collapse;  border-color:blue;\" width=49%>		<TR><TD>		<u><b>Response ref:</b> $team_ref  <b>Respondant:</b> $team_respondent </u>		</TR></TD>		<TR><TD>		$team_response		</TR></TD>		</table>";				echo "2";			}

yes, its messy ;)

Share this post


Link to post
Share on other sites

I think an easier way of getting multiple rows from SQL is with for loops. I don't really get what you exactly want but try this format:

mysql_connect//blah blah blah conect to db 1$rows1=mysql_query("blah blah blah query here");mysql_connect//blah blah blah conect to db 2$rows2=mysql_query("blah blah blah query here");mysql_close();for($i=0;$i<mysql_numrows($rows1);$i++){echo mysql_result($rows1,$i,"column name");}for($i=0;$i<mysql_numrows($rows2);$i++){echo mysql_result($rows2,$i,"column name");}

Is that what you meant?

Share this post


Link to post
Share on other sites

Its sort of what i want, and i hadn't thought of using that way, probably because ive never used a function to count rows in a table. It looks good but the drawback is using that i would get data like this i think:

DB1 Data
Db1 Data
BD1 Data

DB2 Data
Db2 Data
..
..

but what i want is to alternate between data eg:

DB1Db2
DB1
Db2
DB1
..
..
..



the purpose of the script is to have a client's question listed then a response and then any follow up quuestions they had like:

questionanswer
next question
next answer
...
...


Share this post


Link to post
Share on other sites

alright. I gotcha this script should work. No nested while loops required
Try this:

<?php$table1=mysql_query("blah blah blah");$table2=mysql_query("blah blah blah");$j=0;for($i=0;$i<$mysql_numrows($table1);$i+=($j-1)){$j++;$result="table".$j;$j%=2;if($j==1 || mysql_numrows($table2)>$i) echo mysql_result($$result,$i,"column");}?>

The above example wont work if there are more rows in table 2 as in table 1 I thought that is ok because its question and answer. Heh, i kinda felt like coding fancy using modulus(%) and pointers($$result) etc...

If you want a more simple code you can use this:
$table1=mysql_query("blah blah blah");$table2=mysql_query("blah blah blah");for($i=0;$i<mysql_numrows($table1)||$i<msql_numrows($table2);$i++){if($i<mysql_numrows($table1)) echo mysql_result($table1,$i,"column");if($i<mysql_numrows($table2)) echo mysql_result($table2,$i,"column");}

Share this post


Link to post
Share on other sites

it looks good at a glance, im at college at the mo though so i cant impliment it, ill give it a run when i get home though, if this works i will be very happy ;) thanks

Share this post


Link to post
Share on other sites

Just finished most of my script and that code you wrote worked fine after a few tweaks, like you made a typo with putting "msql" instead of "mysql" but thanks to your code and some intuition i now have my data arranged correctly. Thanks! This topic can be pretty much closed now if you want, mods.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.