Jump to content

Archived

This topic is now archived and is closed to further replies.

Akira

2 datasets in 2 <td>'s, don't understand

Recommended Posts

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>'s

So 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
this is the same sql query twice though

you could use sql with 2 databases in any case

SELECT * FROM `table_1` AS t1, `table_2` AS t2 WHERE ...

Share this post


Link to post
Share on other sites
Hey guys, thnx for the fast reply

Don'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 :)


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The easiest way to do this without changing all your code would to be to put a table within a table

Example

[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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

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.