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']}";
}
}
}
Related
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>";
}
}
}
I am trying to display comments below updates. Please help me know how to bring all the comments as an array from the database. The code below will give me only one comment when I call $article->comments as shown below, I only get one result. Counting the rows gives a correct count, but displaying brings only one result.
$articlesQuery = $db->query("
SELECT
updates.id,
updates.update_text,
updates.posted_at,
updates.user_id,
updates.the_group,
members_comments.comment AS comments,
COUNT(articles_likes.id) AS likes,
GROUP_CONCAT(members.id SEPARATOR '|') AS liked_by
FROM updates
LEFT JOIN articles_likes
ON updates.id = articles_likes.article
LEFT JOIN members
ON articles_likes.user = members.id
RIGHT JOIN members_comments
ON members_comments.update_id = updates.id
GROUP BY updates.id
ORDER BY updates.id DESC
LIMIT 3
");
while ($row = $articlesQuery->fetch_object()){
$row->liked_by = $row->liked_by ? explode('|', $row->liked_by) : [];
$articles[] = $row;
}
then to display, this brings errors, but it explains what i'm trying to do.
foreach($articles as $article):
echo $article->update_text;
foreach ($article->comments as $comment){
echo $article->comment;
}
endforeach;
I think the loops are incorrect.
Foreach $article (This is now 1 row)
foreach comment in $article echo comment
So this comment will only be a comment from 1 row?
Unless I have misunderstood this should be your code:
foreach($articles as $article) {
print $article->update_text."\n";
print $article->comment."\n";
}
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).
i have a problem with an association between 2 tables:
i have
table 1 "flowers": "id", "flower";
table 2 "colors": "id", "color";
table 3 "association_flowers_colors": "id", "id_flower", "id_color";
now, a single flower can have many colors, now the problem is the father all the informations inside a single array.
es: if i get all the informations inside the first table and fetch them i'm gonna have
array([0](array[id]=>0, [flower]=>rose), [1]array([id]=>1, [flower]=>tulip));
then if i gather all the informations from the second table i'm gonna have
array([0]array([id]=>0, [color]=>red)[1]), [1]array([id]=>1, [color]=>white));
Now to the problem: i'd need to get an array along the lines of
array([0]array([id]=>0, [flower]=>rose, [color]=>red, [color]=>white), [1]array([id]=>1, [flower]=>tulip, [color]=>red, [color]=>white));
is there a simple way to do it with a single elaborated query or should i go with while loop or something like that and then merge the informations i get?
Thanks
edit:
function get_flowers(){
include('../actions/db_connection.php');
$result = mysqli_query($db_connection, "select *
from flowers f
left outer join association_flowers_colors a on f.id = a.id_flower
left outer join colors c on c.id = a.id_color");
mysqli_close($db_connection);
$flowers = array();
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$flowers[] = $row;
};
return $flowers;
}
I ended up with this after reading juergen's reply, it looks like it works the problem is that it gives me an array for each color(for a total of 4 arrays) instead of giving me only 2 arrays with both colors inside(for a total of 2 arrays).
edit2:
Ok, i didnt find a way to retrieve all the informations in 2 arrays(instead of 4) but i managed to print them in a readable way anyway:
while($row = mysqli_fetch_array($flowers)){
if ($flowerID != $row['id_flower']) {
echo "<div class='flowerContainer'>";
echo "<input type='hidden' name='flowerID' value='".$row['id_flower']."'>";
echo "Name: ".$row['flower']."<br />";
}
echo $row['color']."<br />";
if ($flowerID == $row['id_flower']) {
echo "</div>";
echo "<hr>";
}
$flowerID = $row['id_flower'];
}
in this way even if the loop repeats all the information for every array, it will only output the information if there's a new entry and it won't repeat anything.
select *
from flowers f
left outer join association_flowers_colors a on f.id = a.id_flower
left outer join colors c on c.id = a.id_color
I'm building a simple web app at the moment that I'll one day open source. As it stands at the moment, the nav is generated on every page load (which will change to be cached one day) but for the moment, it's being made with the code below. Using PHP 5.2.6 and MySQLi 5.0.7.7, how more efficient can the code below be? I think joins might help, but I'm after advice. Any tips would be greatly appreciated.
<?php
$navQuery = $mysqli->query("SELECT id,slug,name FROM categories WHERE live=1 ORDER BY name ASC") or die(mysqli_error($mysqli));
while($nav = $navQuery->fetch_object()) {
echo '<li>';
echo ''. $nav->name .'';
echo '<ul>';
$subNavQuery = $mysqli->query("SELECT id,name FROM snippets WHERE category='$nav->id' ORDER BY name ASC") or die(mysqli_error($mysqli));
while($subNav = $subNavQuery->fetch_object()) {
echo '<li>';
echo ''. $subNav->name .'';
echo '</li>';
}
echo '</ul>';
echo '</li>';
}
?>
You can run this query:
SELECT c.id AS cid, c.slug AS cslug, c.name AS cname,
s.id AS sid, s.name AS sname
FROM categories AS c
LEFT JOIN snippets AS s ON s.category = c.id
WHERE c.live=1
ORDER BY c.name, s.name
Then iterate thru the results to create the proper heading like:
// last category ID
$lastcid = 0;
while ($r = $navQuery->fetch_object ()) {
if ($r->cid != $lastcid) {
// new category
// let's close the last open category (if any)
if ($lastcid)
printf ('</li></ul>');
// save current category
$lastcid = $r->cid;
// display category
printf ('<li>%s', $r->cslug, $r->cname);
// display first snippet
printf ('<li>%s</li>', $r->cslug, $r->sname, $r->sname);
} else {
// category already processed, just display snippet
// display snippet
printf ('<li>%s</a>', $r->cslug, $r->sname, $r->sname);
}
}
// let's close the last open category (if any)
if ($lastcid)
printf ('</li></ul>');
Note that I used printf but you should use your own function instead which wraps around printf, but runs htmlspecialchars thru the parameters (except the first of course).
Disclaimer: I do not necessarily encourage such use of <ul>s.
This code is just here to show the basic idea of processing hierarchical data got with one query.
First off, you shouldn't query your database in your view. That would be mixing your business logic and your presentation logic. Just assign the query results to a variable in your controller and iterate through it.
As for the query, yup a join can do that in 1 query.
SELECT * -- Make sure you only select the fields you want. Might need to use aliases to avoid conflict
FROM snippets S LEFT JOIN categiries C ON S.category = C.id
WHERE live = 1
ORDER BY S.category, C.name
This will get you an initial result set. But this won't give you the data nicely ordered like you expect. You'll need to use a bit of PHP to group it into some arrays that you can use in your loops.
Something along the lines of
$categories = array();
foreach ($results as $result) {
$snippet = array();
//assign all the snippet related data into this var
if (isset($categories[$result['snippets.category']])) {
$categories[$result['snippets.category']]['snippet'][] = $snippet;
} else {
$category = array();
//assign all the category related data into this var;
$categories[$result['snippets.category']]['snippet'] = array($snippet);
$categories[$result['snippets.category']]['category'] = $category;
}
}
This should give you an array of categories which have all the related snippets in an array. You can simply loop through this array to reproduce your list.
I'd try this one:
SELECT
c.slug,c.name,s.name
FROM
categories c
LEFT JOIN snippets s
ON s.category = c.id
WHERE live=1 ORDER BY c.name, s.name
I didnt test it, though. Also check the indexes using the EXPLAIN statement so MySQL doesnt do a full scan of the table.
With these results, you can loop the results in PHP and check when the category name changes, and build your output as you wish.
Besides a single combined query you can use two separate ones.
You have a basic tree-structure here with branch elements (categories table) and leaf elements (snippets table). The shortcoming of the single-query solution is that you get owner brach-element repeatedly for every single leaf element. This is redundant information and depending on the number of leafs and the amount of information you query from each branch element can produce large amount of additional traffic.
The two-query solution looks like:
$navQuery = $mysqli->query ("SELECT id, slug, name FROM categories WHERE live=1 ORDER BY name")
or die (mysqli_error ($mysqli));
$subNavQuery = $mysqli->query ("SELECT c.id AS cid, s.id, s.name FROM categories AS c LEFT JOIN snippets AS s ON s.category=c.id WHERE c.live=1 ORDER BY c.name, s.name")
or die (mysqli_error ($mysqli));
$sub = $subNavQuery->fetch_object (); // pre-reading one record
while ($nav = $navQuery->fetch_object ()) {
echo '<li>';
echo ''. $nav->name .'';
echo '<ul>';
while ($sub->cid == $nav->id) {
echo '<li>';
echo ''. $sub->name .'';
echo '</li>';
$sub = $subNavQuery->fetch_object ();
}
echo '</ul>';
}
It should print completely the same code as your example
$navQuery = $mysqli->query("SELECT t1.id AS cat_id,t1.slug,t1.name AS cat_name,t2.id,t2.name
FROM categories AS t1
LEFT JOIN snippets AS t2 ON t1.id = t2.category
WHERE t1.live=1
ORDER BY t1.name ASC, t2.name ASC") or die(mysqli_error($mysqli));
$current = false;
while($nav = $navQuery->fetch_object()) {
if ($current != $nav->cat_id) {
if ($current) echo '</ul>';
echo ''. $nav->cat_name .'<ul>';
$current = $nav->cat_id;
}
if ($nav->id) { //check for empty category
echo '<li>'. $nav->name .'</li>';
}
}
//last category
if ($current) echo '</ul>';