Jul 26, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

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

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


QUOTE


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:

QUOTE


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:

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 biggrin.gif

 

 

 


Reply

galexcd
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:

CODE
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?

Reply

shadowx
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:

QUOTE


DB1 Data
Db1 Data
BD1 Data

DB2 Data
Db2 Data
..
..

but what i want is to alternate between data eg:

QUOTE

DB1
Db2
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:

QUOTE
question
answer
next question
next answer
...
...





Reply

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

CODE
<?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:
CODE
$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");
}

 

 

 


Reply

shadowx
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 laugh.gif thanks

Reply

shadowx
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.

Reply

galexcd
yeah I kinda typed them up on the spot so I didn't get to test them, but I'm glad it worked!

Reply

jlhaslip
Closing per request.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : php sql php nested loops resolved nested loops


    Looking for php, sql, php, nested, loops, make, nested, loops, work,

Searching Video's for php, sql, php, nested, loops, make, nested, loops, work,
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE