Page 1 of 1

Expand and collapsing

Posted: Thu Nov 10, 2011 10:32 am
by louiegiezer
why does my javascript code expanding and collapsing not working inside of loop
<div id="categories">
	<?php
				
        $sel = "select * from `blog_cat`";					
	$cat = mysql_query($sel);
					
	while($data = mysql_fetch_assoc($cat)){
	echo "<a href='JavaScript:doMenu('main');' id=xmain>[+]</a>$data[name]";
						
	$sel2 = mysql_query("SELECT * FROM `posts` WHERE `post_cid`='".$data['id']."'");	
	while($data2 = mysql_fetch_assoc($sel2)){

echo "
<div id=main style='margin-left:1em'>
 <a href=#>$data2[post_title]</a><br>
</div>
<br>						
	                	";
			}
		}
	?>
</div>

Re: Expand and collapsing

Posted: Thu Nov 10, 2011 6:56 pm
by jacek
In what way does it not work ?

Also, you really should not be looping queries like that ! Have a look into using JOINs. Also it might be an idea to create a function to get the information you need instead of mixing up the HTML and logic like that.

Re: Expand and collapsing

Posted: Thu Nov 10, 2011 7:14 pm
by louiegiezer
i remake this code it looks like creepy. i practice the sql join query and the function method. at this thread the problem is the javascript i think it is not compatible inside of loop statement. but when you removed to the sql query its working.

Re: Expand and collapsing

Posted: Fri Nov 11, 2011 8:20 pm
by jacek
louiegiezer wrote:i remake this code it looks like creepy. i practice the sql join query and the function method. at this thread the problem is the javascript i think it is not compatible inside of loop statement. but when you removed to the sql query its working.
I have no idea what you mean :?

Is it working ? :)

It won't be anything to do with the php, you will be making the html invalid in some way so a good step would be to make sure your html is valid.