i have this code:
$Maxcount = 10;$count = 1;$query = "SELECT pages.paid,
pages.pag_desc,
pages.isActive,
pages.pag_pag_id,
pages.pag_type,
parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) { if ($count === 1) {
$query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
$query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid " }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id
INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid
WHERE uid = '$uid' AND $where AND pages.isActive=1";
$result = $db->query($query);
if (mysqli_num_rows($result)) {
$menu = array();
while ($row = $result->fetch_assoc()) {
$menu[] = $row;
}
}
and for showing the menu i used this recursive function :
foreach($menu as $row){
$x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc'];
}function recursive($x, $out) { if (is_array($x)){
//$out .= "<ul>";
foreach($x as $i => $data) {
if ($i === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">';
}
else
{
$out .= '<a href="#">';
}
$out .= $i;
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
the result is that its showing only the parent and the first child but in database the child have another child etc...
for example:
parent user have a child admin and admin have another child called editor
but its only showing user as a parent and admin as a child
any help please? thank u in advance
Related
As per usual I have spent the past few hours on this but cannot work out a better way. I have a CMS and need to create a sitemap. I have a function of the works but is messy and not really fit for purpose and was wondering if anyone had any suggestions. I under there are for loops and the likes but I'm not all that use how to use them yet. I have included the function I already have as well as a demo of the database.
tbl_website_posts
| ID | Title | Parent ID |
|-----|-------------------|-----------|
| 1 | Parent 1 | 0 |
| 2 | Parent 2 | 0 |
| 3 | Child of 2 | 2 |
Functions.php
function generate_sitemap($site_id) {
global $dbcon;
$return = '<ul>';
$icon = array('page' => 'fa-file-word', 'post' => 'fa-newspaper', 'event' => 'fa-calendar-star', 'calander' => 'fa-calendar-alt', 'training' => 'fa-award', 'people' => 'fa-user-alt', 'group' => 'fa-users-class');
$query_1 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='0'");
while ($array_1 = mysqli_fetch_array($query_1, MYSQLI_ASSOC)) {
$parent_id_1 = $array_1['id'];
$query_2 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_1'");
if (mysqli_num_rows($query_2) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_1['post_type']] . '"}\'>' . $array_1['post_title'];
$return .= '<ul>';
while ($array_2 = mysqli_fetch_array($query_2, MYSQLI_ASSOC)) {
$parent_id_2 = $array_2['id'];
$query_3 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_2'");
if (mysqli_num_rows($query_3) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_2['post_type']] . '"}\'>' . $array_2['post_title'];
$return .= '<ul>';
while ($array_3 = mysqli_fetch_array($query_3, MYSQLI_ASSOC)) {
$parent_id_3 = $array_3['id'];
$query_4 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_3'");
if (mysqli_num_rows($query_4) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_3['post_type']] . '"}\'>' . $array_3['post_title'];
$return .= '<ul>';
while ($array_4 = mysqli_fetch_array($query_4, MYSQLI_ASSOC)) {
$parent_id_4 = $array_4['id'];
$query_5 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_4'");
if (mysqli_num_rows($query_5) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_4['post_type']] . '"}\'>' . $array_4['post_title'];
$return .= '<ul>';
while ($array_5 = mysqli_fetch_array($query_5, MYSQLI_ASSOC)) {
$parent_id_5 = $array_5['id'];
$query_6 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_5'");
if (mysqli_num_rows($query_6) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_5['post_type']] . '"}\'>' . $array_5['post_title'];
$return .= '<ul>';
while ($array_6 = mysqli_fetch_array($query_6, MYSQLI_ASSOC)) {
$parent_id_6 = $array_6['id'];
$query_7 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_6'");
if (mysqli_num_rows($query_7) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_6['post_type']] . '"}\'>' . $array_6['post_title'];
$return .= '<ul>';
while ($array_7 = mysqli_fetch_array($query_7, MYSQLI_ASSOC)) {
$parent_id_7 = $array_7['id'];
$query_8 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_7'");
if (mysqli_num_rows($query_8) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_7['post_type']] . '"}\'>' . $array_7['post_title'];
$return .= '<ul>';
while ($array_8 = mysqli_fetch_array($query_8, MYSQLI_ASSOC)) {
$parent_id_8 = $array_8['id'];
$query_9 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_8'");
if (mysqli_num_rows($query_9) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_8['post_type']] . '"}\'>' . $array_8['post_title'];
$return .= '<ul>';
while ($array_9 = mysqli_fetch_array($query_9, MYSQLI_ASSOC)) {
$parent_id_9 = $array_9['id'];
$query_10 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_9'");
if (mysqli_num_rows($query_10) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_9['post_type']] . '"}\'>' . $array_9['post_title'];
$return .= '<ul>';
while ($array_10 = mysqli_fetch_array($query_10, MYSQLI_ASSOC)) {
$parent_id_10 = $array_10['id'];
$query_11 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_10'");
if (mysqli_num_rows($query_11) > 0) {
$return .= '<li>' . $array_10['post_title'];
$return .= '<ul>';
while ($array_11 = mysqli_fetch_array($query_11, MYSQLI_ASSOC)) {
$parent_id_11 = $array_11['id'];
$query_12 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_11'");
if (mysqli_num_rows($query_12) > 0) {
$return .= '<li>' . $array_11['post_title'];
$return .= '<ul>';
while ($array_12 = mysqli_fetch_array($query_12, MYSQLI_ASSOC)) {
$return .= '<li>' . $array_12['post_title'] . '</li>';
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_11['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_10['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_9['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_8['post_type']] . '"}\'>' . $array_8['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_7['post_type']] . '"}\'>' . $array_7['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_6['post_type']] . '"}\'>' . $array_6['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_5['post_type']] . '"}\'>' . $array_5['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_4['post_type']] . '"}\'>' . $array_4['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_3['post_type']] . '"}\'>' . $array_3['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_2['post_type']] . '"}\'>' . $array_2['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_1['post_type']] . '"}\'>' . $array_1['post_title'] . '</li>';
}
}
$return .= '</ul>';
return $return;
}
The function is also very limited to only going so many children in. If anyone has and suggestions on where I could start that would be amazing. Thank you in advance for your time.
Alan.
I'd use a recursive function for this.
Disclaimer: this is untested code I wrote in the StackOverflow editor, but it looks legit to me. YMMV, use it mostly for inspiration :-)
function generate_sitemap($site_id, $parent_id=0) {
global $dbcon;
$return = '';
$query = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id'");
// side note: once you get this working it will be worth the effort to turn
// this query into a prepared statement. Using variables in your query like
// this puts you at risk of a SQL injection attack.
if (mysql_num_rows($query) > 0) {
$return = '<ul>';
$icon = array('page' => 'fa-file-word', 'post' => 'fa-newspaper', 'event' => 'fa-calendar-star', 'calander' => 'fa-calendar-alt', 'training' => 'fa-award', 'people' => 'fa-user-alt', 'group' => 'fa-users-class');
while ($array = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array['post_type']] . '"}\'>' . $array['post_title'];
$return .= generate_sitemap($site_id, $array['id']);
$return .= '</li>';
}
$return .= '</ul>';
}
return $return;
}
So basically, start out assuming the parent won't have any child pages (initial $return value is an empty string, so we don't return an empty <ul></ul>).
Only if there are children, start building the <ul> with all child pages.
Inside the <li> for a child page, have the function call itself with the child's id as the new parent id. If the child has no child pages, the nested function call will return an empty string. Otherwise, it will return a <ul> with all of its children.
I want to change the video limit on my home page (getpopularvideos), but when I change the limit, it always only shows 20 videos.
What am I doing wrong?
protected function getPopularVideos($limit = NULL){
// Set Limit
if(!is_null($limit)) $this->limit = $limit;
// Construct URL
$part = "snippet,contentDetails,statistics";
$this->url = "https://www.googleapis.com/youtube/v3/videos?key={$this->key}&part=$part&chart=mostPopular&maxResults={$this->limit}";
if(!is_null($this->page)) $this->url = $this->url."&pageToken={$this->page}";
// Get Data
$data = Main::cache_get("popular_{$this->page}");
if($data == NULL){
$data = $this->http(TRUE);
Main::cache_set("popular_{$this->page}", $data); $limit = 24;
}
$i = 1;
$html = "<ul class='media'>";
foreach ($data->items as $yt) {
$html .= $this->listVideo($yt);
if($i%24 == 0) {
$html .= "</ul>{$this->ads(728)}<ul class='media'>";
}
$i ++;
}
$html .= "</ul>";
$this->pagination = "<ul class='list-inline'>";
if(isset($data->prevPageToken)){
$this->pagination .= "<li ><a href='{$this->config["url"]}/popular/{$data->prevPageToken}' class='btn btn-ghost'><i class='btn-icon btn-icon--left ninety9lives-youtube-play'></i> Previous</a></li>";
}
if(isset($data->nextPageToken)){
$this->pagination .= "<li ><a href='{$this->config["url"]}/popular/{$data->nextPageToken}' class='btn btn-ghost'><i class='btn-icon btn-icon--left ninety9lives-youtube-play'></i> Next</a></li>";
}
$this->pagination .= "</ul>";
return $html;
}
protected function getUserVideos4Home($q, $limit = NULL){
if(!is_null($limit)) $this->limit = $limit;
$part = "snippet";
$this->url = "https://www.googleapis.com/youtube/v3/search?key={$this->key}&part=$part&order=date&type=video&channelId={$q}&maxResults={$this->limit}";
if(!is_null($this->page)) $this->url = $this->url."&pageToken={$this->page}";
$data = $this->http(TRUE);
$i = 1;
$return = new stdClass();
$return->title = $data->items[0]->snippet->channelTitle;
$html .= "";
$vids = array();
foreach ($data->items as $yt){
$vids[] = $yt->id->videoId;
}
$videos2 = implode(",", $vids);
$part = "snippet,contentDetails,statistics";
$this->url = "https://www.googleapis.com/youtube/v3/videos?key={$this->key}&part={$part}&id={$videos2}";
$data2 = $this->http(TRUE);
$durations = Main::durations($vids);
foreach ($data->items as $yt) {
$html .= $this->listSearchVideo12($yt,$durations[$yt->id->videoId]);
if($i%6 == 0) {
$html .= "</div><div class='adblock'><div class='img'>Google AdSense<br>336 x 280</div></div><div class='list'>";
}
$i ++;
}
$html .= "";
return $html;
}
This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }
I am hoping to convert some of my basic settings like my navigation, mysql settings (host, username, password) to SQLite but it seems from something I read this morning there is no equivalent for the command PDO::FETCH_ASSOC. Is there another way for me to do this query that would be compatible with SQLite?
<?
$sql = "SELECT * FROM menu_items WHERE status = 'ACTIVE' ORDER BY menu_parent_id ASC, sortorder ASC, menu_item_name ASC";
$query = $db->query($sql);
$menu_items = array();
while($data = $query->fetch(PDO::FETCH_ASSOC)) {
if($data['menu_parent_id'] == 0) {
$menu_items[$data['menu_item_id']] = array();
$menu_items[$data['menu_item_id']]['menu_item_id'] = $data['menu_item_id'];
$menu_items[$data['menu_item_id']]['name'] = $data['menu_item_name'];
$menu_items[$data['menu_item_id']]['url'] = $data['menu_url'];
$menu_items[$data['menu_item_id']]['fontawesome'] = $data['fontawesome'];
$menu_items[$data['menu_item_id']]['children'] = array();
} else if($data['menu_parent_id'] != 0) {
$tmp = array();
$tmp['menu_item_id'] = $data['menu_item_id'];
$tmp['name'] = $data['menu_item_name'];
$tmp['url'] = $data['menu_url'];
$tmp['fontawesome'] = $data['fontawesome'];
array_push($menu_items[$data['menu_parent_id']]['children'],$tmp);
unset($tmp);
}
}
function create_list($arr)
{
$html = "";
foreach($arr as $key => $value) {
//Here the menu item has children
if(count($value['children']) > 0) {
$html .= '<!-- PARENT --> <li class="mm-dropdown"> <i class="menu-icon fa '. $value['fontawesome']. '"></i><span class="mm-text">'.$value['name'].'</span>
<ul>';
// Here it is the child
foreach($value['children'] AS $child) {
$html .= '
<!-- child --><li><i class="menu-icon fa '. $child['fontawesome']. '"></i>'.$child['name'].'</li>';
}
$html .= ' </ul>
';
} else{
$html .= ' <a class="menu-icon fa '.$value['fontawesome'].'" id="'.$value['menu_item_id'].'" >'.$value['name'].'</a>';
}
}
return $html;
}
?>
So your $query is SQLite3Result ??
try this way then:
while($data = $query->fetchArray(SQLITE3_ASSOC)) {
I have a function that generates a category menu.
What i'm trying to do is to group two category in one ul.
The problem is almos fixed but i'm getting an empty ul ( see the pic).
How can I fix this ?
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}
Try this:
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0 && !empty($i)){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}
just start counting from -1
$i = -1;