Jump to content

foreach


raptor30506090

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/
Share on other sites

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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295053
Share on other sites

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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295057
Share on other sites

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>";
		  
?>


Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295084
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295168
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295663
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295821
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/252606-foreach/#findComment-1295843
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.