<?php
$q1 = mysql_query("SELECT * FROM num where d =0");
$q2 = mysql_query("SELECT * FROM num where d =1");
echo'
	<table>
';
$i=0;
while($row1= mysql_fetch_assoc($q1)){
	$i++;
	echo'
		  <tr>
			<td>'.$i.'</td>
			<td><div id="im1"></div></td>
			<td><div id="im2">'.$row1["user"].'</div></td>
		  </tr>
	';
}
echo'
	</table>
';

num tabla like this

[B]id	name	d[/B]	
1	Region Name	0	
2	Afghanistan	0	
3	Africa	0	
4	Albania	0	
5	Algeria	0	
6	American Samoa	1	
7	Andorra	1	
8	Angola	1	
9	Anguilla	1	
10	Antarctica	1	
11	Antigua & Barbuda	0	
12	Antilles, Netherlands	1	
13	Arabia, Saudi	0	
14	Argentina	1	
15	Armenia	1	
16	Aruba	0	
17	Asia	0	
18	Australia	1

i want to list d which value is 0 into <div id="im2">
and
i want to list d which value is 1 into <div id="im1">

    Right now, you have one while loop which handles $q1. You'll need to have a second loop which handles $q2.

    You should also be checking for errors returned by mysql_query. Lastly, you should probably consider using mysli_query instead (the "i" stands for "improved")

      sneakyimp;10996411 wrote:

      You'll need to have a second loop which handles $q2.

      I highly disagree.

      Instead, I would think a more sensible solution would be to get rid of the second query and instead retrieve all rows whose 'd' value is 1 or 2. Then, inside the (single) loop, you'd simply have an if() statement to determine which <div> should get the information from the row vs. which div will be left empty.

        incidentally, why do you have divs inside of table cells?

          BG you are definitely right that it would be more efficient to have a single query. I was hoping to guide the poster from their current starting point as that seemed the path of least resistance.

          A single query that fetches all the records and then branches based on the value of d does sound more efficient. And, now that I read the OP again (and Derokorian's post), I see that both divs are in the table cells and that we are creating numerous div tags with identical id attributes which is a no-no. Are we trying to style these divs or identify them?

            Derokorian;10996422 wrote:

            incidentally, why do you have divs inside of table cells?

            To specify the location of mysql_fetch_assoc result

              bradgrafelman;10996416 wrote:

              I highly disagree.

              Instead, I would think a more sensible solution would be to get rid of the second query and instead retrieve all rows whose 'd' value is 1 or 2. Then, inside the (single) loop, you'd simply have an if() statement to determine which <div> should get the information from the row vs. which div will be left empty.

              how can i do it

                4 days later

                You don't need the <div> tags there, you can just put the id attributes on the <td>

                Change your query to something like this:

                SELECT * FROM num where d IN  (0,1) ORDER BY d
                

                As for your while loop, you can't do what you want like that. PHP isn't Javascript, so once you've echoed something out, you can't go back and insert other things into a block just because it's got an id value.

                What you can do is echo out up to the first cell you need to put things into, then start your loop. In the loop, just echo out the data you want until the value of d changes in a row in your loop. When that happens, echo out the HTML to close that cell and open another, then carry on with the loop. Finally, just close off the cell and the table and you're done.

                  Write a Reply...