display sub-menu of parent menu from parent id php - php

<?php
$page= $objPage-> get_page();
$id = isset($_GET['id']);
$child= $objPage-> get_child_page($id);
?>
<ul class="nav nav-tabs">
<li role="presentation" class="dropdown">
<?php
while($row = mysql_fetch_array($page) ) { ?>
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria- expanded="false">
<?php echo $row['title']; ?> <span class="caret"></span>
</a>
<?php } ?>
</li>
</ul>
Function get_page:
function get_page($id="") {
$sql = "SELECT "; if( !empty($field) ){
$sql .=" title FROM page WHERE page_id>1 " ;
} else {
$sql .= " * FROM page ";
}
if( !empty($id) ){
$sql .=" WHERE page_id=".$id;
} else {
$sql .=" WHERE parent_id= -1";
}
$result = mysql_query($sql) or die( mysql_error() );
if( !empty($id) && $result ){
$value = mysql_fetch_array($result) ;
return $value;
}
return $result;
}
Function get_child_page:
function get_child_page($id){
$sql = "SELECT * FROM page WHERE parent_id= $id";
$result = mysql_query($sql);
return $result;
}
i got the parent menu which has the parent id -1 but i want to display the child menu from parent id .. any solution ??

Related

header hover problem any one have solution?

<?php
$q = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=? ORDER BY menu_order ASC");
$q->execute(array(0));
$res = $q->fetchAll();
foreach ($res as $row) {
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
echo ' <li class="header_nav-list_item dropdown">';
if($row['page_id'] == 0) {
if($row['menu_url'] == '') {
$final_url = 'javascript:void(0);';
} else {
$final_url = $row['menu_url'];
}
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
href="<?php echo $final_url; ?>"><?php echo $row['menu_name']; ?><?php if($total) {echo ' <i class="icon-angle-down icon"></i>';}; ?></a>
<?php
} else {
$r = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=? ");
$r->execute(array($row['page_id']));
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
data-bs-toggle="collapse"
data-bs-target="#Menu"
data-trigger="dropdown"
aria-expanded="false"
aria-controls="Menu"
data-page="" href="<?php echo BASE_URL.URL_PAGE.$row1['page_slug']; ?>"><?php echo $row1['page_name']; ?>
<?php if($total) {echo '<em></em>';}; ?></a>
<?php
}
}
// Checking for sub-menu
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
if($total) {
echo ' <div class="dropdown-menu collapse"><ul>';
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
//
echo '<li class="dropdown-list">';
if($row1['page_id'] == 0) {
if($row1['menu_url'] == '') {
$final_url1 = 'javascript:void(0);';
} else {
$final_url1 = $row1['menu_url'];
}
?>
<a class="dropdown-item nav-item" href="<?php echo $final_url1; ?>"><?php echo $row1['menu_name']; ?></a>
<?php
} else {
$s = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=?");
$s->execute(array($row1['page_id']));
$res2 = $s->fetchAll();
foreach ($res2 as $row2) {
?>
<a class="dropdown-item nav-item" href="<?php echo BASE_URL.URL_PAGE.$row2['page_slug']; ?>"><?php echo $row2['page_name']; ?></a>
<?php
}
}
echo '</li>';
//
}
echo '</ul></div>';
}
echo '</li>';
}
?>
This is my header code can any one tell me or updATE THIS CODE FOR ME
I WANT TO SOLVE MY PROBLEM

Echoing html if data from SQL if statement true

So I've been struggling at how to echo this set of HTML with users that are set as admin from my database. I've looked at quite a few places for information but I'm struggling to get it to work. Perhaps I'm doing something really stupid. Thanks for your help.
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
?>
I think that you forgot to put the while between {}. Here is the correct code:
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
}
?>

How to do pagination inside php file which is passed by ajax

I am trying to do pagination in my php file fetch_data.php which is called from a ajax function in index.php but it is not showing the required result
Here is my code from fetch_data.php
<?php
require 'dbserver.inc.php';
$query = "
SELECT * FROM detail WHERE status = '1'
";
$result = mysqli_query($conn,$query);
$number = mysqli_num_rows($result);
$results_per_page = 1;
$number_of_pages = ceil($number/$results_per_page);
if (!isset($_GET['page'])) {
$page = 1;
echo"1";
} else {
echo"2";
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*$results_per_page;
if(isset($_POST["action"]))
{
$query = "
SELECT * FROM detail WHERE status = '1'
AND LIMIT" . $this_page_first_result . ',' . $results_per_page ;
if(isset($_POST["jobtype"]))
{
$jobtype_filter = implode("','", $_POST["jobtype"]);
$query .= "
AND Job IN('".$jobtype_filter."')";
}
if(isset($_POST["salary"]))
{
$salary_filter = implode("','", $_POST["salary"]);
$query .= "
AND Salary IN('".$salary_filter."')";
}
if(isset($_POST["category"]))
{
if($_POST["category"] == 'Choose Category')
{
$query .= "";
}
else
{
$category = $_POST["category"];
$query .= "
AND JobCat like('".$category."')";
}}
if(isset($_POST["location"]))
{
if($_POST["location"] == 'Choose Location')
{
$query .= "";
}
else
{
$location = $_POST["location"];
$query .= "
AND City like('".$location."')";
}}
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($result)){
echo'
<!-- Single Employers List -->
<div class="candidate-list-layout" >
<div class="cll-wrap">
<div class="cll-thumb">
<a href="company-detail.html">';?>
<img class="img_responsive" alt="" src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['Logo']); ?>" /> <?php echo'</a>
</div>
<div class="cll-caption">
<h4>'.$row['Name'].'<span><i class="ti-briefcase"></i>'.$row['Title'].'</span></h4>
<ul>
<li><i class="ti-location-pin cl-danger"></i>'.$row['City'].' fetch ,'.$row['Country'].'</li>
<li><i class="ti-medall cl-success"></i>Established: 1984</li>
</ul>
</div>
</div>
';}
echo'<div class="row mrg-0">
<ul class="pagination">';
for($page = 1 ; $page<=$number_of_pages;$page++){
echo'
<li class="active">'.$page.'</li>
';}
echo' <li><i class="fa fa-ellipsis-h"></i></li>
</ul>
</div>';
}
?>
i want to filter the data a also due to which i have to include these other lines of codes and it is also not redirercting to other page except the one it is currently in and when included these filter code it is showing boolean error on mysqli_query too.

Adding categories & subcategories to a blog

Ok, I have done lots of research and looked at lots of stackoverflow questions but none have answered my question.
I'm building a simple blog and am at the moment i'm trying to build a simple categories/subcategories system but have hit an obstacle in making the subcategories load under their parent. If you know an easier method to the one i am doing below please let me know.
Here is how my db is structured:
id || name || parent_id || status
------------------------------------------
1 || category1 || NULL || 1
2 || subcategory1 || 1 || 1
Then i have my model code:
public function getCategories()
{
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = NULL');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
public function getSubCategories($parent)
{
$bind = [':parent' => $parent];
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = :parent');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
This is my controller code:
public function error()
{
$getSettings = $this->setting->getAll();
$getCategories = $this->setting->getCategories();
$getSubCategories = $this->setting->getSubCategories();
if(is_array($getCategories[0]) ) {
$isCategory = true;
} else {
$isCategory = false;
}
if(is_array($getSubCategories[0]) ) {
$isSubCategory = true;
} else {
$isSubCategory = false;
}
$data = [
'settings' => $getSettings,
'mainCategory' => $getCategories,
'subCategory' => $getSubCategories,
'isCategory' => $isCategory
'isSubCategory' => $isSubCategory
];
$this->view('index', $data);
}
Then I have my templatecode which is where im trying to do the foreach and the top category works fine but i just can't figure out how i do the subcategories. Before i started using the MVC model, i would simply just put the class inside the main category foreach and then put the id but now its all done in the controller so i cannot figure out how i do subcategories using the MVC model.
<?php foreach($data['mainCategory'] as $category) : ?>
<li class="dropdown">
<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#"><?php echo $category['name']; ?></a>
<div class="dropdown-menu" role="menu">
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 fas fa-box"></i> All <?php echo $category['name']; ?></a>
<div class="dropdown-divider" role="presentation"></div>
<?php foreach($data['subCategory'] as $scategory) : ?>
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 <?php echo $scategory['icon']; ?>"></i> <?php echo $scategory['name']; ?></a>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
Thanks very much!
$query = 'SELECT id, parent_id, name, icon FROM categories ORDER BY name';
$result = mysqli_query($conn, $query) OR trigger_error($query.'<br>'.mysqli_error($conn),E_USER_ERROR);
$refs = Array();
$categories = Array();
while($row = mysqli_fetch_assoc($result))
{
$thisref = &$refs[$row['id']];
$thisref['name'] = $row['name'];
$thisref['icon'] = $row['icon'];
if($row['parent_id']) $refs[$row['parent_id']]['children'][$row['id']] = &$thisref;
else $categories[$row['id']] = &$thisref;
}
echo '<ul class="nav navbar-nav mr-auto">';
foreach($categories as $category)
{
echo '<li class="dropdown">';
echo is_array($category['children'])
? '<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#">'.$category['name'].'</a>'.sub($category['children'])
: $category['name'];
echo '</li>';
}
echo '</ul>';
function sub(&$subCat)
{
echo '<div class="dropdown-menu" role="menu">
<a class="dropdown-item hide-me" role="presentation" href="#"><i class="fas fa-fire mr-2"></i> Most Popular </a>
<div class="dropdown-divider hide-me" role="presentation"></div>';
foreach($subCat as $id => $subcategory)
{
echo '<a class="dropdown-item" role="presentation" href="'.FULL_ROOT.'/category/'.$id.'/"><i class="'.$subcategory['icon'].' mr-2"></i>'.$subcategory['name'].'</a>';
}
echo '</div>';
}

select data from seperate tables with same id

I have 2 tables: mb_category and mb_items, I'm trying to select items from mb_items with the same ID as the category they are assigned to in mb_category.
mb_category has 2 rows: id and category.
mb_items has 4 rows: id, item_name, item_price, and mb_category_id
What I want to do is select every item in mb_items with their corresponding category ID and echo them in the correct dropdown.
Here's what I have currently:
<ul class="nav">
<?php
$category = $db->read('mb_category', '*', '', 'ORDER BY id ASC');
// echo $category["count"] . "<br />";
// print_r($category);
foreach ($category["results"] as $row) {
?>
<li class="button-dropdown">
<a href="javascript:void(0)" class="dropdown-toggle">
<?php echo $row->category; ?> <span>▼</span>
</a>
<ul class="dropdown-menu">
<?php
$cid = $db->read('mb_category', 'id', '', '');
$item = $db->read('mb_items', '*', 'mb_category_id="$cid"', '');
// var_dump($item);
// echo $item["count"] . "<br />";
// print_r($item);
foreach ($item["results"] as $irow) {
?>
<li>
<a href="#">
<?php
echo $irow->item_name;
?>
</a>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
And this is my SELECT:
public function read($table_name, $cols, $where = NULL, $limit = NULL) {
//GET $cols FROM $table_name WHERE $where returns 0 for false and fetch_object() for true
$query = "SELECT " . $cols . " FROM " . $table_name;
if ($where) {
$query .= " WHERE " . $where;
}
if ($limit) {
$query .= " " . $limit;
}
//echo $query;
$sql = $this->query($query);
if ($sql) {
$data = array();
$data['count'] = $sql->num_rows;
while ($row = $sql->fetch_object()) {
$data['results'][] = $row;
}
return $data;
}
return false;
}
}
I can suggest you 2 things (I didn't test the code I'm giving you, it's just to show you the idea).
1) You are doing too many requests, you only need one :
SELECT c.id AS category_id, c.category AS category_name, i.id AS item_id, i.item_name
FROM mb_category c
LEFT JOIN mb_items i ON c.id = i.mb_category_id
2) format your data and use it to display your list :
<?php
// format data
$categories = [];
foreach ($category["results"] as $row) {
if (empty($categories[$row[category_id]])) {
$categories[$row[category_id]] = [
'id' => $row[category_id],
'name' => $row['category_name'],
'items' => [],
];
}
if (!empty($categories[$row[item_id]])) {
$categories[$row[category_id]]['items'][] = [
'id' => $row['item_id'],
'name' => $row['item_name'],
];
}
}
?>
<ul class="nav">
<?php
// display data
foreach ($categories as $categoryId => $category) {
?>
<li class="button-dropdown">
<a href="javascript:void(0)" class="dropdown-toggle">
<?php echo $category['name']; ?> <span>▼</span>
</a>
<ul class="dropdown-menu">
<?php foreach ($category['items'] as $item) { ?>
<li>
<a href="#">
<?php
echo $item['name'];
?>
</a>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>

Categories