kts Posted December 21, 2007 Share Posted December 21, 2007 Not sure why it won't let me use the } else { statement. It gives me an error. I'm trying to make it so that if there is a parent_result > 0 then it displays the subcategory, if not it just moves to the next. $k2_sql = mysql_query("SELECT * FROM categories WHERE parent_id = 0"); while ($k_result2 = mysql_fetch_array($k2_sql)){ $cat_id = $k_result2['categories_id']; $parent_sql = "SELECT * FROM categories WHERE parent_id = ".$catpath; $parent_query = mysql_query($parent_sql) or die(mysql_error()); $parent_result = mysql_num_rows($parent_query); $k_sql = mysql_query("SELECT * FROM categories WHERE parent_id = 0"); while ($k_result = mysql_fetch_array($k_sql)){ $cat_id = $k_result['categories_id']; $cat_parent = $k_result['parent_id']; $cat_sort = $k_result['sort_order']; $kk_sql = mysql_query("SELECT categories_name FROM categories_description WHERE categories_id = $cat_id"); while ($kk_result = mysql_fetch_array($kk_sql)){ $cat_name = $kk_result['categories_name']; } ?><li><a href="shop.php?cPath=<?php echo $cat_id; ?>"><?php echo $cat_name; ?></a></li> <?php } $parents_cat_id = mysql_query("SELECT * FROM categories WHERE parent_id = ".$catpath)or die(mysql_error()); $parents_cat_num = mysql_num_rows($parents_cat_id) or die(mysql_error()); while ($parent_c = mysql_fetch_array($parents_cat_id)){ $new_parents_cat_id = $parent_c['categories_id']; $parent_cat_sql = mysql_query("SELECT * FROM categories_description WHERE categories_id = $new_parents_cat_id"); if($parent_result > 0){ while ($p_c_s = mysql_fetch_array($parent_cat_sql)){ $subcat_name = $p_c_s['categories_name']; $subcat_id = $p_c_s['categories_id']; echo '<span id="left_nav_sub"><li><a href="shop.php?cPath=' .$subcat_id . '">' .$subcat_name. '</a></li></span>'; } } } } else { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82686-ifelse-statement-syntax-problem/ Share on other sites More sharing options...
BenInBlack Posted December 21, 2007 Share Posted December 21, 2007 you need to move it up 2 } you are trying to else on a while not the if } else { } } } Quote Link to comment https://forums.phpfreaks.com/topic/82686-ifelse-statement-syntax-problem/#findComment-420549 Share on other sites More sharing options...
kts Posted December 21, 2007 Author Share Posted December 21, 2007 ok, but now the whiles aren't ending correctly :-/ getting more results than before Quote Link to comment https://forums.phpfreaks.com/topic/82686-ifelse-statement-syntax-problem/#findComment-420550 Share on other sites More sharing options...
BenInBlack Posted December 21, 2007 Share Posted December 21, 2007 I commented them and they balance in my Zend Studio <?php $k2_sql = mysql_query("SELECT * FROM categories WHERE parent_id = 0"); //start while 1 while ($k_result2 = mysql_fetch_array($k2_sql)){ $cat_id = $k_result2['categories_id']; $parent_sql = "SELECT * FROM categories WHERE parent_id = ".$catpath; $parent_query = mysql_query($parent_sql) or die(mysql_error()); $parent_result = mysql_num_rows($parent_query); $k_sql = mysql_query("SELECT * FROM categories WHERE parent_id = 0"); //start while 2 while ($k_result = mysql_fetch_array($k_sql)){ $cat_id = $k_result['categories_id']; $cat_parent = $k_result['parent_id']; $cat_sort = $k_result['sort_order']; $kk_sql = mysql_query("SELECT categories_name FROM categories_description WHERE categories_id = $cat_id"); while ($kk_result = mysql_fetch_array($kk_sql)){ $cat_name = $kk_result['categories_name']; } ?><li><a href="shop.php?cPath=<?php echo $cat_id; ?>"><?php echo $cat_name; ?></a></li> <?php } // end while 2 $parents_cat_id = mysql_query("SELECT * FROM categories WHERE parent_id = ".$catpath)or die(mysql_error()); $parents_cat_num = mysql_num_rows($parents_cat_id) or die(mysql_error()); //start while 3 while ($parent_c = mysql_fetch_array($parents_cat_id)){ $new_parents_cat_id = $parent_c['categories_id']; $parent_cat_sql = mysql_query("SELECT * FROM categories_description WHERE categories_id = $new_parents_cat_id"); if($parent_result > 0){ //start while 4 while ($p_c_s = mysql_fetch_array($parent_cat_sql)){ $subcat_name = $p_c_s['categories_name']; $subcat_id = $p_c_s['categories_id']; echo '<span id="left_nav_sub"><li><a href="shop.php?cPath=' .$subcat_id . '">' .$subcat_name. '</a></li></span>'; } // end while 4 } else { //if block of code here } } // end while 3 } // end while 1 ?> Quote Link to comment https://forums.phpfreaks.com/topic/82686-ifelse-statement-syntax-problem/#findComment-420567 Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 You need to learn indentation and how to organize your code layout so you can find loops easier. Quote Link to comment https://forums.phpfreaks.com/topic/82686-ifelse-statement-syntax-problem/#findComment-420570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.