echo out JOIN query PHP in dynamic menu - php

I have two tables inside a test db:
information (id, menu, position, visible)
pages (id, information_id, menu, position, visible, content)
with which I try to make a related navigation like so:
public (inside table information with id = 1)
home (inside table pages with information_id = 1)
about us (inside table pages with information_id = 1)
work (inside table pages with information_id = 1)
cms (inside table information with id = 2)
articles (inside table pages with information_id = 2)
add users (inside table pages with information_id = 2)
when I query the tables in PHPMyadmin I get the result I need, but when I try to echo the result out in PHP, I don't get the wanted structured menu with subitems.
<?php
$info_set = $db->prepare("SELECT *
FROM ccms.information");
$info_set->execute();
while ($information = $info_set->fetch(PDO::FETCH_ASSOC)) {
echo "<li>" . $information["menu"] . "</li>";
$page_set = $db->prepare("SELECT i.*,p.*
FROM information i
JOIN pages p
ON i.id = p.information_id");
$page_set->execute();
echo "<ul>";
while ($pages = $page_set->fetch(PDO::FETCH_ASSOC)) {
echo "<li>" . $pages["menu"] . "</li>";
}
echo "</ul>";
}
?>
The result is that the menu items from information table get echo`ed out fine, but the subitems aren't.
What Am I missing here?
I apologize to everyone offended by the perhaps simplistic nature of my question, I am a novice to PHP & SQL.

Related

creating breadcrumb on article page with php using two queries

I want to have breadcrumb on my website looks like this: Home / Category / Sub Category / Article example: https://jsfiddle.net/10kLs0rc/
table structures
categories: id -- parentid -- title
articles: id -- categoryid -- title -- content
*
It's easy to print article's title to breadcrumb. But I also want to print category of article. (even subcategory if article has it. as you can see the example above.)
I think i need php code with double query, one for "categories" and other for "articles" table.
*
EDIT
Page i want to build called "article.php" user will visit: "article.php?id=1"
I can fetch all data I want, except articles parent categories.
Here is the code I simply have, and I want to create another query for fetching parent categories of article but I'm stuck.
<?php
$id = $_GET['id'];
$query = $handler->query("SELECT * FROM article WHERE id='$id'");
while($r = $query->fetch()) {
echo "
<h3>$r[title]</h3>
<p>$r[content]</p>
";
}
?>
This is the query you want :
select
`a`.`title` as `art_title`,
`a`.`content` as `art_content`,
`c1`.`title` as `cat_title`,
`c2`.`title` as `cat_title_parent`
from `article` as `a`
left join `categories` as `c1`
on `c1`.`id` = `a`.`categoryid`
left join `categories` as `c2`
on `c2`.`id` = `c1`.`parentid`
where `a`.`id`=2;
And Here is the POC on [SQLFiddle
So in your php :
while($r = $query->fetch()) {
echo '<i>';
if(isset($r['cat_title_parent']))
echo '/'.$r['cat_title_parent'];
if(isset($r['cat_title']))
echo '/'.$r['cat_title'];
echo '</i><br />';
echo '<h3>'.$r['art_title'].'</h3>';
echo '<p>'.$r['art_content'].'</p>;
}

Trying to use variable in MySQL INNER JOIN

I am a php and MySQL newbie. What I have done is created an html form with a <select> dropdown. Based on the selection from the form, it changes the $_SESSION[campaignID] variable. Changing the selection in the form is supposed to then change what displays on the page. The page consists of a forum style post that allows users to fill out a textarea and then submit it into the MySQL database into a table called "posts." On this same page I then display the contents of the "posts" table.
What I have done is in the posts MySQL table, I have added a campaignID row. Then in my campaigns table I also have a campaignID row. The campaignID in campaigns table auto increments every time a campaign is created. Then with the earlier mentioned dropdown I can select the campaign I want, and it should then show all posts with the same campaignID as the campaign I selected.
I can verify that the $_SESSION[campaignID] is changing when i select the various options. Because when I do that and then save another post, it takes the session variable campaignID and saves it properly in the posts table.
Now what I need it to do, is when I change the drop down (which then changes $_SESSION[campaignID]) I need it to display the posts that share the same campaignID as the selected campaign. I am just not able to get it to display when trying to put a variable in my INNER JOIN query. I have a feeling the info I have provided may not be enough, but not sure what else I need to include here. Help?
Code that contains the INNER JOIN query and displays the various rows of posts table
UPDATED
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$campaignID = $_SESSION['campaignID'];
$result = mysqli_query($con,"SELECT posts.postDate, posts.postName, posts.postEntry FROM posts INNER JOIN campaigns ON posts.campaignID=" . $campaignID);
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
Without going into a use-prepared-statements rant...
You're not pulling the session variable, but assigning $campaignId to literally the string $_SESSION[campaignID].
Change your line:
$campaignID = '$_SESSION[campaignID]';
to:
$campaignID = $_SESSION['campaignID'];
Also, your query is going to generate a cross product unless you define something in your ON clause like:
SELECT posts.postDate, posts.postName, posts.postEntry FROM posts
INNER JOIN campaigns ON posts.campaignID= $campaignID
AND posts.campaignID= campaigns.id
The value should be in single quote ' so you may try this
posts.campaignID='" . $campaignID ."'"
instead of
posts.campaignID=" . $campaignID

PHP Mysql Left Join

<?php
$q = mysql_query("SELECT sub_cat.*, links.*
FROM links
LEFT JOIN sub_cat
ON links.p_id = sub_cat.id
WHERE sub_cat.p_id = '$id'
ORDER BY name ASC") or die (mysql_error());
while ($r = mysql_fetch_assoc($q))
{
$links_name = $r['name'];
$link_h3 = $links_name != '' ? '<h3>' . $links_name . '</h3>' : '';
//print $link_h3;
print '<pre>';
print_r ($r);
}
?>
I have two tables with rows like:
sub_cat
id
name
p_id
links
id
links
p_id
In sub cat i have movie categories, like foreign language movies, national movies, uncategorised movies and so on. In links table i have concrete movie links and depending on sub category.
The only thing is that i do not want dublicate titles (sub_cat.name).
result is:
Without Category www.moviesite.com
Without Category www.moviesite2.com
Without Category www.moviesite3.com
Foreign Movies www.moviesite1.bla
Foreign Movies www.moviesite2.bla
I want to be
Without Category www.moviesite.com
www.moviesite2.com
www.moviesite3.com
Foreign Movies www.moviesite1.bla
www.moviesite2.bla
and do not have any idea how to do this :(
any help appreciated.
To do the job, you have 2 solutions:
The first solution is to process your data before showing it, in order to group all movies by category.
You can do for example:
$moviesByCategory = array();
while ($r = mysql_fetch_assoc($q))
{
// Create the new sub array for the new category if necessary
if (!isset($moviesByCategory[$r['name']]))
$moviesByCategory[$r['name']] = array();
// Add the movie in the category
$moviesByCategory[$r['name']][] = $r['links'];
}
And then, you can now iterate on this new array like
foreach($moviesByCategory as $category => $movies)
{
// Print the category name
echo '<h1>' . $category . '</h1>';
// Print all movies of the category
foreach($movies as $movie)
echo '<h3>' . $movie . '</h3>';
}
The second solution is to modify the SQL query to group directly all movies that have the same category. You just have to use a GROUP BY clause on sub_cat.id and then apply an agregate function on all other fields in the select.
For performance aspect, the best solution is the SQL solution, but doing it with PHP will give you more flexibility for the presentation.
Try something like:
$lastSubcatName = "";
while ($r = mysql_fetch_assoc($q))
{
$links_name = $r['name'];
if($lastSubcatName != $links_name)
{
echo "<h1>".$links_name."</h1>";
$lastSubcatName = $links_name;
}
echo '<h3>' . $r['links'] . '</h3>';
}

List category names from database as menu

I want to create a menu from category names in database, so far I have this:
$list = "SELECT category FROM posts";
$rlist = mysql_query($list) or die(mysql_error());
while($rows = mysql_fetch_assoc($rlist))
{
$catname = $rows['category'];
echo '<li>' . $catname . '</li>';
}
Which lists everything but I need to make each only list once so its a menu.
Maybe you want
SELECT DISTINCT category FROM posts;
And assuming you want them ordered
SELECT DISTINCT category FROM posts ORDER BY category ;
If you want your category to be only listed once, you chould change your query to
SELECT DISTINCT(category) FROM POSTS

loop categories with assigned forums

forumcats: id, name
forums: id, name, cat_id
How can i join these together and print the forums assigned to the categories under them
Would really appriciate if someone could give me a hand
This is what i mean:
A category
A forum<
A forum
A 2nd category
Another forum
Another forum
Another forum
A 3rd catagory
Another forum
Another forum
Another forum
$reslt = mysql_query("select id, name, cat_id from forums");
while ($row=mysql_fetch_assoc($reslt)) {
echo "<h1>Category here</h1<";
echo "<h3>$row[name]</h3>";
}
You want this query:
SELECT forums.*, forumcats.name AS category_name
FROM forums
INNER JOIN forumcats ON (forums.cat_id = forumcats.cat_id)
Then when you loop through the results, you want to identify when you've moved on to a new category. For example:
$last_cat_id = null;
while ($row=mysql_fetch_assoc($reslt)) {
if ($last_cat_id != $row['cat_id']) {
echo '<h1>' . $row['category_name'] . '</h1>';
$last_cat_id = $row['cat_id'];
}
echo "<h3>$row[name]</h3>";
}

Categories