Jump to content

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


Akira

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
Link to comment
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.
Link to comment
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 :)


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.