PHP - Mysql how show items from a DB related to specific column - php

Hello below some info about my problem
My_DB name is: products
Table name is: spec_products
Columns in spec_products are:
id|sku|brand|title|price
How can i show (with php) only items from the brand column?
In particular it's a piece of code that i need insert in sidebar of single product page that show products correlated to the brand

Here is the MySQL query:
Select brand From spec_products;

use clause distinct to avoid duplicate values if more of one of your products have the same brand
select dintinct brand from spec_products

From what I understand, you want to display only the brands from the table. If this is the case, it is a simple Selecting Data from MySQL that you would have got by simple search on Google.
You can refer to a W3School tutorial here
You can simply run a SELECT query for brands:
SELECT brand FROM spec_products

Related

Add or Remove Dynamic Dependent Select Box data from two mysql tables using jQuery with PHP Ajax

I need complete sample code to Add or Remove Dynamic Dependent Select Box data from two mysql tables using jQuery with PHP Ajax
Dependent select data should be come these below tables. And should be add and remove button to add row.
Category
Sub Category
Below is sample data, but in this, dependent select data coming from only one mysql table.
https://www.webslesson.info/2019/09/add-or-remove-dynamic-dependent-select-box-using-jquery-with-php-ajax.html
Can any provide me sample demo with source code ?
Please this sample image
Please see this image, There are three tables, (1) Items, (2) Category, (3) Sub Category, I need same like this, but data should come from Category table and sub category table in dependent select boxes. And this is multi dynamic row. Can you give me all demo code, becuase I do not know about Jave,Ajax and jQuery.
Below is sample demo and source code weblink, But in this sample, He is getting data from only one table for both dependent boxes category and sub category. Data should be come from two tables both dependent boxes.
enter link description here
I hope you understand what I required.

Create A List From a Category Section in mysql

I was wondering if mysql has a way to look at a column and only retrieve the results when it finds a unique column once. For example
if the table looks like this:
id name category
1 test Health
2 carl Health
3 bob Oscar
4 joe Technology
As you can see their are two rows that could have the same category. Is their a way to retrieve the result where the array will one only return the category once?
What I am trying to do is get all the categories in the database so I can loop through them later in the code and use them. For example if I wanted to created a menu, I would want the menu to list all the categories in the menu.
I know I can run
SELECT categories FROM dbname
but this returns duplicate rows where I only need the cateogry to return once. Is there a way to do this on the mysql side?
I assume I can just use php's array_unique();
but I feel like this adds more overhead, is this not something MYSQL can do on the backend?
group by worked perfectly #Fred-ii- please submit this as answer so I can get that approved for you. – DEVPROCB
As requested by the OP:
You can use GROUP BY col_of_choice in order to avoid duplicates be shown in the queried results.
Reference:
https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
By using database normalization, you would create another table with an unique id and the category name and by that link those two together, like
select * from mytable1
on mytable1.cat = mytable2.id
group by mytable1.cat
You can ofcourse also use group by without multiple tables, but for the structure, I recommend doing it.
You can use select distinct:
SELECT DISTINCT categories
FROM dbname ;
For various reasons, it is a good idea to have a separate reference table with one row per category. This helps in many ways:
Ensures that the category names are consistent ("Technology" versus "tech" for instance).
Gives a nice list of categories that are available.
Ensures that a category sticks around, even if no names currently reference it.
Allows for additional information about categories, such as the first time it appears, or a longer description.
This is recommended. However, if you still want to leave the category in place as it is, I would recommend an index on dbname(categories). The query should take advantage of the index.
SELECT id, name from dbname GROUP BY categoryname
Hope this will help.
You can even use distinct category.

Displaying Products from Mysql PHP

RDBMS: MySQL
I'm designing an online store and need some assistance in displaying the products for a category page. Say there are 100 products and each product has 1-3 images.
I need to display all products, but also multiple images per product.
I'm having trouble in finding a way to display it correctly.
Database schema:
I can retrieve the data as an array and loop through it but if I join the tables with all of the images I'm going to have duplicate rows retrieved with different images in each. I'm having trouble understanding how I would display multiple images for every product when viewing all products.
Let me know if you'd like me to elaborate further.
Thanks in advance!
Write two separate queries to fetch data from the database:-
Fetch all the products from the database and then display them to the page
Second query: Fetch the images based on ProductID. If you wanna do something fancy, you can use java script to rotate the images.
Let me know if you need further assistance. Thank you!
You can use group_concat() Mysql function to get all the images in one go, without duplicating the records. Below example shows the same.
select p.product_id, p.product_name, group_concat(pi.image_path)
from procuct p join product_images pi on p.product_id = pi.product_id
group by p.product_id, p.product_name
This will give you all the paths as comma separated list along with each product record.
Use this query:
select products.*,GROUP_CONCAT(image_path)
form products
LEFT JOIN product_images on products.product_id = product_images.product_id
GROUP BY product_images.product_id
You will get the result of products with comma separated images
then explode those comma separated images in PHP and display it.

display multiple mysql rows in html table

I have a table called jobcardlabour. It has 3 colums, Jobcard, Description, Quantity and Amount. When I create a jobcard I enter the work I have done into this table. I know want to create an invoice so I want to call this table and display the results in a html table on the printable invoice. I can do this but where I am having difficulty is there might be 3 or more rows which all have the same jobcard number. I need all the rows displayed in the table. The table needs three headings, called Description, Qty and Amount. If anyone can help with the basics I can modify it to suit my needs. Many thanks again.
If you need all the information, then just do
SELECT * FROM jobcardlabour;

multi category in page PHP MYSQL

i have problem with multi category for page in php and mysql,
i have "Business" , "Category" and "Business_con_Category" tables in mysql.
the Business_con_Category table is connect between Business to Category,
so i think doing select multiple, and implode for add to table.
but my problem is editing how i can know if remove category and delete from table ?
i treid in to array old category and new category and compare, but is not work good..
do you have an idea
thanks!
An easy solution is to not try to remove only some rows from business_con_category. Instead, remove all rows for the given business, and then add the rows with the categories that still should be present.

Categories