Jump to content


Photo

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


  • Please log in to reply
8 replies to this topic

#1 Akira

Akira
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 09 June 2006 - 02:01 PM

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

offcorse this doesn't work, so i guess i need to set a double while or somthing, like:


while ($row = mysql_fetch_array($en_r) && $lala = mysql_fetch_array($as_r))

Offcorse this doens't work either, but how can i solve this??

Regards,
Akira
Greetz Akira


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 June 2006 - 02:09 PM

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.

#3 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 02:10 PM

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.
[table]



Don't worry, the printer fairies will sort it out.

#4 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 09 June 2006 - 02:18 PM

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

#5 Akira

Akira
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 09 June 2006 - 02:24 PM

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



Greetz Akira


#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 June 2006 - 02:31 PM

Rather than using two databasets, query two tables with one query! therfore you use just only one dataset, heres an example:
$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);
Now you use a normal while loop
while($row = mysql_fetch_array($result))
{
    echo "<tr><td class=\"lang\">$row['someIndex']</td><td class=\"Lang\">$row['someOtherIndex']</td></tr>";
}


#7 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 02:43 PM

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:
// 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>";
}

[!--quoteo(post=381852:date=Jun 9 2006, 09:31 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 9 2006, 09:31 AM) View Post[/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:
$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);
Now you use a normal while loop
while($row = mysql_fetch_array($result))
{
    echo "<tr><td class=\"lang\">$row['someIndex']</td><td class=\"Lang\">$row['someOtherIndex']</td></tr>";
}
[/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 as t1_text, t2.text as t2_text
FROM table1_name t1, table2_name t2
WHERE t1.views='4' AND t2.views='4'
ORDER BY sort ASC [!--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.
[table]



Don't worry, the printer fairies will sort it out.

#8 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 09 June 2006 - 03:53 PM

The easiest way to do this without changing all your code would to be to put a table within a table

Example

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

Easy enough?!?!

ray

#9 Akira

Akira
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 09 June 2006 - 09:27 PM

Hey guys! thnx for all the good reply's!

Found that the following was the easiest to implent for me :)

// 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>";
}

thnx again for the great help! Works like a charm now :D
Greetz Akira





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users