How to group my results and have all rows shown? - php

SELECT i.itemsname
, i.itemsprice
, i.itemsdescrip
, c.catname
, c.catdes
, c.status
, c.collapse
, c.catid
FROM items i
LEFT
JOIN categories c
ON c.catid = i.catid
WHERE i.restid
AND c.restid =12
GROUP
BY c.catid
that is my query at the moment but I would like to have something like this....
but this is what I'm getting:

Ok, I lied in the comments, so With PDO (haven't tested it)
$stmt = $PDO->prepare('SELECT
categories.catname,
items.itemsname,
items.itemsprice,
items.itemsdescrip,
categories.catdes,
categories.status,
categories.collapse,
categories.catid
FROM items
LEFT JOIN categories ON items.catid=categories.catid
WHERE items.restid AND categories.restid = :restid');
$stmt->execute([':restid' => 12]);
$data = $stmt->fetchAll(\PDO::FETCH_GROUP);
foreach($data as $catname => $rows){
//echo group html stuff
//echo "<dl>";
//echo "<dt>$catname</dt>".;
foreach($rows as $row){
//echo row data stuff
// echo "<dd> {stuff} </dd>";
}
//echo "</dl>";
}
I'll leave the html up to you. But as I said you want a data structure like this
[
'BREAKFASTS' => [
0 => [ name => "wimpy hamburger", description => "bla bla", price => "$100,000"],
1 => [ ... ]
],
'SINGLE BURGERS' => [ ...]
]
note that the first field after "SELECT" is by default the field used by FETCH_GROUP
See in this way, the first foreach can output the title of the category, which is BREAKFASTS for example. Then the inner foreach can do the individual rows in the table.
Personally I would use a dl, dt, dd tag setup as my structure (hinted in the comments, i really am to lazy today to code all the html, <sigh>)
https://www.w3schools.com/tags/tag_dt.asp
UPDATE
You may want to check your query
...
WHERE
items.restid AND ...
Seems to be flawed, just saying. I saw this while optomizing the query for sorting.
SELECT
c.catname,
i.itemsname,
i.itemsprice,
i.itemsdescrip,
c.catdes,
c.status,
c.collapse,
c.catid
FROM
(
SELECT c0.catid FROM categories AS c0 WHERE c0.restid = :restid SORT BY c0.catname
) AS t
JOIN
categories AS c ON t.catid=c.catid
LEFT JOIN
items AS i ON items.catid=categories.catid
WHERE
items.restid = ? //< this is the error/omission/strangeness i pointed out above.
So a few things to note, first you should base the query off the categories, as an empty category should be shown, while an item without a category will blow it all to bits ( basically, ie how can you group them by the category if they have none ) You'll wind up with some hodgepoge of items with no category at the end, of course based on your example I'm assuming a Many to One relationship. For example One category can have Many items, and Many items can belong to a category. (it's probably more ideal to do a Many to Many, but that's another story for another day)
The reason the above query is more optimized is the inner query, creates only a small temp table using the catid, And sorts on just the data from the cat table and only the data that is pulled by the where.
Then as we move to the outer query, they basically inherent the sort from the join, and we can pull the rest of the data from that. It's typically about 2-10x faster this way (of course I haven't test this particular query) in theory. Of course this is a bit more complex/advanced query and is optional, but it should improve sort performance if my mind is in the right place tonight... lol
Also I abbreviated your table names (alias), as I said I am lazy like that. Sadly my answers are always so long, dont ask me how I see all these issues, it's just experience or how my dyslexic brain works?
Lastly, if you really must use mysqli, you can manually group them with something like this.
$data = [];
while(false !== ($row = $res->fetch_assoc())){
$key = $row['catname'];
if(!isset($data[$key])) $data[$key] = [];
$data[$key][] = $row;
}
It's all so prosaic (common place, non-poetic) at this point for me.
Good luck.

$cat = mysqli_query($connect, "SELECT
categories.catname,
items.itemsname,
items.itemsprice,
items.itemsdescrip,
categories.catdes,
categories.catid
FROM items
LEFT JOIN categories ON items.catid=categories.catid
WHERE items.restid AND categories.restid = 12");
if($cat === FALSE) {
die(mysqli_error());
}
$data = [];
while ($rowb = mysqli_fetch_array($cat)) {
$key = $rowb['catname'];
if(!isset($data[$key])) $data[$key] = [];
$data[$key][] = $rowb;
foreach($data as $catname => $rowbs){
echo "
<dl><button class='accordiontry'><dt>$catname</dt></button>";
<div class='panel1'>
foreach($rowbs as $rowb){
echo"<div class='rmenu'>
<dd><span class='item'>{$rowb['itemsname']}</span>
<span class='price'>£{$rowb['itemsprice']}</span><br>
<span class='des'>{$rowb['itemsdescrip']}</span> ";
}
echo"</div></dd>
</div></dl>";
}
}
}

Related

SQL query to get value from referenced table

I have two tables called mg_product and mg_product_user_property.
In mg_product there are 3 columns: id, title, price as
In mg_product_user_property table product_id corresponds with id column in mg_product table.
So my goal is to get the value of property_id of "15", which in the picture above will be "Mediatek".
This is my SQL:
$sql = "SELECT *
FROM mg_product AS products
INNER JOIN mg_product_user_property AS properties
ON products.id = properties.product_id
WHERE title LIKE '%$search%')";`
PHP:
$resultSet = DB::query($sql);
if ($resultSet->num_rows > 0) {
while ($rows = $resultSet->fetch_assoc()) {
$title = $rows['title'];
$price = $rows['price'];
}
} else {
$output = "No results";
}
Now I need to assign to a php variable the value of property_id=15 so I will be able to print "Mediatek" on my website. How can I achieve that? Sorry for my English.
You are pretty close to what you want, but a couple things are going to either be a mess, or unwanted. So, since there are different ways one can go with this, I will only present a very stripped example (and am INTENTIONALLY leaving out a bunch of code here).
You may not want to do a JOIN like that in the initial search, as for each property, it will also return another of the same product. So looping through that will result in dozens of the same product.
However, if _ALL_YOU_WANT_ is to show the Product Title, Price, and Property 15... you can reduce some headwork with a simpler query:
SELECT p.title, p.price, pr.value
FROM mg_product AS p
LEFT JOIN mg_product_user_property AS pr
ON p.id = pr.product_id AND pr.property_id = 15
WHERE p.title LIKE '%$search%'
The LEFT JOIN means if the property doesn't exist, it will still return the product. But with an empty property value. And this should not return dozens of the same product for every other property in the table.
--
The OTHER way you could go about doing it, using the SQL query you already have (and the dozens of results of the same product it will return), you can alter your php loop like so:
$found_products = array();
while ($row = $resultSet->fetch_assoc()) {
if ($row['property_id'] == 15) {
$found_products[$row['product_id']] = array(
'title' => $row['title'],
'price' => $row['price'],
'prop' => $row['value']
);
}
}
// now you have a clean array of found products that have the property
--
Also I am forced to point out that you should use a prepared statement here, replacing inserting $search directly into the code. But showing you all of how to do that is beyond the scope of this question/answer.

How do i merge specific parts of an associative array?

Question first, explanation later:
Insteat of the first array, i want one which looks like the secound array:
echo json_encode($movies);
//WHAT I DO NOT WANT: [{"movie_name":"test1","genre_name":"Action"},{"movie_name":"test2","genre_name":"Drama"},{"movie_name":"test2","genre_name":"Action"}]
//WHAT I WANT: [{"movie_name":"test1","genres":["Action"]},{"movie_name":"test2","genres":["Drama","Action"]}]
So insteat of severals rows with the same value for movie_name but different values for genre_name, i want one row for each movie, where all genre_names are merged into one gernes array;
The solution i wish:
The data is fetched from an database using php and mysqli. This is the SQL query i use and which generates the first array:
SELECT movies.movie_name, genres.genre_name FROM genres
INNER JOIN genre_movie ON genres.id = genre_movie.genre_id
INNER JOIN movies ON movies.id = genre_movie.movie_id;
I am not good with SQL and think there is a query which gets the me the secound array (the one I WANT) right away.
My solution so far:
i actually solved the problem using a php arglorithm, but its kinda complecated and hard if the anything scales or i add new columns:
foreach($myArray as $movie)
{
foreach($newList as $key => $item)
{
if($item['movie_name'] == $movie['movie_name']){
$exists = true;
$position = $key;
}
}
if($exists == true)
{
$newList[$position]['genres'][] = $movie['genre_name'];
$exists= false;
} else {
$newList[] = array('movie_name' => $movie['movie_name'], 'genres' => array($movie['genre_name']));
}
}
Take a look at group_concat: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
I didn't emulate your tables but maybe this is your query:
SELECT movies.movie_name, group_concat(genres.genre_name) FROM genres
INNER JOIN genre_movie ON genres.id = genre_movie.genre_id
INNER JOIN movies ON movies.id = genre_movie.movie_id
GROUP BY movies.movie_name;
Edition
Well, I emulated it now, and yes, it's your query. I would just change:
group_concat(genres.genre_name)
For:
group_concat(distinct(genres.genre_name))
To return only different values.

SQL-Query PHP -Blog

I am sure that somewhere in the web this question has been already asked but i have not found it, because i don't know for what exactly i should search. So i will try to describe the problem:
I have a Blog written in PHP and a SQL-database, where i receive the needed data.
Each Blog entry contains 1 image and 1 text-field and has its own id.
To get this data i execute this query (pseudo-code) -> this is working fine:
foreach($db->query("SELECT news.id, news.position, news_text.date, news_text.text, news_images.image
FROM news
LEFT OUTER JOIN news_text ON news.id=news_text.id
LEFT OUTER JOIN news_images ON news_images.id = news.id
ORDER BY position") as $row){
echo "{$row['id']}";
echo "{$row['position']}";
echo "{$row['date']}";
echo "{$row['text']}";
echo "{$row['image']}";
}
Now the problem starts. I want to add a comment field, where users can add a message. So for each blog-entry there can be several comments (would probably be a 1:n connection).
- I tried to add a foreach loop in a foreach loop (stupid)
- I tried with several SQL-query but only get rubbish as a result
- I don't see the logic how to connect the database news_comment with the others
Here is my simplified database:
Can someone give me a hint how i could solve this problem. Result should be:
foreach($db->query("SELECT news.id, news.position, news_text.date, news_text.text, news_images.image
FROM news
LEFT OUTER JOIN news_text ON news.id=news_text.id
LEFT OUTER JOIN news_images ON news_images.id = news.id
ORDER BY position") as $row){
echo "{$row['id']}";
echo "{$row['position']}";
echo "{$row['date']}";
echo "{$row['text']}";
echo "{$row['image']}";
Here i get for each blog-entry the corresponding comments..
}
Thank you.
Misch
You should retrieve comments in another one query (not in loop) using the same conditions as for retrieving news. So that after executing both queries you will have to arrays: news and comments.
Then you just need to take appropriate comments for each news item.
To do that you might need to rebuild comments array so that comment news_id will be an index of array where comments are stored.
Finally it'd look like this:
$news = [
[
'id' => ...,
'position' => ...
<so on>
]
];
$comments = [
$news[0]['id'] = [<comment array>],
<so on>
];
That's the way most framework use to have data bound in result set.
Solved it with the help of Alex(thx) and MulipleIterator. This is how it worked for me (maybe someone has a better solution).
foreach($db->query("SELECT news.id, news.position, news_text.date, news_text.text, news_images.image
FROM news
LEFT OUTER JOIN news_text ON news.id=news_text.id
LEFT OUTER JOIN news_images ON news_images.id = news.id
ORDER BY position") as $row)
{
$news_id[] = $row['id'];
$news_text[] = $row['news_text'];
$news_images[] = $row['news_images'];
$values = new MultipleIterator();
$values->attachIterator(new ArrayIterator($news_id));
$values->attachIterator(new ArrayIterator($news_text));
$values->attachIterator(new ArrayIterator($news_images));
}
foreach($values as $value)
{
list($news_text, $news_images, $news_id) = $value;
{
echo '$news_text';
echo '$news_images';
foreach($db->query("SELECT comment FROM news_comment WHERE id = $news_id ") as $row)
{
echo "{$row['comment']}";
}
}
}

Foreach looping too many times

I'm using this to display information from a queried db in Wordpress. It displays the correct information but it loops it too many times. It is set to display from a SELECT query and depending on the last entry to the db seems to be whether or not it prints double or triple each entry.
foreach ($result as $row) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
Does anyone know what could be producing this error?
Thanks
The query powering that script is:
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
I can't seem to see the duplicate but I agree it must be there.
EDIT-- when I replace the WHERE clause to WHERE ws.username = 'password' , it still repeats. It it displaying a result for each time a result has username='password' , and displaying that set twice as well.
I think you want the following, if you're using MySQLi:
while ($row = $result->fetch_object()) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
Redundant JOIN clauses in my query which was pretty much pulling the same results from two tables (one of which was just a VIEW of the other).

PHP: Loop over a grouped SQL query array but echo one value once for each “grouping”?

I’ve a small piece of code I’ve tried to wrap my brain around for some hours now.
I’m trying to create a query which retrieves some elements and in the end group them by a selector.
I’ll try to demonstrate: I’m querying all the members in my organization. Each member is a member of a “department” and there can be several members in each department, but only one department for each member.
So I’ve created the query to select all members in my organization group by departments.
When I’m displaying it using a simple foreach($my_query as $q) … I want to output $q->department_name but only once for that department. Right now it says “Marketing” under all five members and then it changes to “HR” for all those members. I’ve tried some different methods such as array_unique, creating a function that checks for the string to see if it is the same and other but with no result.
I could create two foreach loops but if it is at all possible not to I would prefer that because two foreach loops would affect the performance.
Any help or suggestion would be very much appreciated.
Sinerely
- Mestika
--- Edited ---
By the way, my query looks like this:
SELECT *
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND meta.meta_key = 'active'
AND meta.meta_value = 'active'
AND cat_posts.post_type = 'member'
AND cat_term_taxonomy.taxonomy = 'deparment'
GROUP BY cat_terms.slug, cat_term_relationships.object_id
$prev_department = NULL;
foreach ($my_query AS $q)
{
if ($prev_department != $q->department_name)
echo '<h2>'. htmlentities($q->department_name, ENT_COMPAT, 'UTF-8') .'</h2>';
// ...
$prev_department = $q->department_name;
}

Categories