PHP MySQL Multidimensional Array - Dropdown Menu - php

I want to create my dropdown menu from a mysql query, but i'm having trouble with the sub-items.
My basic table:
NavigationID ParentID Name Url
1 1 Home home
2 2 About about
3 3 Products products
4 3 Category1 #
5 3 Category2 #
6 4 Product1 #
7 5 Product2 #
My simple MySQL Query and adding to array:
class Navigation{
private $data;
public function __construct($par){
if(is_array($par))
$this->data = $par;
}
public function __toString(){
return '<li>'.$this->data['Name'].'</li>';
}
}
$query = mysql_query("SELECT * FROM Navigation n") or die(mysql_error());
$num = mysql_num_rows($query);
$menuitems = array();
while($row = mysql_fetch_assoc($query)){
$menuitems[] = new Navigation($row);
}
echo '<div id="nav"><ul>';
foreach($menuitems as $item){
echo $item;
}
echo '</ul></div>';
The result of this is:
<div id="nav"><ul>
<li>Home</li>
<li>About</li>
<li>Products</li>
<li>Category1</li>
<li>Category2</li>
<li>Product1</li>
<li>Product2</li>
</ul></div>
But what I would REALLY like is this:
<div id="nav"><ul>
<li>Home</li>
<li>About</li>
<li>Products
<ul>
<li>Category1
<ul>
<li>Product1</li>
</ul>
</li>
<li>Category2
<ul>
<li>Product2</li>
</ul>
</li>
</ul>
</li>
</ul></div>
How can I achieve this result? I've tried many other examples, but none seems to help me. Maybe I'm not searching for the right thing.

Why to make it complicated, this could be done with a very simple recursive function.
This is what I did in my local machine. I have the connection parameter and then called a function
bulit_tree(0);
In this function it will check if the argument is 0 then select all
the item where id and parentid is same.
Then loop through and use the recursive function and generate sub tree.
Need to make sure that the $con is accesible within the function.
$con = mysql_connect("localhost","testuser","testpass");
$db_selected = mysql_select_db('testdb', $con);
if (!$db_selected) {
die ('Can\'t use testdb : ' . mysql_error());
}
bulit_tree(0);
function bulit_tree($pid=0){
global $con ;
if($pid == 0 ){
$qry = "select * from Navigation where NavigationID = ParentID";
$q = mysql_query($qry,$con);
if(mysql_num_rows($q) > 0 ){
echo '<ul>';
while($row = mysql_fetch_assoc($q)){
echo '<li>'.$row["Name"].'';
bulit_tree($row["NavigationID"]);
echo '</li>';
}
echo '</ul>';
}
}else{
$qry = "select * from Navigation where ParentID = ".$pid." AND NavigationID <> ".$pid;
$q = mysql_query($qry,$con);
if(mysql_num_rows($q) > 0 ){
echo '<ul>';
while($row = mysql_fetch_assoc($q)){
echo '<li>'.$row["Name"].'';
bulit_tree($row["NavigationID"]);
echo '</li>';
}
echo '</ul>';
}
}
}

You might need to restructure your DB first. Consider a join table. This comes handy especially if your Product falls into multiple categories.
Master table:
NavigationID Name Url
1 Home home
2 About about
3 Products products
4 Category1 #
5 Category2 #
6 Product1 #
7 Product2 #
Lookup Table:
NavigationID ParentId
1 1
2 2
3 3
4 3
5 3
6 4
7 5
Then in your class, you can make it structured like:
<?php
class Navigation{
private $menuitems;
public function __construct($par){
if(is_array($par))
$this->menuitems = $par;
}
public function __toString() {
$this->printNavigation($this->menuitems);
}
private function printMenuItem($menu) {
echo '<li>'.$menu->name.'';
if(count($menu->children)) {
print printNavigation($menu->children);
}
'</li>';
}
private function printNavigation($menuItems) {
echo "<ul>";
foreach ($menuitems as $menu {
$this->printMenuItem($menu);
}
echo "</ul>";
}
}
class MenuItem{
private $url;
private $name;
private $children;
public function __construct($par){
if(is_array($par)) {
$this->url = $par['url'];
$this->$name = $par['name'];
$this->children = $this->fetchChildren($par['NavigationID']);
}
}
function fetchChildren($id) {
$query = mysql_query("SELECT * from navigation n INNER JOIN Lookup l on l.parentID = n.NavigationID
WHERE n.NavigationID = $id") or die(mysql_error());
$num = mysql_num_rows($query);
if($num > 0) {
while($row = mysql_fetch_assoc($query)){
$this->children[] = new MenuItem($row);
}
}
}
}
$query = mysql_query("SELECT * from navigation n INNER JOIN Lookup l on l.NavigationID = n.NavigationID
WHERE l.NavigationID = l.parentIDn
AND l.NavigationID != n.NavigationID") or die(mysql_error());
$num = mysql_num_rows($query);
$menuitems = array();
while($row = mysql_fetch_assoc($query)){
$menuitems[] = new MenuItem($row);
}
$navigation = new Navigation($menuitems);
echo "<div id='nav'>$navigation</div>";

Thanx for all the comments. I've gone with Agha's suggestion of using a recursive function.
http://crisp.tweakblogs.net/blog/317/formatting-a-multi-level-menu-using-only-one-query.html

Related

push array data in every index of an array using codeigniter php

I have two tables sport_tbl, match_tbl. In sport_tbl, i defined sport_name such as cricket. In match_tbl, I have match_name,match_date,sport_id.
I want to show match_date of every sport_name (ex. i am showing match_date list for cricket sport and i want to show every date has match_name list).
I want to show one distinct match_date.
Image
my controller code:-
$url = 'cricket' // for example first sport_name
$data['getSportMatch'] = $this->user_model->getSportMatch($url);
my model code:-
public function getSportMatch($sport)
{
$query = $this->db->get_where('match_tbl',array('sport_name' => $sport));
if($query->num_rows > 0)
{
foreach($query->result() as $item){
$data[] = $item;
}
return $data;
}
}
my code in view:-
<div><?php foreach($getSport as $item): ?><h4><?= $item->sport_name; ?></h4><div><?= foreach($getSportMatch as $item): ?>
match_date)) ?>here i want to show list match_name of every match_date
My table structure images
1) sport_tbl
2) match_tbl
3) another match_tbl
you can solve this in model easily. if i did not understand wrong . you need 2 function in model.
1. will get sport names
2. will get matches of given sport name
//model functions
function get_sports(){
$data = array();
$sports = $this->db->select('sport_name')->from('sport_tbl')->get()->result();
if($sports)
{
foreach($sports as $sport){
$data[$sport->sport_name] = $this->get_matches($sport->sport_name);
}
return $data;
}
}
function get_matches($sport_name){
$matches = $this->db->select('*')->from('match_tbl')->where('sport_name',$sport_name)->get()->result();
return $matches;
}
so in view data will be something like this
$data => array(
'cricket'=> array(0 => array(
'match_id' => 11,
'sport_id' = 2 .....
)))
Try this coding ...
public function getSportMatch($sport)
{
$query = $this->db->query("SELECT * FROM sport_tbl as st INNER JOIN match_tbl as mt ON st.sport_id = mt.sport_id WHERE st.sport_name ='".$sport."'");
if($query->num_rows > 0)
{
$query_result = $query->result_array();
$final_result = array();
foreach($query_result as $result ) {
$date = $result['match_date'];
$final_result[$date][] = $result;
}
return $final_result;
}
}
View Coding :-
if(isset($final_result)) {
foreach($final_result as $result) {
echo $result['match_date']; // First display match date
if(isset($result['match_date'])) {
foreach($result['match_date'] as $match) {
echo $match['match_name']; // Second listout the match name
}
}
}
}

Loop through PDO statment fetch(PDO::FETCH_OBJ) using a class

I have class called posts in a separate file :
<?php
class POSTS {
//Start of class properties:
private $db_connection;
public $post_id;
public $section_id;
public $user_id;
public $post_title;
public $post_details;
public $post_date;
public $post_category;
public $post_display;
public $num_of_rows;
public function getRelatedPosts($section_name, $category, $display) {
$stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
$stm->bindParam(":Section_name", $section_name);
$stm->bindParam(":Category", $category);
$stm->bindParam(":Display", $display);
$stm->execute();
$this->num_of_rows = $stm->rowCount();
if ($this->num_of_rows >= 1) {
$post_data = $stm->fetch(PDO::FETCH_OBJ);
$this->post_id = $post_data->id;
$this->section_id = $post_data->section_id;
$this->user_id = $post_data->user_id;
$this->post_title = $post_data->title;
$this->post_details = $post_data->details;
$this->post_date = $post_data->date;
$this->post_category = $post_data->category;
$this->post_display = $post_data->display;
}
}
}
?>
Then I want to loop through the results in my Index file:
$section_name = 'PHP';
$display = 'yes';
$POSTS->getRelatedPosts($section_name, $category $display);
$num_of_rows = $POSTS->num_of_rows;
if ($num_of_rows >= 1) {
for ($m=1; $m<=$num_of_rows; $m++) {
$post_id = $POSTS->post_id;
$section_id = $POSTS->section_id;
$user_id = $POSTS->user_id;
$post_title = $POSTS->post_title;
$post_details = $POSTS->post_details;
$post_date = $POSTS->post_date;
?>
<div id="related_post">
<h4><?php echo $post_title;?></h4>
<p><?php echo $post_details;?></p>
</div>
<?php
}
} else {
echo 'Sorry no related posts now!';
}
Unfortunately The results are only one record repeated as many as the $num_of_rows variable equal.
I tried some different ways with fetch methods like:
fetchAll() and others styles but always the result is an error or only one record repeated.
Someone help me with my code please.
If you want to loop, try looping in your method:
public function getRelatedPosts($section_name, $category, $display)
{
$stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
$stm->bindParam(":Section_name", $section_name);
$stm->bindParam(":Category", $category);
$stm->bindParam(":Display", $display);
$stm->execute();
$this->num_of_rows = $stm->rowCount();
if ($this->num_of_rows >= 1) {
while($post_data = $stm->fetch(PDO::FETCH_OBJ)) {
$this->post_id[] = $post_data->id;
$this->section_id[] = $post_data->section_id;
$this->user_id[] = $post_data->user_id;
$this->post_title[] = $post_data->title;
$this->post_details[] = $post_data->details;
$this->post_date[] = $post_data->date;
$this->post_category[] = $post_data->category;
$this->post_display[] = $post_data->display;
}
}
}
Your loop would likely be something like:
for ($m=1; $m<=$num_of_rows; $m++) {
$post_id = $POSTS->post_id[$m];
$section_id = $POSTS->section_id[$m];
$user_id = $POSTS->user_id[$m];
$post_title = $POSTS->post_title[$m];
$post_details = $POSTS->post_details[$m];
$post_date = $POSTS->post_date[$m];
?>
<div id="related_post">
<h4><?php echo $post_title;?></h4>
<p><?php echo $post_details;?></p>
</div>
<?php
}
In my class I should use the following instead of the current one:
$results = $stm->fetchAll(PDO::FETCH_OBJ);
I will not get any advantages of my current class properties.
In my index file I will loop through the $results using foreach loop as the following:
foreach($results as $post){
$post_title = $post->title;
$post_details = $post->details;
}
and so on...
I'm experimenting with PDO and I had the same issue with PDO::FETCH_OBJ
I'm using PHP 5.6 in xampp 5.6.30
needless to say that
- the DB name is animals
- it has three columns only : animal_id, animal_type, animal_name
the following test code works fine and outputs all the records (that in my test DB are only eleven)
$stmt = $dbh->prepare("SELECT * FROM animals");
$stmt->execute();
if($stmt->rowCount() > 0) {
while($obj = $stmt->fetch(PDO::FETCH_OBJ)) {
echo $obj->animal_type . " " . $obj->animal_name . "<br>";
}
}
perhaps you may insert the counters inside the loop or, even better, make the query to properly limit the range.
Anyway, the code above, as expected outputs what follow
kookaburra bruce
emu bruce
goanna bruce
dingo bruce
kangaroo bruce
wallaby bruce
wombat bruce
koala bruce
kiwi cambiato
pippo zollo
pippz zoll2

Multiple foreach - first result not displaying

I am playing around with the below code, I am making my personal "music database". In my mysql I have the following tables:
Music Categories
Music Artists
Music Tracks
The code below works fine except it doesn't display the first track of the database, it skips it, but it displays all the rest just fine. If I take the below example, the data displayed is as follows:
Category: 70's
artist2 trackname2
Category: 90's
artist3 trackname3
So, the first track get's skipped. I have been playing around with the code, but I cannot get it to work. It should be displayed like this:
Category: 70's
artist1 trackname1
artist2 trackname2
Category: 90's
artist3 trackname3
Example of how it looks like in the database:
music_categories:
musicID name
1 70s
2 80s
3 90s
music_artists:
artistID name
1 artist1
2 artist2
3 artist3
music_tracks:
id musicID artistID track
1 1 1 trackname1
2 1 2 trackname2
3 3 1 trackname3
Each music track has one artist and one category. I am using the code below in my classes.php file:
public function grabResults($table)
{
$result = 'SELECT * FROM '.$table;
$query = $this->mysqli->query($result);
if($query)
{
while($row = $query->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
else {
return false;
}
}
And this is the code itself:
$categories = $data->grabResults(music_categories);
$artists = $data->grabResults(music_artists);
$tracks = $data->grabResults(music_tracks);
$catName = '';
$counter = 0;
foreach ($categories as $category)
{
foreach ($tracks as $track)
{
if($category['name'] != $catName)
{
$countID = $category['id'];
$total = $data->mysqli->query("SELECT `musicID` FROM `music_tracks` WHERE `musicID` = '$countID' ");
$totalCount = $total->num_rows;
if($totalCount > 0)
{
echo $category['name'];
}
$catName = $category['name'];
$counter = 0;
}
if($counter > 0)
{
if($category['id'] == $track['mid'])
{
foreach ($artists as $artist)
{
if($track['artistID'] == $artist['id'])
{
echo $artist['name'];
}
}
echo $track['track'];
echo "<br>";
}
}
$counter++;
}
}

Nested category parent id

i have mysql table (exe_categories)
| id, title, parent_id, extension |
`<ul>
<li>Parentcat</li>
<ul>
<li>ParentSub</li>
<li>ParentSub</li>
<li>ParentSub</li>
</ul>
<li>Parentcat</li>
<ul>
<li>ParentSub</li>
<li>ParentSub</li>
<li>ParentSub</li>
</ul>
</ul>`
and i want to show a nested category list and subcategory using 2 arguments
first-argument is (parent_id) (0)
AND second argument is (extension) 'com_extension'
I want to show up the nested categories titles and path for the 2 arguments above
i have used this code below and works fine but i cant have another argument for extension argument.
<?php
function query($parent_id) { //function to run a query
$query = mysql_query ( "SELECT * FROM exe_categories WHERE parent_id=$parent_id");
return $query;
}
function has_child($query) { //This function checks if the menus has childs or not
$rows = mysql_num_rows ( $query );
if ($rows > 0) {
return true;
} else {
return false;
}
}
function fetch_menu($query) {
while ( $result = mysql_fetch_array ( $query ) ) {
$id = $result ['id'];
$extension = $result['com_bids'];
$title = $result ['title'];
$menu_link = $result ['menu_link'];
echo "<li><a href='{$menu_link}'><span>{$title}</span></a>";
if (has_child ( query ( $id ) )) {
echo "<div>";
echo '<ul role="menu" class="fofc">';
fetch_menu ( query ( $id) );
echo "</ul>";
echo "</div>";
}
echo "</li>";
}
}
fetch_menu (query(1)); //call this function with 1 parent id
?>
the code above will fetch list of categories starting from parent_id=0 which works great only that i cant have another rule for my extension column .
any help will be appreciated :)
The Html output is like this
Parentcat
ParentSub
ParentSub
ParentSub
Parentcat
ParentSub
ParentSub
ParentSub

CodeIgniter: Displaying data from two tables in one loop

Let's say I have these two database tables:
blog comments
------- ----------
blog_id comment_id
title blog_id
content comment
Now I'd like to run through the 3 latest blog entries and display the title, content and the number of comments made on that entry.
For that, I created a Blog_model:
function get_entries($n)
{
if ($n < 1) {$n = 1;}
$this->db->order_by("blog_id", "desc");
$q = $this->db->get('blog', $n);
if($q->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
The model is loaded in the controller and passed on to the view:
$this->load->model('Blog_model');
$data['blog_rows'] = $this->Blog_model->get_entries(3);
$this->load->view('blog_view');
That gives me the three latest blog entries. I can now loop through the rows and display the content in the view like this:
<?php foreach ($blog_rows as $row): ?>
<h2><?=$row->title;?></h2>
12>
<p><?=$row->content;?></p>
<?php endforeach; ?>
So far so good. But now the tricky part:
I'd like to display the number of comments associated with the displayed blog entry. How would I accomplish that, adhering to the CodeIgniter practices?
Here are two proposals for the model method: the first does a query for each blod row, while the second does an unique query wich is faster. the second query is not tested.
Model 1
function get_entries($n)
{
if ($n < 1) {$n = 1;}
$this->db->order_by("blog_id", "desc");
$q = $this->db->get('blog', $n);
if($q->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
foreach($data AS &$blog_e)
{
$q = "SELECT COUNT(*) AS count FROM comments WHERE blog_id = ?";
$query = $this->db->query($q,array($blog_e->blog_id);
$ncomments = $query->result_array();
$blog_e->n_comments = $ncomments[0]['count'];
}
}
Model 2
function get_entries($n)
{
if ($n < 1) {$n = 1;}
$q = "SELECT blog.blog_id,title,content,COUNT(comment_id) AS n_comments
FROM blog JOIN comments ON blog.blog_id = comments.blog_id
GROUP BY blog.blog_id,title,content
ORDER BY blog.blog_id DESC
LIMIT 0,?"
$query = $this->db->query($q,array($n));
if($query->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
View
<?php foreach ($blog_rows as $row): ?>
<h2><?=$row->title;?></h2>
<?=$row->n_comments?>>
<p><?=$row->content;?></p>
<?php endforeach; ?>

Categories