I am having 5 checkbox with the food items like Chicken, fruits, vegetables, Snacks,.. like that.
What i am trying to implement is when the user selects the checkbox items and clicks submit, it will search for the restaurants providing the selected items,
Table will have 2 fields: restid, menu and the data is stored like this in the table
restid->1, menu->chicken
restid->1, menu->Burger
restid->2, menu->fruits
restid->3, menu->chicken
My doubt is how to search for the restaurants in the table. My plan is, loop through the for loop for each item with the select statement like (select * from restaurant_table where menu='menu';)
when we loop through the loop how can we combine the results for each menu?
Plz help any help will be appreciated
i hope you are having two table restaurant and menu
restaurant - restid, name
menu - menuid, restid, menu
the php code can be like this
<?php
//$_POST['menus'] is the array of checkboxes
foreach($_POST['menus'] as $menu)
{
if($menu) $selected[]=intval($menu);
}
$selectedlist=implode(",",$selected);
//The SQL Query
$query="SELECT name from restaurant WHERE menu in(".$selectedlist.")";
$record=mysql_fetch_array(mysql_query($query)); //Loop this to get more records
?>
Hope this is useful
The query below will return you the restids for restaurants that have both chicken and burger on the menu - assuming that restaurant_table doesn't have duplicate rows.
SELECT restid, COUNT(*) as cnt
FROM restaurant_table
WHERE menu IN ('chicken', 'burger')
GROUP BY restid
HAVING COUNT(*) = 2
Maybe this can help you as starting point:
$where = " 1=1 ";
if ( $_POST["chicken"] )
$where = $where . " AND menu = 'chicken' ";
if ( $_POST["Burger"] )
$where = $where . " AND menu = 'Burger' ";
if ( $_POST["fruits"] )
$where = $where . " AND menu = 'fruits' ";
$sql = "SELECT * FROM restaurants WHERE " + $where;
Related
I have a search form where I can search for my webshop products.
1 product can be in multiple categoris, not just in one. I store this in the termek_katgoria_kapcsolo table. At insert, it creates as many lines, as the product belong to many categoria.
Example: The ID 12 product belong to ID 1, ID 2, ID 3 categoria.
The search sql only look at categoria, when one categoria is selected. Most often, I just search for the products name, I don't sort it to categoris.
How can I write the sql, that if I select a categoria also? I show you the tables on a pic.
if($termek_kategoria == 0 ) // Sort to categoria or not only search for product name, id...
{
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
else
{
// Sorting for categoria also
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' AND termek_kategoria =
'$termek_kategoria' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
Update:
$sql = "
SELECT termek.termek_id, termek.termek_nev, termek.termek_cikkszam, termek.termek_status
termek_kategoria_kapcsolo.*, termek_kategoria.kat_id
FROM termek
LEFT JOIN termek_katgoria_kapcsolo ON termek_kategoria
WHERE termek_kategoria_kapcsolo.kat_kapcs_kategoria_id = termek_kategoria.kat_id
AND termek.termek_id IN (SELECT kat_kapcs_termek_id FROM
termek_kategoria_kapcsolo WHERE kat_kapcs_kategoria_id = '$termek_kategoria')
";
This result:
Whats going wrong here?
What I want is when I select a categoria, the program give me the products, that are in the selected categoria.
I solved the problem:
$sql =
"
SELECT
t.termek_id,
t.termek_nev,
t.termek_cikkszam,
t.termek_status,
kapcs.kat_kapcs_kategoria_id,
kapcs.kat_kapcs_termek_id
FROM termek t
LEFT JOIN termek_katgoria_kapcsolo kapcs ON kapcs.kat_kapcs_kategoria_id = '$termek_kategoria'
WHERE t.termek_id = kapcs.kat_kapcs_termek_id AND t.$kereses_helye LIKE '%$kw%' ORDER BY t.$kereses_rendezes $kereses_sorrend
";
I'm trying to create a count for every option in select option, below is the function I made to do so but I realized all it does it count how many users haven't made a selection. I want it to count how many times each option has been selected in database instead, anyone have any advice?
function teamCount($mysqli){
$count = "SELECT count(selection) FROM users GROUP BY selection HAVING count(*) > 1";
$result = $mysqli->query($count);
$rows = mysqli_fetch_row($result);
echo '('.$rows[0].')';
}
below is table mock up:
username selection
..... Buffalo bills
..... Baltimore ravens
I would like the count to go through the selection column and count duplicate entries in database so it appears as such on front end:
Buffalo Bills [1]
Baltimore Ravens [1]
$count = "SELECT selection, count(*) FROM users GROUP BY selection HAVING count(*) > 1";
you can try this one.
I'm trying to determine whether a combination of topics related to a category is unique. This is done while adding topics to a category. The uniqueness is checked with this query:
$sql_unique = "SELECT Categories_CategoryID, list
FROM (
SELECT Categories_CategoryID, GROUP_CONCAT( Topics_TopicID ) AS list
FROM (
SELECT *
FROM Topics_crosstable
ORDER BY Topics_TopicID
)H
GROUP BY Categories_CategoryID
)A
WHERE list = (
SELECT GROUP_concat( TopicID )
FROM Topics
WHERE Name = 'nr1'
OR Name = 'nr2'
ORDER BY Categories_CategoryID ASC )";
$result = mysql_query($sql_unique);
echo mysql_num_rows($result);
if($result > 1)
{
echo' HELP! It is not unique';
}
else
{
echo ' getshere';
}
This works fine. The problem is however, the number of topics that are added is variant. So is there some way to include a for loop in the where section of the query?
Something like for(number of topics added; nr++) {OR Name =', $inserted_topicName '}
Make use of 'WHERE ... IN':
WHERE Name IN ('nr1','nr2',...)
And
string implode ( string $glue , array $pieces );
Use as follows:
$options = array(); //here's your choices
$sql_unique = "SELECT Categories_CategoryID, list
FROM (
SELECT Categories_CategoryID, GROUP_CONCAT( Topics_TopicID ) AS list
FROM (
SELECT *
FROM Topics_crosstable
ORDER BY Topics_TopicID
)H
GROUP BY Categories_CategoryID
)A
WHERE list = (
SELECT GROUP_concat( TopicID )
FROM Topics
WHERE Name IN (";
$sql_unique .= implode(",",$options);
$sql_unique .= ") ORDER BY Categories_CategoryID ASC )";
table Categories
catID
table Topics
topID
category
Categories : Topics ( 1 : n )
Question: does category $x have unique set of topics?
first get sum of all topics of category $x
SELECT GROUP_CONCAT(topID ORDER BY topID) topiclist
FROM Topics
WHERE category='$x'
you may need to explode the topiclist and add some new values to that array, I am assuming that the result of the above query was fetched into an associative array $r
$list=explode(',',$r['topiclist']);
$list[]=$newtopicID1;
$list[]=$newtopicID2;
// etc ... or use $list += $newTopicsArray; which will ADD those two arrays
sort($list); //make it sorted again
$list=implode(',',$list);
and then query the database again, trying to find such a combination excluding the category i am querying:
SELECT GROUP_CONCAT(topID ORDER BY topID) topiclist
FROM Topics
WHERE category!='$x'
GROUP BY category
HAVING topiclist='$list'
then check whether there is some record returned. If so, then you found another category with the same topics, if not, then the combination of topics is unique
I have a comics site and have implemented a tagging system similar to that of StackOverflow's.
I wanted to give users more sorting options by introducing a SO-style tagging system - each user can select and remove 1 or many tags... each comic id is associated to 1 or many tags.
The goals are:
Function will display all comics if no category and no tags are chosen
If category x is selected, will display only that category's comics
If category x AND tag x is selected, will display all comics in category x AND tag x
If NO category is selected, but tags x, y, z are selected, function will display only those comics associated with those tags
I'm using a relational table to check if there's a matching imgid to the selected tagids.
So in the database, in order for the image to be associated to multiple tags (which it should be able to), I have to add the same imgid again, with a different tagid.
Here's the query:
$sql =
"SELECT tbl.*, t.*
FROM $table tbl
LEFT JOIN $assocTable a ON (tbl.id = a.imgID)
LEFT JOIN $tagsTable t ON (t.tagid = a.tagID)
WHERE ";
$sql .= !empty($_SESSION[$sessiontagIDs]) ? "a.tagID IN (" . implode(', ', $_SESSION[$sessiontagIDs]). ") " : "1 = 1";
$sql .= $catquery ." " . $order;
Unfortunately, that means that a comic is now showing twice from the query:
Do I have the tables or query set up incorrectly?
Thank you!
The problem is that you are fetching the comic for every tag that it matches. Perhaps you want the query to get:
select distinct tbl.*
. . .
Or, if you want the tags at the same time, use group_concat() to get them in a list:
select tbl.*, group_concat(t.tagname)
. . .
group by tbl.id
I have a SELECT name="idproduct" inside a FORM and its OPTION VALUE is the Id of the product that I have SELECTED. I would like to make an array in PHP with the Id´s of the products that I have SELECTED one after another.
EXAMPLE:
$idproducts=(1, 2, 14, 24, 8)
On the top of the table I have the SELECT and first I choose one product. Then I choose another one etc.... I need the Id´s of the choosen products in that select because down the page I have a table where I want to show the products I have selected at the top.
$sql='SELECT id_product,
nameproduct,
situation,
quantity
FROM product
WHERE id_product = "'.$idproducts.'"';
Somebody would explain me how can I make the array with the posted Id´s of the SELECT?
$sql='SELECT id_product, nameproduct, situation, quantity
FROM product WHERE id_product IN (.$idproducts.)';
Assuming your <select> is multi select, you could loop through the array like so to build the query string:
$sql='SELECT id_product,
nameproduct,
situation,
quantity
FROM product
WHERE 1';
foreach( $_POST['idproducts'] as $k=>$v ){
$sql .= ' OR id_product = ' . $v;
}
If $idproducts is an array:
$sql='SELECT id_product, nameproduct, situation, quantity FROM product
WHERE id_product IN ('.implode(',',$idproducts).')';
Here is a function I like to use to turn arrays of data in to or based where queries.
$sql_stub = "SELECT id_product, nameproduct, situation, quantity FROM product";
$sql = orQuery($sql_stub, array("id1", "id2"), "id_product");
function orQuery($sql_stub, $array, $field){
//START WHERE CLAUSE
$where = "WHERE ";
//ADD DATA TO THE WHERE CLAUSE
foreach($array as $data){
$where = $where." ".$field." = '".$data."' OR ";
}
//CHECK THAT THE WHERE CLAUSE IS MORE THAN JUST WHERE
if($where!="WHERE "){
//REMOVE THE TRAILING OR
$where = substring($where, strlen($where)-4);
//APPEND TO SQL_STUB
$sql_stub = $sql_stub.$where;
}
return $sql_stub;
}