i have an array where i can get id's from a database table, now i need to extract those values as i run a domain link array such as: domain.com/page?id=1
Now this page will bring a category, lets say A category with ID 1 i need to list items under this category on the same database table example products, i can use it without a filter now i need a filter using arrays, here is my code, thank you in advance.
$query ="SELECT * FROM tablename WHERE id = '".$id."' DESC";
before this i have on my php code:
$id = $_GET["id"];
So when i call from this table all comes out, but i need to filter where category 1 (example) have items, not all items with category 2, 3, 4 etc.
With my code i can get the item id as the link with array works fine for example: domain.com/page.php?id=1, but here is the catch, i get item id 1 and i need let's say category id 2, if i run the same link with id=2 i get item id 2 and that's not what i want.
I need to retrieve those values as arrays, any idea? Thank you in advance!
EDITED
Table structure example:
Items
ID, Name, Category
I need Category from Items as a filter like this: domain.com/?id=2&category=2
So i get all items under row Category only
Update your URI to something like this domain.com/page.php?id=1&category=2
$category = $_GET["category"];
$query ="SELECT * FROM tablename WHERE id = '".$id."' AND `Category` = '".$category."'";
Pass the query string parameter optionally
domain.com/page.php?id=1&category=2
Add the where clause dynamically like this
$query ="";
$conact =" where 1=1 ";
if(isset($_GET['id']))
{
$conact.=" and id=".$_GET['id'];
}
if(isset($_GET['category']))
{
$conact.=" and category=".$_GET['category'];
}
$query ="SELECT * FROM tablename $conact DESC";
Related
Data stored in the database as below where category is column name and comma separated strings are values.
category = 'cat,cat1,cat2,cat3,cat33,cat4';
I'm trying to search with a category from the list:
$query= mysqli_query($mysqli, "SELECT * FROM table where
INSTR(category,'cat3') > 1 ");
But this doesn't work because the result includes cat3 and cat33.
Any thoughts on how I can do this search, please?
Thanks!
You can use FIND_IN_SET function :
SELECT * FROM table where FIND_IN_SET('cat3', category) > 0
But you should normalize your data by creating category table and relate to your table's primary key
"SELECT * FROM table where category LIKE '%cat3,%'";
"SELECT * FROM table where category LIKE '%cat33,%'";
if you don't want to add comma to the end of each category name, then
"SELECT * FROM table where category LIKE '%cat3,%' OR category LIKE '%cat3'";
You can "solve" this using the query below. BUT I HARDLY RECCOMEND you to normalize your database and use this only if you REALLY NEED
SELECT * FROM table WHERE fild LIKE '%,camp3,%' OR fild LIKE 'camp3' OR field LIKE '%,camp3'
I'm trying to place one mysql select inside another one and combine the results to be displayed.
this is my code:
$allattrs = "";
$sql69 = "SELECT * FROM product_details";
$query69 = mysqli_query($db_conx, $sql69);
$login_check69 = mysqli_num_rows($query69);
if($login_check69 > 0){
while($row69 = mysqli_fetch_array($query69, MYSQLI_ASSOC)){
$FID = $row69["id"];
$sql2s = "SELECT * FROM ATTRIBUTES WHERE id='$FID'";
$query2s = mysqli_query($db_conx, $sql2s);
$login_check2s = mysqli_num_rows($query2s);
if($login_check2s > 0){
while($row2s = mysqli_fetch_array($query2s, MYSQLI_ASSOC)){
// Get member ID into a session variable
$Sid = $row2s["id"];
$attr = $row2s["attr"];
$allattrs .= ''.$attr.', ';
}
}
$product_list .= '<tr>
<td>'.$allattrs.'</td>
</tr>';
}
}
The problem i'm having is that the $allattrs returns the values but it will put everthing together.
for example:
if one attr column in mysql database has apples, and another one has oranges, when i see the results of $allattrs on my PHP page i see this:
id 1 - apples
id 2 - apples, oranges
id 3 - apples, oranges, apples, oranges
etc etc
this is in fact wrong as each attribute value needs to stay true to their own id and product_details id field!
I'm not sure what I am doing wrong to cause this.
could someone please advise on this issue?
any help would be appreciated.
The right way to write your query is using JOIN, EXISTS, or IN. I think you would find this most natural:
SELECT a.id, GROUP_CONCAT(a.attr) as attrs
FROM ATTRIBUTES a
WHERE a.id IN (SELECT id FROM product_details)
GROUP BY a.id;
This replaces a bunch of your code.
Looks like you are only interested in the the attributes then try this out instead of the first sql:
SELECT * FROM ATTRIBUTES where id IN (SELECT id FROM product_details)
You need to set $allattrs to an empty string inside your first while loop, instead of only once before.
Apart from that, you should look into the following two topics: Normalization and JOINs.
I'm trying to do this kind of query,
When i make the query by get, i want to display the results from the id, for example if i have this id in my GET =11;
I want to display all the products with the category_id = 11, even if they have another id in the concat format, I don't know how i can do this kind of query,
$id = $_GET['id'];
$sql = "SELECT * FROM `product` WHERE `product_category_id`='$id'";
My product_category_id contains the id's of the categories in CONCAT format:
product_category_id (type text)
11,30,29
The FIND_IN_SET MySQL function might be what you're looking for.
$sql = "SELECT * FROM `product` WHERE FIND_IN_SET('$id', `product_category_id`)";
With a field like that, you will have to do something like this:
$sql = "SELECT *
FROM product
WHERE product_category_id LIKE '$id,%'
OR product_category_id LIKE '%,$id,%'
OR product_category_id LIKE '%,$id'";
In general, though, this will be a slow query.
I use the following code to get information from my MySql data base in order to display items :
$sql = "SELECT product,size1,size2,prdname,price
FROM multisize
WHERE online= 'online' AND category = 'backcovers' AND campaign = '".$brand."'
ORDER BY size1 DESC, size2 DESC ";
Some times I have the same item that needs to be displayed in two campaigns. (ex. iphone4, iphone5)
is there any way to tell my code that this item should be displayed in those two campaigns or should i just create duplicate items ?
You may use GROUP BY clause in your query.
I have a table:
id, affiliate
Each time somebody clicks a link, a new row is inserted,
ID being the ID of the page, and affiliate being the ID of the affiliate.
For example:
Page ID: 9 Affiliate ID: 1
Page ID: 9 Affiliate ID: 2
Page ID: 9 Affiliate ID: 3
I only have 3 affiliates.
I want to select this information, and group them by affiliate, for the ID.
I have tried this query:
SELECT COUNT(*) FROM table
WHERE id = '9' GROUP BY
affiliate
It works fine when I do it in php my admin, how do I get the info in PHP?
I have tried:
$q = mysql_query("SELECT COUNT(*) FROM
table WHERE id = '" . $id . "'
GROUP BY affiliate");
$r = mysql_fetch_array($q);
When trying to print the data onto the page, I am only getting one result.
Do I need to use a foreach/while loop to get all 3? How would I go about doing this?
Thank you!
You should do mysql_fetch_array() (or mysql_fetch_assoc()) in a loop:
while ($row = mysql_fetch_assoc($q)) {
echo $row["id"];
echo $row["affiliate"];
}
UPDATE (in accordance with comment):
If you ALWAYS have 3 rows in your result, probably mysql_result() function would be helpful:
$firstAffiliate = mysql_result($q, 0, "affiliate");
$secondAffiliate = mysql_result($q, 1, "affiliate");
$thirdAffiliate = mysql_result($q, 2, "affiliate");
BTW, be careful and check, whether query actually returns 3 results.
Loop like this:
while($row = mysql_fetch_array($q)) {
print_r($row);
}
If you just want to get list of Affiliate ID, you can consider this query
select group_concat(affiliate)
from table
where id=9;
The above will return a comma separate value like 1,2,3 in single row
So, you can then in PHP explode using ,, and the size of the exploded array is the same as count(*)
you can use a variable to hold state inside the actual mysql statement itself. Try the following:
affiliates:
page_id
afil_id
set #page_id=0;
select page_id, if(#page_id <> page_id, count(*), 0), #page_id:=page_id from clicks group by page_id;
Hope that helps