Akira Posted June 9, 2006 Share Posted June 9, 2006 Hey people,Got bit weird question, googled it, but couldn't find anything about this subject.This is the isseu. I got 2 datasets in a while loop and want to insert them in the 2 seperate <td>'sSo you would get something like this:[code]$en_q = "SELECT * FROM doh WHERE view='4' ORDER BY sort ASC";$en_r = mysql_query($en_q) or die (mysql_error()); while ($row = mysql_fetch_array($en_r)) { $menu_en=$row['text']; echo"<tr><td class='lang'>$menu_en</td>"; }$en_q = "SELECT * FROM doh WHERE view='4' ORDER BY sort ASC";$en_r = mysql_query($en_q) or die (mysql_error()); while ($row = mysql_fetch_array($en_r)) { $menu_en=$row['text']; echo"<td class='lang'>$menu_en</td></tr>"; }[/code]offcorse this doesn't work, so i guess i need to set a double while or somthing, like:[code]while ($row = mysql_fetch_array($en_r) && $lala = mysql_fetch_array($as_r)) [/code]Offcorse this doens't work either, but how can i solve this??Regards,Akira Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/ Share on other sites More sharing options...
wildteen88 Posted June 9, 2006 Share Posted June 9, 2006 I am completly confused with this yur queries are exactly the same and I have no idea what you're trying to do.Could explain what you're trying to do and include an example too. Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43627 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 The problem is you're looping through every row returned by mysql_fetch_array and so are creating many <tr><td> and then </td></tr> which is invalid HTML. If the query only returns one row then it should work but if not, then that's your problem. Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43628 Share on other sites More sharing options...
joquius Posted June 9, 2006 Share Posted June 9, 2006 this is the same sql query twice thoughyou could use sql with 2 databases in any caseSELECT * FROM `table_1` AS t1, `table_2` AS t2 WHERE ... Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43632 Share on other sites More sharing options...
Akira Posted June 9, 2006 Author Share Posted June 9, 2006 Hey guys, thnx for the fast replyDon't mind the same queries, the 'doh' part is different, shoudl be $doh, cause the tabels are variable, sorry for that.so what i want is<table> <tr> <td> one databset ( one query with loop, both dataset have the same amount of rows) </td> </tr> <tr> <td> second dataset with different query </td> </tr></table>Hope this clears a lot :) Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43633 Share on other sites More sharing options...
wildteen88 Posted June 9, 2006 Share Posted June 9, 2006 Rather than using two databasets, query two tables with one query! therfore you use just only one dataset, heres an example:[code]$sql = "SELECT t1.*, t2.* FROM table1_name t1, table2_name t2 WHERE t1.views = "4" && t2.views = "4" ORDER BY sort ASC";$result = mysql_query($sql);[/code]Now you use a normal while loop[code]while($row = mysql_fetch_array($result)){ echo "<tr><td class=\"lang\">$row['someIndex']</td><td class=\"Lang\">$row['someOtherIndex']</td></tr>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43638 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 Well I can see what you want but not why you'd want it. How are you going to separate the values in each dataset? Or do you actually want the datasets to appear in two separate columns and not rows. In which case you could do:[code]// Since both datasets have the same number of rows$q1 = [Your first query]$q2 = [Your second query]while($r1 = mysql_fetch_array($q1)){$r2 = mysql_fetch_array($q2);echo "<tr><td>$r1[text]</td><td>$r2[text]</td></tr>";}[/code][!--quoteo(post=381852:date=Jun 9 2006, 09:31 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 9 2006, 09:31 AM) [snapback]381852[/snapback][/div][div class=\'quotemain\'][!--quotec--]Rather than using two databasets, query two tables with one query! therfore you use just only one dataset, heres an example:[code]$sql = "SELECT t1.*, t2.* FROM table1_name t1, table2_name t2 WHERE t1.views = "4" && t2.views = "4" ORDER BY sort ASC";$result = mysql_query($sql);[/code]Now you use a normal while loop[code]while($row = mysql_fetch_array($result)){ echo "<tr><td class=\"lang\">$row['someIndex']</td><td class=\"Lang\">$row['someOtherIndex']</td></tr>";}[/code][/quote]Wouldn't it be easier to use:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] t1.text [color=green]as[/color] t1_text, t2.text [color=green]as[/color] t2_text[color=green]FROM[/color] [color=orange]table1_name[/color] t1, table2_name t2[color=green]WHERE[/color] t1.views[color=orange]=[/color][color=red]'4'[/color] [color=blue]AND[/color] t2.views[color=orange]=[/color][color=red]'4'[/color][color=green]ORDER BY[/color] sort [color=green]ASC[/color] [!--sql2--][/div][!--sql3--] then you can call $row['t1_text'] and 't2_text' as opposed to having to count the columns in the database table to determine the correct index. Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43641 Share on other sites More sharing options...
craygo Posted June 9, 2006 Share Posted June 9, 2006 The easiest way to do this without changing all your code would to be to put a table within a tableExample[code]<table><tr><td><table><?// run your first mysql loop here which would start with <tr> and end with </tr>$en_q = "SELECT * FROM doh WHERE view='4' ORDER BY sort ASC";$en_r = mysql_query($en_q) or die (mysql_error()); while ($row = mysql_fetch_array($en_r)) { $menu_en=$row['text']; echo"<tr><td class='lang'>$menu_en</td>"; }?></table></td><td><table><?// run your second mysql loop here which would start with <tr> and end with </tr>$en_q2 = "SELECT * FROM doh WHERE view='4' ORDER BY sort ASC";$en_r2 = mysql_query($en_q2) or die (mysql_error()); while ($row2 = mysql_fetch_array($en_r2)) { $menu_en2=$row2['text']; echo"<td class='lang'>$menu_en2</td></tr>"; }?></table></td></tr></table>[/code]Easy enough?!?!ray Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43694 Share on other sites More sharing options...
Akira Posted June 9, 2006 Author Share Posted June 9, 2006 Hey guys! thnx for all the good reply's!Found that the following was the easiest to implent for me :)[code]// Since both datasets have the same number of rows$q1 = [Your first query]$q2 = [Your second query]while($r1 = mysql_fetch_array($q1)){$r2 = mysql_fetch_array($q2);echo "<tr><td>$r1[text]</td><td>$r2[text]</td></tr>";}[/code]thnx again for the great help! Works like a charm now :D Quote Link to comment https://forums.phpfreaks.com/topic/11579-2-datasets-in-2-s-dont-understand/#findComment-43855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.