I have a table that looks like this:
+----------------------------------+
| Category | Sub-Category |
+---------------+------------------+
| Cell Phones | Smart Phones |
| Cell Phones | Tablet Phones |
| Cell Phones | Other Phones |
| Computers | Desktops |
| Computers | Laptops |
| Computers | Chromebooks |
+---------------+------------------+ etc..
And I would like to display this data on my website using PHP, like this:
How would I display the information as above(one category, all subcategories under) using PHP? Also how can I evenly divide the list of category/subcategories into 3 columns?
I've found tutorials but none seemed to use PHP, which is the part I'm having trouble with.
Thank you guys for your time.
This type of reports are called cross tabing reports. In sql there is pivot function which gives data as you require. But in mysql there is no pivoting so you need to use your own logic.
To achieve this you will need to have two loops as described below a short idea on how to do this.
Select distinct maincategory from table
Loop for main category{
select subcategory from table where maincategory='category'{
print all sub categories
}
}
$data = query_data();
$cat = array();
foreach($data as $item)
{
$cat[$item['category']][] = $item['sub_category'];
}
Now, $cat is what you need.
You can give category id to them for example, say cellphones has an id of 1 then all items inside this will also have a subcategory_id of 1. Similarly if the printers have id of 2 then all subcategory_id under it will have id as 2.
Now you can run simple query to retrieve the field specific category. like:
SELECT * from table_name WHERE subcategory_id = 1;
This will give out all the categories inside cellphones...similarly you can do for all fields...
This should work -
$categories = Array();
//Assuming database handle is $db
$r1 = $db->query("select distinct(Category) from table_name") or die("err in q1");
while($cat_arr = $r1->fetch_assoc()){
$cat = $cat_arr['Category'];
$sub_cats = Array();
$db->query("select Sub-Category from table_name where Category = $cat") or die("err in q2");
while($sub_arr = $r2->fetch_assoc()){
array_push($sub_cats,$sub_arr['Sub-Category']);
}
$categories[$cat] = $sub_cats;
}
The $categories array now has the details of the table, and can be used as is.
Related
I have two MySQL tables, first, is house and second one family, The house table has two columns called house_id and house_name while the second table has three columns family_id, member_name the last column in the family table used to reference house_id from first table this is house_id
CONCEPT:
How can get number of family_members with/who come from a certain house and put the counted number in the HTML table by fetching only on house table?
I created the following script to fetch from house,
SELECT * FROM house;
And I manipulated the fetched data using foreach like this,
$mystmt = $db -> prepare('SELECT * FROM house');
$mystmt->execute();
$myresult = $mystmt->fetchAll();
foreach($myresult as $mydata) {
$myarray = array();
$myarray[] = $mydata['house_id'];
$myarray[] = $mydata['house_name'];
$output[] = $myarray;
}
echo json_encode($output);
On the above code i get only two columns the house_id and house_name, Now how can i adjust this code to count from family table so that I can get another column called total_family_members
Just like this
family_id | member_name | house_id
1 | John Jackson | 1
2 | Ketty Bebe | 2
3 | Maam Juke | 1
4 | Tike Nuke | 2
5 | Carol Michael | 2
Desired result
house_id | house_name | total_family_members
1 | Joh's house| 2
2 | kim's house| 3
In your example, two different processes are involved:
Link everyone's house id to the house name. This can be achieved with a JOIN.
Count the number of people in each house. This can be achieved with a GROUP BY and a COUNT.
Here is a solution:
SELECT house.house_id , house.house_name , COUNT( * ) AS total_family_members
FROM house
INNER JOIN family ON family.house_id = house.house_id
GROUP BY family.house_id
I have a table recording hits to pages like this :
id | ip_address | page_id
---------------------------------------
1 | 192.123.456.78 | 2787321
2 | 192.000.000.00 | 2787321
3 | 192.123.456.78 | 342415
4 | 192.123.456.78 | 2787321
5 | 192.432.999.80 | 2787321
I'm getting the results like this :
$getHits = $wpdb->get_results( "SELECT * FROM $table WHERE page_id = $pageID" );
I want to display the results grouped by ip_address based on how many times each IP accessed the same page_id.
Example of desired output (where $pageID = 2787321) :
192.123.456.78 - 2 Views
192.000.000.00 - 1 View
192.432.999.80 - 1 View
I've been trying a few things (grouping mainly and running through while and foreach loops) but it quickly got really complicated and I can't get it to work.
Could somebody point me in the right direction please?
You should not use var for dinamic query you are at risk of sqlinjection
anyway
$wpdb->get_results( "SELECT ip_address, count(*) hits
FROM $table WHERE page_id = $pageID
GROUP BY ip_address
ORDER BY hits DESC" );
http://www.mysqltutorial.org/mysql-group-by.aspx
Here’s what I have :
I have table 1 which is the parties table. It lists all the parties that are scheduled for the month.
I have 4 other tables, each contains the type of party favours that we use.
party_toys
party_drinks
party_foods
party_balloons
I need to query all parties that start this coming Saturday and list them back until the beginning of the year.
I have accomplished this.
What I need to do is figure out how to search but the party name from parties table and search all the party favours tables and list the results.
Still sorted this Saturday to the beginning of the year and filter out parties which we do not have any favours.
Currently I can search only by party name from the parties table. I can’t figure out how to search all of them at the same time.
I also need to paginate all of these.
I currently have pagination working but it currently lists everything including the parties that have no favours assigned.
I am stuck. Does this make sense?
Structure
For the parties table:
| party_id | parent_id | party_title | party_description | party_start | party finish |
For the party_drinks table:
| party_id | drink_id | drink_title | drink_notes |
For the party_foods table:
| party_id | food_id | food_title | food_notes |
For the party_toys table:
| party_id | toy_id | toy_title | toy_notes |
For the party_balloons table:
| party_id | balloon_id | ballon_title | balloon_notes |
SELECT parties.*, party_toys.*, party_drinks.*, party_foods.*, party_balloons.*
FROM parties
INNER JOIN party_toys ON parties.id = party_toy.party_id
INNER JOIN party_drinks ON parties.id = party_drinks.party_id
INNER JOIN party_foods ON parties.id = party_foods.party_id
INNER JOIN party_balloons ON parties.id = party_balloons.party_id
WHERE parties.title LIKE "%Super party%"
OR party_toys.toy_title LIKE "%Super toy 9000%"
OR party_drinks.drink_title LIKE "%Tequila%"
OR party_foods.food_title LIKE "%Burger%"
This is going to select all the data from all the table so feel free to customize the infos you want to select. Then it's going to link all of them, and depending on the parameters you set on the WHERE it's going to return you the stuff you ask for.
Also for pagination you can do it using a get parameter from your url :
your_site.com/search.php?page=12
Get the page number before your query :
if( isset($_GET['page']) and is_int($_GET['page']) )
$page = $_GET['page'];
else
$page = 0;
$results_per_page = 20;
$offset = $page * $results_per_page;
Once you have your page number you just add this parameter at the end of your query
$query = "all the query i wrote above";
$auer .= " LIMIT ".$offset.", ".$results_per_page;
I have many different merchant stores like (Nike, Reebok, Adidas, Loto etc) in my table.
I want to retrieve all the information related to one particular store like (ex: Nike) and I want to show all the information related to that Nike store.
My table is like:
+----+--------------+--------------+
| id | store_name | description |
+----+--------------+--------------+
| 1 | nike | dasfdasdfas |
| 2 | reebok | dfasdfa |
| 3 | addidas | adasdf |
| 4 | loto | asdfasfdas |
| 5 | nike | sadlfjasldfj |
+----+--------------+--------------+
I wrote the code as:
<?php include ('config.php');
$q = mysql_query("SELECT * FROM `Merchant Store` WHERE store=$store_name");
while($r=mysql_fetch_array($q)){
?>
echo <?php $r ?>
How can I do this? I want all the Nike store related content to be displayed.
$q = mysql_query("SELECT * FROM `coupons` WHERE store='" . $store_name . "'");
Missing single quotes
Also in your table there is not column named store , so posted query retrieving all columns (using * instead of store)
PS. See this post, why not to use mysql extension
Here:
$q = mysql_query("SELECT * FROM coupons WHERE store='$store_name'");
Also your $store_name variable must have the value of that store whose value you want to fetch and display. Also you most probably have each store name only once in your table. So you can leave out while loop which is used when multiple results are expected.
And after running mysql_fetch_array() part you need to get each field in a variable and then display it.
So your query will look like
<?php include ('config.php');
$q = mysql_query("SELECT * FROM coupons WHERE store='$store_name'");
$r=mysql_fetch_array($q);
$store_name = $r['store_name'];
$description = $r['description'];
echo 'Store name '.$store_name;
echo 'Description '.$description;
?>
I'm working on a big project a social networking site but now I'm stuck and i need your advice.
My problem is that I wan't to display everything like posts, videos and statuses into his profile.php under user timeline.
I got more than 40 Tables but let me specify what I wan't, I wan't to get data from these tables and how to display them on the profile.php timeline section ?
Status Table
------------------------------------------
ID | sMessage | sDate | uId
------------------------------------------
1 | Testing status | 2013/07/03 | 1
Videos Table
-----------------------------------------------------
ID | vName | vCode | vDate | uId
-----------------------------------------------------
1 | PHP and MYSQL | 2MvYwz | 2013/07/03 | 1
Users Table
-----------------------------------
ID | uName | JoinDate
-----------------------------------
1 | Tem Henov | 2013/07/03
And here is what i tried:
class userTimeline {
public function loadTimeline(){
dbConn();
$query = "SELECT
sMessage, sDate, vName, vDate, vCode
FROM status
INNER JOIN users
ON (status.uId = users.uId)
INNER JOIN videos
On (videos.uId = users.uId)
WHERE users.uId = '1'";
return $result = mysql_query($query);
}
}
and its loads the data fine but how to display that data in blocks like if its a status display in a separate block and if its a video display it in another block and how to order by date ?! any help is appreciated.
First : To Show data into various <div> first fetch them and show like,
1) Fetch all the record and store into a php array
$rows = array();
while($row = mysql_fetch_array(YOUR_QUERY))
{
$rows[] = $row;
}
2) Now Using foreach() loop use data fetched from query where ever you want
if(is_array($rows)) {
foreach($rows as $row) {
//do with $row or create some if,switch condition here !
}
}
For specific limits and tweaks study the result set we get from mysql_fetch_array() !
Second : To short data by date you can use multiple sorting (click here for
tutorial) but i think you should use MYSQL UNION to merge two
or more MySQL query results to get individual sorting result.