Categories, whilst only displaying the category title once. I have one table that holds the category title (the parent) and the second table which holds the sub-category information. Although when displaying the information, I dont know how to show the category information only once.
$result_array = mysqli_query($connect, "SELECT *
FROM tbl_customer, categories
WHERE tbl_customer.category_QA = categories.id
ORDER BY Category ASC");
while ($data = mysqli_fetch_array($result_array)) {
echo $data["Category"]; //display once for every new category
echo $data["productName"];
}
You could check for change
$result_array = mysqli_query($connect,"SELECT * FROM tbl_customer, categories WHERE tbl_customer.category_QA = categories.id ORDER BY Category ASC");
$checkCategory = '';
while($data = mysqli_fetch_array($result_array)){
if ($checkCategory != $data["Category"]){
echo $data["Category"]; //display once for every new category
$checkCategory = $data["Category"];
}
echo $data["productName"];
}
Related
I have two tables; One is category and the other one is sub_category.
First I want to display all the items in category and store the value of each category and then display all the sub_categories related to that category but the category should not repeat (it should be displayed only once for all sub-categories).
.
<?php
$abcd = $mysqli->query("SELECT * FROM categories;");
while($obj = $abcd->fetch_object())
{
$category = $obj->name;
$results = $mysqli->query("SELECT * FROM `sub-category` WHERE category = '$category';");
while($omg=$results->fetch_object())
{
echo $category. ' '.$omg->subcategory.'<br>';
}
echo $omg->subcategory;
}
?>
Create an array of subcats and implode them so you only get commas where you want them. Only print the $category outside the loop.
<?php
$abcd = $mysqli->query("SELECT * FROM categories ");
while ($obj = $abcd->fetch_object()) {
$category = $obj->name;
$results = $mysqli->query("SELECT * FROM `sub-category` WHERE category = '$category' ");
echo $category.': ';
$subcats = array();
while ($omg = $results->fetch_object()) {
$subcats[] = $omg->subcategory;
}
echo implode(',', $subcats).'<br>';
}
?>
Use a simple JOIN.
<?php
$abcd = $mysqli->query('SELECT DISTINCT categories.name,sub-category.subcategory FROM `categories` JOIN `sub-category` ON category.name = categories.category;');
while($obj = $abcd->fetch_object())
{
echo $obj->name.' '.$obj->subcategory.'<br>';
}
?>
I am listing the categories and when clicking category it has to show if there is subcategory . If there isnt subcategory it includes products and have to list products. I mean if includes product case b, else case a
I can use query a or b, But i dont know how to check if have products so use query a else use query b.
Sql :
Categories : id | parent_id | name
product to category : id | product_id | category_id
query a (category list):
<?php
$query_product = mysql_query("SELECT * FROM categories WHERE status = '1' AND parent_id = '".$row['id']."' ORDER BY sort_order");
while($row_product = mysql_fetch_array($query_product)){
$product_name = $row_product['name'];
}
?>
query b (product list):
$query_product_id = mysql_query("SELECT * FROM product_to_category WHERE category_id = '".$row['id']."'");
while($row_product_id = mysql_fetch_array($query_product_id)){
//sort order yapilacak.
$query_product = mysql_query("SELECT * FROM products WHERE status = '1' AND id = '".$row_product_id['product_id']."' ORDER BY sort_order");
$row_product = mysql_fetch_array($query_product);
$product_name = $row_product['name'];
}
?>
Sorry for not telling my question clearly.I found a solution like this but couldnt integrate. I can check if there are products.
I have to use a variable like $code includes
<div id="container-portfolio" class="portfolio-4">
<?php
$query_product = mysql_query("SELECT * FROM categories WHERE status = '1' AND parent_id = '".$row['id']."' ORDER BY sort_order");
while($row_product = mysql_fetch_array($query_product)){
if($language != 'TR'){
$query_product_lang = mysql_query("SELECT image FROM categories WHERE language = '".$language."' AND language_parent_id = '".$row_product['id']."'");
$row_product_lang = mysql_fetch_array($query_product_lang);
$product_name = $row_product_lang['name'];
} else {
$product_name = $row_product['name'];
}
?>
and $link
<a href="<?php echo get('site_link'); ?>c/<?php echo $row_product['link']; ?>" >
<img src="<?php echo HTTP_RESIM.'categories/'.$row_product['image']; ?>" alt="" /></a>
Then use the if statement like below. If you can help me for editing variables, i could use the others i think. Getting errors converting html to php for echo code.
Regards
<?php $sql = mysql_query("SELECT * FROM product_to_category WHERE category_id = '".$row['id']."'");
$urun = mysql_num_rows($sql);
if($urun > 0) {
$code= "......";
$link= "....";
} else {
$code= "......";
$link= "....";
}
?>
I tried to get articles titles where category id = 8.
here is the code i used.
$catID = 8;
$db = JFactory::getDBO();
$db->setQuery("SELECT title FROM #__content WHERE catid = ".$catID);
$catDesc = $db->loadResult();
echo $catDesc;
it display only one title which category id=8.
i want to display all the titles which category id = 8.
Update your code as below:
$catID = 8;
$db = JFactory::getDBO();
$db->setQuery("SELECT title FROM #__content WHERE catid = ".$catID);
$catDesc = $db->loadObjectList();
var_dump($catDesc);
Use loadResult() when you expect just a single value back from your database query.
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query.
I have two tables
First one is categories and second one is products.
Categories table contains cat_id, cat_name, Cat_desc.
Products table contains product_id, product_name, Product_desc and cat_id
I want to display all categories as heading and list only 4 related products under each category heading.
For Eg:
Category1
Product 1, Product 2, product 3
Category 2
Product 4, product 5
How can I script this. I am newbie pls help
OK i think this is ok with your question. try this...
$sql = "SELECT cat_id, cat_name FROM categories ";
$query = mysql_query($sql);
while($result = mysql_fetch_array($query)){
echo $result['cat_name'];
$sql1 = "SELECT * FROM products WHERE cat_id=".$result['cat_id'];
$query1 = mysql_query($sql1);
while($row = mysql_fetch_array($query1)){
echo $row['product_name'];
}
}
AND refer this http://php.net/manual/en/book.mysql.php
With the help of Nytram's post, I just alter the code. So this answer is 99% owes to Nytram.
$sql = "SELECT cat_id, cat_name FROM categories ";
$query = mysql_query($sql);
while($result = mysql_fetch_array($query)){
echo $result['cat_name'];
And altered line is:
$sql1 = "SELECT * FROM products WHERE cat_id='".$result['cat_id']."' LIMIT 0,10";
Then use the same code
$query1 = mysql_query($sql1);
while($row = mysql_fetch_array($query1)){
echo $row['product_name'];
}
}
Try this ::
Select * from
categories c left join products p on (p.catId = c.catId)
order by c.catId
You can refer to this tutorial
You need to do a recursive function for this
i call this category tree
http://codeassembly.com/How-to-display-inifinit-depth-expandable-categories-using-php-and-javascript/
Sorry about the lame title, but I really don't know how to explain it!
Basically, I want to query the game categories from a table, then query games from another table which equal the category of the category queried from the categories table.
Got me so far?
This is the code I have so far...
$get_categories_query = mysql_query("
SELECT *
FROM game_categories
ORDER BY category_name ASC");
while ($row = mysql_fetch_array($get_categories_query)) {
$category_name = $row['category_name'];
$get_category_games = mysql_query("
SELECT *
FROM games
WHERE category = '$category_name'
ORDER BY RAND() LIMIT 5");
while ($row = mysql_fetch_array($get_category_games)) {
$category_game_id = $row['id'];
$category_game_title = $row['game_title'];
$category_game_display .= '<li><img
class="category_module_img"
src="http://www.game.assets.buddyweb.me/' .
$category_game_id .
'/_thumb_100x100.png"></li>';
}
$category_display .= '<div class = "category_module">
<h4>'.$category_name.'</h4>
'.$category_game_display.'
<div class="play_more_btn">More</div>
</div>';
}
But what I get from that is every game appearing from the query from the first category in the list.
I think the problem is the $row variable.
Try this:
$get_categories_query = mysql_query("SELECT * FROM game_categories ORDER BY category_name ASC");
while($row = mysql_fetch_array($get_categories_query))
{
$category_name = $row['category_name'];
$get_category_games = mysql_query("SELECT * FROM games WHERE category = '$category_name' ORDER BY RAND() LIMIT 5");
$category_game_display = false; // new!!
while($row_cat = mysql_fetch_array($get_category_games))
{
$category_game_id = $row_cat['id'];
$category_game_title = $row_cat['game_title'];
$category_game_display .= '<li><img class = "category_module_img" src = "http://www.game.assets.buddyweb.me/'.$category_game_id.'/_thumb_100x100.png"></li>';
}
$category_display .= '<div class = "category_module"><h4>'.$category_name.'</h4>'.$category_game_display.'<div class = "play_more_btn">More</div></div>';
}