I'm working on a dropdown menu that is items are rows from a table called notifications each row has a type column and each type have a specific output ( echo ) so to output every row as an item in the menu I looped for each item in the MySQL Array but when I do those items shows in the dropdown menu but they are empty like this:-
Code:-
// notifications function here //
// unreaded notifications count to echo in the bootstrap4 badge //
$unreadednotifications = "SELECT * from `notifications` where `status` = 'unread' order by `date` DESC";
$unreadedcount = count(fetchAll($unreadednotifications));
// all notifications to sort in the dropdown menu //
$notifications = "SELECT * FROM `notifications` WHERE accountid = '8' ORDER BY `date` DESC";
$notificationscount = count(fetchAll($notifications));
// notification function ends here//
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="notificationsdrop" onclick="badgefade()" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<div style="font-size: 12px;"><i class="fa fa-bell fa-3x bell" aria-hidden="true"></i><?if($unreadedcount > 0){echo'<span class="badge badge-notify" id="notificationsBadge" style="font-size:15px;">'.$unreadedcount.'</span>';}?></div></a>
<div class="dropdown-menu notifications" aria-labelledby="notificationsdrop">
<? if(count(fetchAll($notifications)) > 0){
foreach(fetchAll($notifications) as $i){
?>
<a class="dropdown-item" href="view.php?id=<?php echo $i['id'] ?>" style="<? if($i['status']==['unread']){
echo"font-weight:bold;";
}?>">
<?
if ($i['type']==['socialalert']){
echo'Someone Followed You';
}
?>
</a>
<?}}?>
</div>
</li>
update : added function.php:-
function fetchAll($query){
$con = new PDO(DBINFO, DBUSER, DBPASS);
$stmt = $con->query($query);
return $stmt->fetchAll();
}
function performQuery($query){
$con = new PDO(DBINFO, DBUSER, DBPASS);
$stmt = $con->prepare($query);
if($stmt->execute()){
return true;
}else{
return false;
}
}
?>
Fixed: I forget that the equal value for if statement must be like this.
if ($i['type']=='socialalert'){
echo'Someone Followed You';
}
Related
I have this code that supposed to generate a multi-level menu from my database. My problem I have a specific HTML code when there are/are no child menu.
Here is the HTML Code if the menu does not have a child menu:
<li class="nav-item dropdown">Menu</li>
Here is the HTML code if the menu has a child menu:
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle arrow-none" href="javascript: void(0);" id="topnav-user-access" role="button">
<span key="t-user-access">Parent Menu</span> <div class="arrow-down"></div>
</a>
<div class="dropdown-menu" aria-labelledby="topnav-user-access">
Child Menu 1
Child Menu 2
...
</div>
</li>
Here is my PHP code to generate the menu.
public function generate_multilevel_menu($module_id, $username, $parent_menu = null){
$menu = '';
if(!empty($parent_menu)){
$query = 'SELECT MENU_ID, MENU, PARENT_MENU, IS_LINK, MENU_LINK FROM technical_menu WHERE MODULE_ID = :module_id AND PARENT_MENU = :parent_menu ORDER BY ORDER_SEQUENCE, MENU';
}
else{
$query = 'SELECT MENU_ID, MENU, PARENT_MENU, IS_LINK, MENU_LINK FROM technical_menu WHERE MODULE_ID = :module_id AND (PARENT_MENU IS NULL OR PARENT_MENU = "0") ORDER BY ORDER_SEQUENCE, MENU';
}
$sql = $this->db_connection->prepare($query);
$sql->bindValue(':module_id', $module_id);
if(!empty($parent_menu)){
$sql->bindValue(':parent_menu', $parent_menu);
}
if($sql->execute()){
while($row = $sql->fetch()){
$menu_id = $row['MENU_ID'];
$menu_access_right = $this->check_role_access_rights($username, $menu_id, 'menu');
if($menu_access_right > 0){
if(!empty($row['MENU_LINK'])){
$menu .= ''. $row['MENU'] .'';
#$menu .= '<li class="nav-item dropdown">'. $row['MENU'] .'</li>';
}
else{
$menu .= '<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle arrow-none" href="javascript: void(0);" id="topnav-user-access" role="button">
<span key="t-user-access">'. $row['MENU'] .'</span> <div class="arrow-down"></div>
</a>';
}
$menu .= '<div class="dropdown-menu" aria-labelledby="topnav-user-access">' . $this->generate_multilevel_menu($module_id, $username, $row['MENU_ID']) . '</div>';
$menu .= '</li>';
}
}
return $menu;
}
}
Here is the screenshot of my database structure and details.
Here is the sample output of my code:
My problem is that I can't seem to incorporate the HTML code to fit my code. The code works if the HTML code is a simple HTML list but using my HTML code and conditions I can't seem to fix it. What part of my code do I need to modify to accommodate my if conditions?
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";
}
}
}
?>
I have a a navigation bar and one of the nav items is a dropdown with sub categories. Only the subcategories are being pulled from the database by using a while loop.
When clicking on one of the dropdown items they will be redirected to dancerProfile.php.
I want dancerProfile.php to pull the menu item name from the other pages.
html
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
//if($res==FALSE){
//die('there was an error running query [' . $con->error . ']');
// }
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
foreach ($dancerName as $DANCER){
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
}
?>
<li>Add New</li>
</ul>
</li>
This works well as all dancers appear in the dropdown.
What I want is that when I click on dancerA the dancerProfile.php will show dancerA information. And when I click on dancerB it will show dancerB info, etc.
But it will only show the last dancer's information no matter which name I click on.
dancerProfile.php
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $dancerName;?></div>
</div>
So When I click on dancerA on the nav bar in any other page, in dancerProfile.php $dancerName should print dancerA. And it should print dancerB if I clicked on dancerB from the nav bar.
but it is only printing dancerB no matter which link I click.
I am using bootstrap. Can anyone help me?
EDIT
Here's an update of what my code looks like now.
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
?>
<li>Add New</li>
</ul>
</li>
And dancerProfile.php:
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = " . $_GET['id'] . ";";
$dancer_res = mysqli_query($con,$dancers);
if($dancer_res){
$DANCER='dancer_name';
}
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo " $DANCER ";?></div>
</div>
I also forgot to mention that this navigation is also on dancerProfile page. So all of the code I am providing is on dancerProfile page. I don't know if that matters
My database table
You need pass dancer id or which is unique in your dancer table. something like given below.
<li>'.$data["dancer_name"].'</li>
And now go to dancerProfile.php and try something like this.
if (isset($_GET['dancer_id'])) {
$dancer_id=$_GET['dancer_id'];
//your query
}
Your full code:
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){ ?>
$dancerName = $data['dancer_name'];
<li><?php echo $data['dancer_name']; ?>'</li>
<li class="divider"><hr></li>
<?php } ?>
<li>Add New</li>
</ul>
</li>
Your dancerProfile.php should be
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = '$dancerId'";
$dancer_res = mysqli_query($con,$dancerquery);
$data=mysqli_fetch_array($dancer_res);
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $data['dancer_name'];?></div>
</div>
You are currently overriding the value of $DANCER with each iteration, so the last will always be the used value.
Just change your loop a bit:
while($data=mysqli_fetch_array($dres)){
echo '<li>'.$data['dancer_name'].'</li>';
}
I am retrieving data from mysql and need to display the result in a specific class. I am using jquery to update every 10 seconds and this is working ok. Where I am getting stuck is getting that data into a specific class: actions.
I would be grateful if someone could point me in the right direction? php or jquery will be acceptable. Many thanks
$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'");
$rows = mysqli_fetch_assoc($sql);
$num = $rows['total'];
$ni = $num;
if($ni < 1) {
$ni = '0';
} else {
echo $ni; <--- NEED TO LOAD RESULT IN ACTIONS CLASS
}
Example html from header.php
<li>
Boxes <span class="drop-icon">▸</span> <label class="drop-icon" for="sm4" title="Toggle Drop-down">▾</label>
<input id="sm4" type="checkbox">
<ul class="sub-menu">
<li>
New Intake <span style="float: right;" class="notification ni"><?php echo $ni_num; ?></span>
</li>
<li>
Retrievals <span style="float: right;" class="notification retrievals"><?php echo $brtv_num; ?></span>
</li>
<li>
Returns <span style="float: right;" class="notification returns"><?php echo $brtn_num; ?></span>
</li>
<li>
Destructions <span style="float: right;" class="notification destructions"><?php echo $bdstr_num; ?></span>
</li>
<li>
Permanent Retrieval <span style="float: right;" class="notification pretrieval"><?php echo $prtv_num; ?></span>
</li>
</ul>
</li>
Example from loadActions.php
$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'");
$rows = mysqli_fetch_assoc($sql);
$num = $rows['total'];
$ni = $num;
if($ni < 1) {
$ni = '0';
} echo $ni;
$nisql= mysqli_query($conn,"SELECT count(*) as intake FROM act WHERE activity='New Intake' AND new = '1'"); // provide db connection object as first parameter
$ni_row = mysqli_fetch_assoc($nisql);
$ninum = $ni_row['intake'];
//echo $num;
$ni_num = $ninum;
if($ni_num < 1) {
$ni_num = '0';
} echo $ni_num;
$brtvsql= mysqli_query($conn,"SELECT count(*) as brtv FROM act WHERE activity='Box Retrieval' AND new = '1'"); // provide db connection object as first parameter
$brtv_row = mysqli_fetch_assoc($brtvsql);
$brtvnum = $brtv_row['brtv'];
//echo $num;
$brtv_num = $brtvnum;
if($brtv_num < 1) {
$brtv_num = '0';
} echo $brtv_num;
$brtnsql= mysqli_query($conn,"SELECT count(*) as brtn FROM act WHERE activity='Box Return' AND new = '1'"); // provide db connection object as first parameter
$brtn_row = mysqli_fetch_assoc($brtnsql);
$brtnnum = $brtn_row['brtn'];
//echo $num;
$brtn_num = $brtnnum;
if($brtn_num < 1) {
$brtn_num = '0';
} echo $brtn_num;
Please note, I am NOT a PHP programmer so you will have to do some work yourself
Take below as pseudo code and ask another PHP question if the PHP does not make sense.
Have ONE request
select activity,
count(*) total,
sum(activity = 'New Intake') intakeCount,
sum(activity = 'Box Retrieval') boxCount,
...
from act WHERE new = '1'
if ($rec["total"] == 0) {
echo '{ "total" : 0 }';
die 0;
}
$res = array(
"total" => $rec["total"],
"ni" => $rec["intakeCount"],
"retrievals" => $rec["boxCount"],
...
);
echo json_encode($res);
Result should be
{ "total" : 24,
"ni" : 14,
"retrievals" : 9,
....
}
Then you can do
function getBoxes() {
$.get('/domain/admin/loadActions.php', function(data) {
processData(data);
setTimeout(getBoxes, 15000);
});
}
function processData(data) {
for (key in data) {
console.log(key,data[key])
$("." + key).text(data[key]);
}
}
$(function() { // page load
// testing - remove this when running the getBoxes():
processData({
"total": 24,
"ni": 14,
"retrievals": 9
});
// getBoxes(); // remove comment when tested
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>
Boxes <span class="drop-icon">▸</span> <label class="drop-icon" for="sm4" title="Toggle Drop-down">▾</label>
<input id="sm4" type="checkbox">
<ul class="sub-menu">
<li>
New Intake <span style="float: right;" class="notification ni"></span>
</li>
<li>
Retrievals <span style="float: right;" class="notification retrievals"></span>
</li>
<li>
Returns <span style="float: right;" class="notification returns"></span>
</li>
<li>
Destructions <span style="float: right;" class="notification destructions"></span>
</li>
<li>
Permanent Retrieval <span style="float: right;" class="notification pretrieval"></span>
</li>
</ul>
</li>
<div class="total"></div>
I've been trying to build a dropdown menu but I'm not getting my desired results. Here's my code:
<?php require_once 'core/init.php'?>
<?php
$sql = 'SELECT * FROM categories WHERE parent = 0';
$pquery = mysqli_query($db,$sql);
?>
<?php while($parent = mysqli_fetch_assoc($pquery)):?>
<?php
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = "parent_id"';
$cquery = mysqli_query($db,$sql2);
?>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<?php echo $parent['id'];?><span class='caret'</span</a>
<ul class='dropdown-menu' role='menu'>
<?php while($child = mysqli_fetch_assoc($cquery)):>
<li><a href='#'><?php echo $child['parent'];?></a>
</li>
<?php endwhile; ?>
</ul>
</li>
<?php endwhile;?>
My DB is like this and the result is this.
Try with console phpmyadmin before write codes in page.
I think your codes is wrong.
Your code must like this
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = '.$parent_id;
I suggest to you for use ajax to be beautiful again.