raptor30506090 Posted December 6, 2011 Share Posted December 6, 2011 Hi can any one help please im still very new but hard trying The problem is that i only get 1 result back from the database i need it to loop any ideas please $query = mysql_query("SELECT * FROM categorys, sub_categorys WHERE categorys.cat_id = sub_categorys.cat_id"); echo "<ul>"; $row[] = mysql_fetch_array($query); foreach($row as $category){ echo "<li>"; echo $category['category']; foreach($row as $sub_category){ if($category['cat_id'] == $sub_category["cat_id"]){ echo "<ul><li>".$sub_category["sub_category"]."</li></ul>"; } } echo "</li>"; } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/ Share on other sites More sharing options...
xyph Posted December 6, 2011 Share Posted December 6, 2011 http://www.php.net/manual/en/function.mysql-fetch-assoc.php#90030 Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295049 Share on other sites More sharing options...
raptor30506090 Posted December 6, 2011 Author Share Posted December 6, 2011 Sorry no errors its getting back •Home ◦our staff thats it need it to loop get rest of the menu <?php $query = mysql_query("SELECT * FROM categorys, sub_categorys WHERE categorys.cat_id = sub_categorys.cat_id")or die(mysql_error()); echo "<ul>"; $row[] = mysql_fetch_array($query); foreach($row as $category){ echo "<li>"; echo $category['category']; foreach($row as $sub_category){ if($category['cat_id'] == $sub_category["cat_id"]){ echo "<ul><li>".$sub_category["sub_category"]."</li></ul>"; } } echo "</li>"; } echo "</ul>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295053 Share on other sites More sharing options...
xyph Posted December 6, 2011 Share Posted December 6, 2011 Because you're only fetching one result. You have to use mysql_fetch_xxx in a loop to grab every row returned. Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295055 Share on other sites More sharing options...
raptor30506090 Posted December 6, 2011 Author Share Posted December 6, 2011 ok changed that to this now and i now get 2 submenu results but still not doing the job <?php $query = mysql_query("SELECT * FROM categorys, sub_categorys WHERE categorys.cat_id = sub_categorys.cat_id")or die(mysql_error()); echo "<ul>"; $row[] = mysql_fetch_array($query); foreach($row as $category){ echo "<li>"; echo $category['category']; $row[] = mysql_fetch_array($query); foreach($row as $sub_category){ if($category['cat_id'] == $sub_category["cat_id"]){ echo "<ul><li>".$sub_category["sub_category"]."</li></ul>"; } } echo "</li>"; } echo "</ul>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295057 Share on other sites More sharing options...
ManiacDan Posted December 6, 2011 Share Posted December 6, 2011 $row[] = mysql_fetch_array($query); You need to use mysql_fetch_array IN THE LOOP. The manual page you were linked to contains examples on how to do it properly. Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295076 Share on other sites More sharing options...
raptor30506090 Posted December 6, 2011 Author Share Posted December 6, 2011 whats the manual page? Thank for your help Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295080 Share on other sites More sharing options...
xyph Posted December 6, 2011 Share Posted December 6, 2011 Search this page for the words 'manual' One of those results will be a link Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295082 Share on other sites More sharing options...
raptor30506090 Posted December 6, 2011 Author Share Posted December 6, 2011 Yes had a look and now it returns this as you can see it nown returns two home links there should only be one and also a service link as well below the about driving me mad thanks for this help Home ◦our history ◦our staff Home ◦our history ◦our staff ◦plastering About ◦why we started ◦about the company Contact ◦company policy <?php $query = mysql_query("SELECT * FROM categorys, sub_categorys WHERE categorys.cat_id = sub_categorys.cat_id")or die(mysql_error()); echo "<ul>"; while($row[] = mysql_fetch_array($query)){ foreach($row as $category){ } echo "<li>"; echo $category['category']; $row[] = mysql_fetch_array($query); foreach($row as $sub_category){ if($category['cat_id'] == $sub_category["cat_id"]){ echo "<ul><li>".$sub_category["sub_category"]."</li></ul>"; } } echo "</li>"; } echo "</ul>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295084 Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 Ignore the specific example that you were linked to, since it's a more advanced tactic than you can handle at the moment. The example from the top of the page is easier to follow, look: while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } You don't need the $row[] syntax or your inner foreach. Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295168 Share on other sites More sharing options...
raptor30506090 Posted December 7, 2011 Author Share Posted December 7, 2011 :'( mm sorry cant see how that is going to help im trying to make a drop down dymanic menu if i do two querys how do i compare the id fields? Many thanks Daz Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295201 Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 Because this is wrong: while($row[] = mysql_fetch_array($query)){ foreach($row as $category){ And this is right: while ($row = mysql_fetch_assoc($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295256 Share on other sites More sharing options...
raptor30506090 Posted December 8, 2011 Author Share Posted December 8, 2011 Still having probs with this any more help would be great this works but when i try to get it from database thats when it go's wrong. <?php $conn = mysql_connect("localhost","root",""); $db = mysql_select_db("array", $conn); // array.categorys $categorys = array( array('cat_id'=>1,'category'=>'Home'), array('cat_id'=>2,'category'=>'About'), array('cat_id'=>3,'category'=>'Services'), array('cat_id'=>4,'category'=>'Contact') ); // array.sub_categorys $sub_categorys = array( array('sub_cat_id'=>1,'cat_id'=>3,'sub_category'=>'our history'), array('sub_cat_id'=>2,'cat_id'=>3,'sub_category'=>'why we started'), array('sub_cat_id'=>3,'cat_id'=>3,'sub_category'=>'about the company'), array('sub_cat_id'=>4,'cat_id'=>3,'sub_category'=>'our staff'), array('sub_cat_id'=>5,'cat_id'=>3,'sub_category'=>'plastering'), array('sub_cat_id'=>6,'cat_id'=>3,'sub_category'=>'plumbing'), array('sub_cat_id'=>7,'cat_id'=>3,'sub_category'=>'company policy') ); echo "<ul>"; foreach($categorys as $category){ echo "<li>"; echo $category['category']; foreach($sub_categorys as $sub_category){ if($category['cat_id'] == $sub_category["cat_id"]){ echo "<ul><li>".$sub_category["sub_category"]."</li></ul>"; } } echo "</li>"; } echo "</ul>"; ?> Database side this is how fare i have got <?php $sql = "SELECT * FROM categorys INNER JOIN sub_categorys ON categorys.cat_id = sub_categorys.cat_id ORDER BY categorys.cat_id, sub_categorys.sub_cat_id"; $query = mysql_query($sql) or die(mysql_error()." $sql");$Category = ''; echo "<ul>"; while($row = mysql_fetch_array($query)){ if ($row['category'] != $Category){ if ($Category != ''){ echo "</ul>"; } $Category = $row['category']; echo "<li>" . $row['category'] . "</li>"; echo "<ul>";} echo "<li>".$row['sub_category']."</li>";} if ($Category != ''){ echo "</ul>";} echo "</ul>"; ?> 17163_.gz Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295663 Share on other sites More sharing options...
ManiacDan Posted December 8, 2011 Share Posted December 8, 2011 Please indent your code properly and put braces on their own lines so we can see where your control structures begin and end. what does the result set look like for this sql query? Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295755 Share on other sites More sharing options...
raptor30506090 Posted December 8, 2011 Author Share Posted December 8, 2011 Hi the problem is that i want to show all the menu as well as sub menu this will show all if i put sub menu to each. Home submenu About submenu but id i dont add a submenu then you cant see the above Home, About, Contact Thank you for any help Darren Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295821 Share on other sites More sharing options...
ManiacDan Posted December 8, 2011 Share Posted December 8, 2011 Then maybe you want a left join. Or you could provide the two things I asked for last time. I'm going to go ahead and ignore this thread from now on. It really isn't that hard to copy/paste the results of your query and I'm not getting paid to beg you for the information I need to help you. Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295843 Share on other sites More sharing options...
raptor30506090 Posted December 9, 2011 Author Share Posted December 9, 2011 Hi Guys Any ideas to get this dusted Thanks Daz Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1296114 Share on other sites More sharing options...
Pikachu2000 Posted December 9, 2011 Share Posted December 9, 2011 Please indent your code properly and put braces on their own lines so we can see where your control structures begin and end. what does the result set look like for this sql query? Quote Link to comment https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1296342 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.