Menu
Hi all,
Cheers for reading my post. I’ve got a list of categories in my main database table, and any of these can be related categories of another (1 to many).
So, i’ve got 2 tables:
categoryID
categoryName
subCategoryID
categoryID
relatedCategoryID
and i would need to output as:
category 1
– category 2
– category 3
category 2
– category 6
– category 8
I’ve tried a few SQL queries, but i can’t seem to get the right output.
Can anyone please provide help or guidance?
Thanks. Martin.
[code=php]
$sql = "
SELECT
c1.categoryName,
IFNULL(c2.categoryName, '')
FROM
Categories AS c1,
LEFT JOIN subCategory AS sc ON sc.categoryID = c1.categoryID,
LEFT JOIN Categories AS c2 ON c2.categoryID = sc.subCategoryID
GROUP BY c1.categoryID
ORDER BY c1.categoryName, c2.categoryName
";
$result = mysql_query($sql) or die(mysql_error());
$category = "";
while($row = mysql_fetch_row($result))
{
if($row[0] != $category)
{
$category = $row[0];
echo $row[0] . "<br />n";
}
if(!empty($row[1]))
{
echo "- " . $row[1] . "<br />n";
}
}
[/code]
<i>
</i> Categories AS c1,
LEFT JOIN subCategory AS sc ON sc.categoryID = c1.categoryID,
<i>
</i>LEFT JOIN Categories AS c2 ON c2.categoryID = sc.subCategoryID
...with...<i>
</i>LEFT JOIN Categories AS c2 ON c2.categoryID = sc.relatedCategoryID
[code=php]
$sql = "
SELECT
c1.categoryName,
IFNULL(c2.categoryName, '') AS subName
FROM
Categories AS c1
LEFT JOIN SubCategory AS sc ON sc.categoryID = c1.categoryID
LEFT JOIN Categories as c2 ON sc.relatedCategoryID = c2.categoryID
WHERE c1.categoryID NOT IN (SELECT relatedCategoryID FROM SubCategory)
ORDER BY c1.categoryName, c2.categoryName
";
$result = mysql_query($sql) or die(mysql_error());
$category = '';
while($row = mysql_fetch_row($result))
{
if($row[0] != $category)
{
$category = $row[0];
echo $row[0]."<br />n";
}
if($row[1] != '')
{
echo "- ".$row[1]."<br />n";
}
}
[/code]
<i>
</i>Top A
- Sub A 1
- Sub A 2
Top B
- Sub B 1
- Sub B 2
Top C
<i>
</i>-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 01, 2010 at 08:20 PM
-- Server version: 5.1.36
-- PHP Version: 5.2.9-2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: <span><code>tester</code></span>
--
-- --------------------------------------------------------
--
-- Table structure for table <span><code>categories</code></span>
--
CREATE TABLE IF NOT EXISTS <span><code>categories</code></span> (
<span><code>categoryID</code></span> int(11) NOT NULL AUTO_INCREMENT,
<span><code>categoryName</code></span> varchar(255) NOT NULL,
PRIMARY KEY (<span><code>categoryID</code></span>)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data for table <span><code>categories</code></span>
--
INSERT INTO <span><code>categories</code></span> (<span><code>categoryID</code></span>, <span><code>categoryName</code></span>) VALUES
(1, 'Top A'),
(2, 'Top B'),
(3, 'Sub A 1'),
(4, 'Sub A 2'),
(5, 'Sub B 1'),
(6, 'Sub B 2'),
(7, 'Top C');
-- --------------------------------------------------------
--
-- Table structure for table <span><code>subcategory</code></span>
--
CREATE TABLE IF NOT EXISTS <span><code>subcategory</code></span> (
<span><code>subCategoryID</code></span> int(11) NOT NULL AUTO_INCREMENT,
<span><code>categoryID</code></span> int(11) NOT NULL,
<span><code>relatedCategoryID</code></span> int(11) NOT NULL,
PRIMARY KEY (<span><code>subCategoryID</code></span>),
KEY <span><code>categoryID</code></span> (<span><code>categoryID</code></span>,<span><code>relatedCategoryID</code></span>)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table <span><code>subcategory</code></span>
--
INSERT INTO <span><code>subcategory</code></span> (<span><code>subCategoryID</code></span>, <span><code>categoryID</code></span>, <span><code>relatedCategoryID</code></span>) VALUES
(1, 1, 3),
(2, 1, 4),
(3, 2, 5),
(4, 2, 6);
Martin.
That's absolutely fine, you've most certainly put me in the right direction with this. Thanks for all your help, it's nice to see people who go out of their way to help others. I'll pay it forward when i can on these forums.
Cheers. Martin.[/QUOTE]
[CODE]<categories>
- <category>
<title>Work</title>
<related>
- <opt>
<id>Physical Health</id>
<title>Physical Health</title>
<image>resources/images/saheph.jpg</image>
</opt>
[B] // Should be here[/B]
</related>
</category>
- [B]<opt>
<id>Relationships</id>
<title>Relationships</title>
<image />
</opt>[/B]
</categories>[/CODE]
[code=php]<?php
header("Content-type: text/xml");
$xml_output = "<?xml version="1.0"?>n";
$xml_output .= "<categories>n";
dbConnect();
$result = mysql_query("SELECT cat.categoryName AS categoryName, subcat.categoryName AS subcategoryName
FROM category AS cat
LEFT OUTER
JOIN subCategory ON subCategory.parentID = cat.categoryID
LEFT OUTER
JOIN category AS subcat ON subcat.categoryID = subCategory.categoryID
ORDER BY cat.categoryName, subcat.categoryName");
$previousCategory = "";
$previousSubCategory = ""; [B]// Added this variable to check[/B]
while($row = mysql_fetch_assoc($result))
{
if ($row['categoryName'] != $previousCategory) {
$xml_output .= "<category>";
$xml_output .= "<title>" . $row['categoryName'] . "</title>";
$xml_output .= "<description><![CDATA[" . $row['categoryDescription'] . "]]></description>";
$xml_output .= "<image>" . $row['categoryImage'] . "</image>";
$xml_output .= "<ymal>";
}
if ($row['subcategoryName'] != $previousSubCategory) {
$xml_output .= "<opt>";
$xml_output .= "<id>" . $row['subcategoryName'] . "</id>";
$xml_output .= "<title>" . $row['subcategoryName'] . "</title>";
$xml_output .= "<image>" . $row['subcategoryImage'] . "</image>";
$xml_output .= "</opt>";
}
if ($row['categoryName'] != $previousCategory)
{
$xml_output .= "</ymal>";
$xml_output .= "</category>";
}
$previousCategory = $row['categoryName'];
$previousSubCategory = $row['subcategoryName'];
}
dbClose();
$xml_output .= "</categories>";
echo $xml_output;
?> [/code]
0.1.9 — BETA 5.17