Jump to content

join question


raptor30506090

Recommended Posts

ok im trying to make a menu but keep having same problem is it the query ? any help would be great thanks

 


<?php 
$query = mysql_query("SELECT * FROM categorys INNER JOIN sub_categorys ON categorys.cat_id =  sub_categorys.cat_id");

while($row = mysql_fetch_array($query)){


echo "<ul>";
echo "<li>" . $row['category'] . "</li>";
echo "<ul>";
echo "<li>";
echo $row['sub_category'];
echo "</li>";
echo "</ul>";
echo "</ul>";

}
?> 



Link to comment
Share on other sites

You need to build up a list for each of the categories and then output it because you want all of the subcategories to be listed under it.

 

When you go through the query results either:

a - cycle and load all values into an array based on $array [ $row [ 'category' ] ] [] = $row [ 'sub_category' ]; then cycle through $array

b - keep track of the current category you are in and if it changes then emit the <ul> stuff

 

As for if the query is the issue, try it in something like phpMyAdmin to see.

 

Hope this helps.

 

~awjudd

Link to comment
Share on other sites

Hi

 

You want to create a new <ul> set of tags when the category changes, and close the previous set if they existed.

 

Something like this (not tested so excuse typos).

 

<?php 
$Category = '';
$query = mysql_query("SELECT * FROM categorys INNER JOIN sub_categorys ON categorys.cat_id =  sub_categorys.cat_id");

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

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

 

<?php

$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'=>1,'sub_category'=>'our history'),
  array('sub_cat_id'=>2,'cat_id'=>2,'sub_category'=>'why we started'),
  array('sub_cat_id'=>3,'cat_id'=>4,'sub_category'=>'about the company'),
  array('sub_cat_id'=>4,'cat_id'=>3,'sub_category'=>'our staff'),
  array('sub_cat_id'=>5,'cat_id'=>2,'sub_category'=>'plastering'),
  array('sub_cat_id'=>6,'cat_id'=>3,'sub_category'=>'plumbing'),
  array('sub_cat_id'=>7,'cat_id'=>1,'sub_category'=>'company policy')
);
?>

 

thanks

Link to comment
Share on other sites

Hi

 

Assuming the arrays data should have been in mysql tables then this does it for you.

 

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

?>

 

All the best

 

Keith

Link to comment
Share on other sites

Thanks Keith

 

ok the problem now is yes it brings up

 

•Services

    ◦our history

    ◦why we started

    ◦about the company

    ◦our staff

    ◦plastering

    ◦plumbing

    ◦company policy 

 

but nothing else no Home About or Contact

 

 

 

This works correct but dont now when connect to db to get the data

 




<?php

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


?>

Link to comment
Share on other sites

Hi

 

Can you do an actual export of the data.

 

The code I posted did work when I set up a copy of what I believe the be the table layouts using the data you supplied in the arrays.

 

You code won't work properly at the moment as each sub category has its own unordered list.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.