How can i check sql , and use something like do case - php

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= "....";
}
?>

Related

PHP - show category once per group

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

PHP: Category and subcategories from the same table and row?

I have a table that looks like this:
id di_make di_model
1 Samsung TV656
2 Samsung TV555
3 Sony LCD33
I need to create a category and subcategories menu from this table...
LIKE THIS:
http://jsfiddle.net/7NYhe/500/
So i tried this:
$storecategories = "";
$storesubcategories = "";
$sql2="SELECT DISTINCT `di_make` FROM `products` ORDER BY id";
$query2 = mysqli_query($db_conx, $sql2);
$existCount2 = mysqli_num_rows($query2);
if ($existCount2!=0) {
while($row2 = mysqli_fetch_array($query2, MYSQLI_ASSOC)){
$di_makeC = $row2["di_make"];
/////Create sub categories/////////
$sql3="SELECT * FROM `products` WHERE di_make='$di_makeC'";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {
while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){
$di_modelC = $row3["di_model"];
$storesubcategories .='<li>'.$di_modelC.'</li>';
}
}
/////End of Create sub categories/////////
$storecategories .= '<li class="dropdown">'.$di_makeC.'
<ul>
'.$storesubcategories.'
</ul>
</li>';
}
}
However, the code above acts weirdly!
By that I mean, it will create the Categories and then create the subcategories BUT it will repeat the subcategories from the second category.
Like this:
http://jsfiddle.net/7NYhe/501/
Could someone please advice on this issue?
Thanks in advance.

trying to form SQL join on several tables

So Im trying to figure out how to do this with 1 query if possible. Here is what I have with several queries
$file_url = 'http://'.$cms.'/filemanager/gallery';
$link_url = '/our-process/gallery/';
include 'inc/config.php';
function gallerySafeName($var){
$var = strtolower(preg_replace('([^a-zA-Z0-9_])','-',$var));
return $var;
}
$galleryimages = array();
$sql = "SELECT * FROM `cms_gallery_categories` ORDER BY RAND() LIMIT 3";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)){
$sql1 = "SELECT * FROM `cms_gallery_images` WHERE `image_id` = ".$row['category_image_id'];
$result1 = mysqli_query($con,$sql1);
while ($row1 = mysqli_fetch_array($result1)){
$sql2 = "SELECT * FROM `cms_gallery` WHERE `gallery_id` = ".$row1['gallery_id'];
$result2 = mysqli_query($con,$sql2);
while ($row2 = mysqli_fetch_array($result2)){
print '<li>
<a href="'.$link_url.$row['category_name'].'/'.$row2['gallery_name'].'/'.gallerySafeName($row1['image_caption']).'/">
<img width="210" height="133" border="0" src="'.$file_url.'/'.$row['category_name'].'/'.$row2['gallery_name'].'/'.$row1['image_name'].'" />
</a>
</li>';
}
}
}
Im trying to get 3 random entries from the gallery category and the image assigned to represent that category. Then get the gallery name and then the image name to form a img src and link. My table structure is as follows
-cms_gallery_categories
category_id
category_title
category_name
category_image_id
-cms_gallery
gallery_id
gallery_title
gallery_name
category_id
-cms_gallery_images
image_id
image_name
image_caption
gallery_id
Images belong to galleries, and galleries belong to categories
SELECT GC.*, G.*, I.*
FROM cms_gallery_categories GC
INNER JOIN cms_galleries G ON (G.category_id = GC.category_id)
INNER JOIN cms_gallery_images I ON (I.gallery_id = G.gallery_id)
ORDER BY RAND() LIMIT 3
You will want to limit your fields to what you need only. From what I can see in your code, it's this:
SELECT GC.category_name, G.gallery_name, I.image_caption FROM...
More info
http://www.w3schools.com/sql/sql_join.asp
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
http://www.tutorialspoint.com/sql/sql-using-joins.htm

Selecting one image out of 5 images for one item

I have tried to create a script that loops through 5 items that match 1 item and then show one image associated with each of the 5 items.
Here is a fiddle I create to illustrate my issue.
http://sqlfiddle.com/#!2/17d66/1/0
I want to select one picture from each matching item. Here is the code I have come up with but so far it is not working. There are two points of comparison I have used, a tag and family. The tag associated with the type of product and the family is the category it belongs to. For instance, the iPhone 4 would have smartphone for a tag and iPhone 4 as the family. Any help would be appreciated, my code is below.
<?php
// main product
$images = "SELECT img_id FROM product_images WHERE img_tag = '$p_type' && img_family = '$p_family' LIMIT 5";
$img_list = mysql_query($images);
while($rows = mysql_fetch_array($img_list)) {
$img_name = $rows['img_name'];
$img_tag = $rows['img_tag'];
$img_family = $rows['img_family'];
$img_id = $rows['img_id'];
//echo $img_name;
<div>
<img src="view.php?h=400&w=400&imgid=<?php echo $img_id; ?>" />
</div>
}
</div>
</div>
<div id="left_other">
<div class="titleHeaders">Other matching items:</div>
<?php
$match_items = "SELECT id,product_name,product_type FROM product_list WHERE name_family = '$p_family' AND id <> '$id' LIMIT 5";
$match_q = mysql_query($match_items);
$count = mysql_num_rows($match_q);
if($count > 0) {
//get matching products
while($items = mysql_fetch_array($match_q)) {
$id = ($items['id']);
echo "<p>$id</p>";
$p_name = ($items['product_name']);
echo "<p>$p_name</p>";
$type = ($items['product_type']);
echo "<p>$type</p>";
$full = "$p_name $type";
echo "<p>$full</p>";
//get photos
$other_photos = "SELECT * FROM product_images WHERE img_family = '$p_family' AND img_tag <> '$p_type' LIMIT 1";
echo $other_photos;
$other_q = mysql_query($other_photos);
while($pictures = mysql_fetch_array($other_q)) {
echo "<p>----------------------</p>";
$theid = ($pictures['img_id']);
echo "<p>$theid</p>";
$photo = ($pictures['img_name']);
echo "<p>$photo</p>";
$thetype = ($pictures['img_tag']);
echo "<p>$thetype</p>";
echo "<p>----------------------</p>";
if($photo != $newname && $theid != $newid){
if ($photo == '') {
$poly = 'images/pph.jpg';
//echo $poly;
}else{
$poly = 'product_images/regular_images/'.$photo;
//echo $poly;
} // end if
$newname = $photo;
$newid = $theid;
}else{
echo "<p>The photo name is the same!</p>";
}
You can use a single query to grab the results. Here is how you can do it.
SELECT
pl.id,
pl.product_name,
pl.product_type ,
pi.product_images
FROM product_list AS pl
LEFT JOIN (SELECT product_list_id , product_images FROM product_images LIMIT 1) AS `pi` ON pi.product_list_id = pl.id
WHERE pl.img_tag = '$p_type' AND pl.img_family = '$p_family'
LIMIT 5
But it would be batter if there was some table structure , sample data and desired output.
EDIT
Try this query and you can put a where at last
SELECT
*
FROM
`products` AS p
LEFT JOIN (SELECT MAX(img_id), img_name, img_tag, img_family FROM `images`) AS i ON i.img_family = p.`p_family` AND i.img_tag = p.`p_tag` ;
EDIT
SELECT *
FROM `products` AS p
LEFT JOIN images as i
ON i.img_family = p.`p_family`
group by p.p_name;

Running two queries for each individual thing

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>';
}

Categories