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>';
}
Related
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.
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= "....";
}
?>
When I add $elan_category, I get category_id, instead category_title.
Tried to apply "left join" but no success. I have following tables in database:
function getElanDetail()
{
global $con;
if (isset($_GET['elan_id'])) {
$elan_id = $_GET['elan_id'];
$get_elan = "select * from elan where elan_id='$elan_id'";
$run_elan = mysqli_query($con, $get_elan);
while ($row_elan = mysqli_fetch_array($run_elan)) {
$elan_id = $row_elan['elan_id'];
$elan_category = $row_elan['elan_category'];
$elan_title = $row_elan['elan_title'];
$elan_description = $row_elan['elan_description'];
$elan_image = $row_elan['elan_image'];
$elan_contact = $row_elan['elan_contact'];
echo "
$elan_category //Getting ID of category instead Title :(
$elan_title
$elan_description
$elan_image
$elan_contact
";
}
}
}
With join you can do something like:
$elan_id = $_GET['elan_id'];
$get_elan = "SELECT * FROM `elan`
JOIN `categories` ON `categories`.category_id = `elan`.elan_category
WHERE `elan`.elan_id='$elan_id'";
$run_elan = mysqli_query($con, $get_elan);
while ($row_elan=mysqli_fetch_array($run_elan)){
print_r($row_elan);
// see the keys in $row_elan and use them accordingly
}
For subcategories try this query:
SELECT * FROM `elan`
JOIN `categories` ON `categories`.category_id = `elan`.elan_category
JOIN `subcategories` ON `subcategories`.subcategory_id = `elan`.elan_subcategory
WHERE `elan`.elan_id='$elan_id'
$elan_category = $row_elan['elan_category'];
add these two lines after above code
$cat = mysqli_fetch_row(mysqli_query($con,"SELECT category_title FROM categories WHERE category_id = $elan_category"));
$cat_name = $cat[0];
$cat_name is your category name enjoy
Im trying to generate an array but not sure how to go about it.
I'm currently getting my data like so:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test#test.com'");
$row = mysql_fetch_array($query);
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT * FROM usersettings WHERE userId = ".$row['userId']." AND usersettingCategory".$row1['categoryId']." LIKE 'y'");
$isyes = mysql_num_rows($query2);
if($isyes > 0){
$cat1 = mysql_query("SELECT * FROM shops WHERE shopstateId = 1 AND (categoryId1 = ".$row1['categoryId']." OR categoryId2 = ".$row1['categoryId']." OR categoryId3 = ".$row1['categoryId'].")");
$cat1match = mysql_num_rows($cat1);
if($cat1match > 0){
while($cat1shop = mysql_fetch_array($cat1)){
$cat1msg = mysql_query("SELECT * FROM messages WHERE shopId = ".$cat1shop['shopId']." and messagestateId = 1");
while($cat1msgrow = mysql_fetch_array($cat1msg)){
echo $cat1msgrow['messageContent']." - ".$cat1msgrow['messageCode'];
$cat1img = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$cat1shop['shopimageId']);
$imgpath = mysql_fetch_array($cat1img);
echo " - ".$imgpath['shopimagePath']."<br/>";
}
}
}
}
}
But this can cause duplicates when a user has all 3 of a shops categories picked in their preferences. I am trying to find a way to just pull the message ID out instead of the whole thing and put it into an array giving me, for example:
1,3,5,7,1,3,5,2,4,7,8
Then I can just run a separate query to say get me all messages where the ID is in the array, but i am unsure of the most constructive way to build such an array and examples of array from a while loop I have seen do not seem to be what I am looking for.
Is there anyone out there that can push me in the right direction?
Can't help with this code. But if you want an array from a query without duplicate result, you can use " select DISTINCT (id) " in your query or for more simple solution :
$id_arr = array();
$sql = mysql_query("select id from id_table");
while ($id_result = mysql_fetch_array($sql) {
$id = $id_result['id'];
if (!in_array($id, $id_arr)) {
$id_arr[] = $id;
}
}
I have found a much easier way to create the required result. I think at 6am after a hard night coding my brain was fried and I was making things a lot more complicated than I needed to. A simple solution to my issue is as follows:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test2#test2.com'");
$row = mysql_fetch_array($query);
$categories = "(";
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT usersettingCategory".$row1['categoryId']." FROM usersettings WHERE userId = ".$row['userId']);
$row2 = mysql_fetch_array($query2);
if($row2['usersettingCategory'.$row1['categoryId']] == y){
$categories .= $row1['categoryId'].",";
}
}
$categories = substr_replace($categories ,")",-1);
echo $categories."<br />";
$query3 = mysql_query("SELECT * FROM shops,messages WHERE shops.shopId = messages.shopId AND messages.messagestateId = 1 AND (shops.categoryId1 IN $categories OR shops.categoryId2 IN $categories OR shops.categoryId3 IN $categories)");
while($row3 = mysql_fetch_array($query3)){
$query4 = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$row3['shopimageId']);
$row4 = mysql_fetch_array($query4);
echo $row3['messageContent']." - ".$row3['messageCode']." - ".$row4['shopimagePath']."<br />";
}