I want to create a menu from category names in database, so far I have this:
$list = "SELECT category FROM posts";
$rlist = mysql_query($list) or die(mysql_error());
while($rows = mysql_fetch_assoc($rlist))
{
$catname = $rows['category'];
echo '<li>' . $catname . '</li>';
}
Which lists everything but I need to make each only list once so its a menu.
Maybe you want
SELECT DISTINCT category FROM posts;
And assuming you want them ordered
SELECT DISTINCT category FROM posts ORDER BY category ;
If you want your category to be only listed once, you chould change your query to
SELECT DISTINCT(category) FROM POSTS
Related
I have 2 tables. The first one, pages, contains a foreign key category_id and the other table, categories, contains an id and a title field.
Here is my sql query :
SELECT categories.title as titleCategory, pages.title as titlePage FROM categories INNER JOIN pages ON pages.category_id = categories.id ORDER BY categories.title
What is want is to display the pages and categories like this :
category 1
page 1
page 2
category 2
page 1
etc...
Is there a way to do that using a foreach or do I have to use 2 queries?
Thank you very much for your help.
Assuming PHP, but the logic shoudl work in any language.
$cur_cat = null;
foreach($results as $result){
if($cur_cat != $result['titleCategory']){
echo $result['titleCategory'];
}
$cur_cat = $result['titleCategory'];
echo $result['titlePage'];
}
Just put a check in the single foreach loop.
$currentCategory = null;
foreach( $results as $result ) {
if( $currentCategory != $result['titleCategory'] ) {
$currentCategory = $result['titleCategory'];
echo $result['titleCategory'];
}
echo $result['titlePage'];
}
This will only print out the title if it has changed. You already have the, ordered by categoryTitle so if the category doesn't change it will only print the pageTitle.
I have a category tree that contains up to 3 levels of children-categories, like this:
houseproducts->livingroom->sofas->twoseats
houseproducts->livingroom->sofas->threeseats
houseproducts->livingroom->sofas->fourseats
So for each sublevel, I do a SELECT based on the mothers category-id. This is done in a PHP-loop like the code below, but I guess it could be done in one single Mysql-query, for better performance. I have tried different JOINS but find it really difficualt. Any advice will be highly appreciated.
function build_category_tree()
{
$cat = array();
// main category loop
$r1 = mysql_query("SELECT cat_id,cat_name FROM categories WHERE cat_mother=0 OR cat_mother='' ORDER BY cat_name");
while ($row=mysql_fetch_assoc($r1))
{
$cat[$row['cat_id']] = $row['cat_name'];
// check for subcategories
$r2 = mysql_query("SELECT cat_id,cat_name FROM categories WHERE cat_mother='".$row['cat_id']."'");
while ($subrow=mysql_fetch_assoc($r2))
{
$cat[$subrow['cat_id']] = ' - '.$subrow['cat_name'];
// check if there is subcats for the current subcategory
$r3 = mysql_query("SELECT cat_id,cat_name FROM categories WHERE cat_mother='".$subrow['cat_id']."'");
while ($subrow2=mysql_fetch_assoc($r3))
{
$cat[$subrow2['cat_id']] = ' -- '.$subrow2['cat_name'];
// check if there is subcats for the current subcategory
$r4 = mysql_query("SELECT cat_id,cat_name FROM categories WHERE cat_mother='".$subrow2['cat_id']."'");
while ($subrow3=mysql_fetch_assoc($r4))
{
$cat[$subrow3['cat_id']] = ' --- '.$subrow3['cat_name'];
}
}
}
}
return $cat;
}
I would read your entire table into an array and segment that array by the mother's keys
Try this:
SELECT l1.cat_id AS l1_cat_id
,l1.cat_name AS l1_cat_name
,l2.cat_id AS l2_cat_id
,l2.cat_name AS l2_cat_name
,l3.cat_id AS l3_cat_id
,l3.cat_name AS l3_cat_name
,l4.cat_id AS l4_cat_id
,l4.cat_name AS l4_cat_name
FROM categories AS l1
JOIN categories AS l2
ON l2.cat_mother = l1.cat_id
JOIN categories AS l3
ON l3.cat_mother = l2.cat_id
JOIN categories AS l4
ON l4.cat_mother = l3.cat_id
WHERE l1.cat_mother=0 OR l1.cat_mother=''
ORDER BY l1_cat_name, l2_cat_name, l3_cat_name, l4_cat_name
<?php
$q = mysql_query("SELECT sub_cat.*, links.*
FROM links
LEFT JOIN sub_cat
ON links.p_id = sub_cat.id
WHERE sub_cat.p_id = '$id'
ORDER BY name ASC") or die (mysql_error());
while ($r = mysql_fetch_assoc($q))
{
$links_name = $r['name'];
$link_h3 = $links_name != '' ? '<h3>' . $links_name . '</h3>' : '';
//print $link_h3;
print '<pre>';
print_r ($r);
}
?>
I have two tables with rows like:
sub_cat
id
name
p_id
links
id
links
p_id
In sub cat i have movie categories, like foreign language movies, national movies, uncategorised movies and so on. In links table i have concrete movie links and depending on sub category.
The only thing is that i do not want dublicate titles (sub_cat.name).
result is:
Without Category www.moviesite.com
Without Category www.moviesite2.com
Without Category www.moviesite3.com
Foreign Movies www.moviesite1.bla
Foreign Movies www.moviesite2.bla
I want to be
Without Category www.moviesite.com
www.moviesite2.com
www.moviesite3.com
Foreign Movies www.moviesite1.bla
www.moviesite2.bla
and do not have any idea how to do this :(
any help appreciated.
To do the job, you have 2 solutions:
The first solution is to process your data before showing it, in order to group all movies by category.
You can do for example:
$moviesByCategory = array();
while ($r = mysql_fetch_assoc($q))
{
// Create the new sub array for the new category if necessary
if (!isset($moviesByCategory[$r['name']]))
$moviesByCategory[$r['name']] = array();
// Add the movie in the category
$moviesByCategory[$r['name']][] = $r['links'];
}
And then, you can now iterate on this new array like
foreach($moviesByCategory as $category => $movies)
{
// Print the category name
echo '<h1>' . $category . '</h1>';
// Print all movies of the category
foreach($movies as $movie)
echo '<h3>' . $movie . '</h3>';
}
The second solution is to modify the SQL query to group directly all movies that have the same category. You just have to use a GROUP BY clause on sub_cat.id and then apply an agregate function on all other fields in the select.
For performance aspect, the best solution is the SQL solution, but doing it with PHP will give you more flexibility for the presentation.
Try something like:
$lastSubcatName = "";
while ($r = mysql_fetch_assoc($q))
{
$links_name = $r['name'];
if($lastSubcatName != $links_name)
{
echo "<h1>".$links_name."</h1>";
$lastSubcatName = $links_name;
}
echo '<h3>' . $r['links'] . '</h3>';
}
the lists are for "Categories" and "Sub-Categories" i want if the user select the first category then the second list will display all the sub-categories that related to the selected category, am using PHP and MySQL,
this is my fisrt list code:
<?php
require("connect.php");
$extract = mysql_query("SELECT * FROM categories ORDER BY catg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$catgname = $row['catg_name'];
$id = $row['catg_code'];
echo "<option value='$id' onClick='$var=$id' > $catgname </option> ";
}
?>
this is my second list code:
<?php
require("connect.php");
//$select4 = $_POST['select2'];
$extract = mysql_query("SELECT * FROM subcategories WHERE catg_code='$var' ORDER BY subCatg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$scatgname = $row['subCatg_name'];
$sid = $row['subCatg_code'];
echo "<option name='mm' value='$sid'> $scatgname </option> ";
}
?>
the tables in database includes (categories, subcategories and products) category includes (catg_code and catg_name)
subCategories table include (subcatg_code, subCatg_name and catg_code)
You need to know how about primary and foreign keys.
If you have a reference to the main category from your subcategory table you would only need to run the query once. Have a read up on join queries such as inner join and outer join there are plenty of tutorials out there (Google mysql joins).
forumcats: id, name
forums: id, name, cat_id
How can i join these together and print the forums assigned to the categories under them
Would really appriciate if someone could give me a hand
This is what i mean:
A category
A forum<
A forum
A 2nd category
Another forum
Another forum
Another forum
A 3rd catagory
Another forum
Another forum
Another forum
$reslt = mysql_query("select id, name, cat_id from forums");
while ($row=mysql_fetch_assoc($reslt)) {
echo "<h1>Category here</h1<";
echo "<h3>$row[name]</h3>";
}
You want this query:
SELECT forums.*, forumcats.name AS category_name
FROM forums
INNER JOIN forumcats ON (forums.cat_id = forumcats.cat_id)
Then when you loop through the results, you want to identify when you've moved on to a new category. For example:
$last_cat_id = null;
while ($row=mysql_fetch_assoc($reslt)) {
if ($last_cat_id != $row['cat_id']) {
echo '<h1>' . $row['category_name'] . '</h1>';
$last_cat_id = $row['cat_id'];
}
echo "<h3>$row[name]</h3>";
}