Return multiple vars inside loop PHP - php

I want to return multiple notifications within a loop. I have 2 functions, the first one which pulls the notifications from the database and the second one for the dashboard header. The dashboard header is where the notifications will be displayed.
Now, the issue is, for some reason only one notification will be displayed. I've tried changing the return inside the loop to echo but that outputs the notifications at the beginning of dashboardTop().
What's going wrong and how can I resolve this?
public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
return '<li>' . $notification['content'] . '</li>';
}
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}

public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
$out=''; //empty var tof concaternation
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
$out.= '<li>' . $notification['content'] . '</li>'; //concaternate each line in to string
}
return $out; //return whole string
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}

Related

repeat div and UL 3 times but li will repeat 7 times

I have a HTML structure with bootstrap 3 columns, which all div repeat 3 times only, but all div have one ul and each ul has 7 li.
I want to show the dynamic list according to the below HTML structure:
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Mechanical Engineering Jobs</li>
<li><i class="fa fa-angle-right"></i> BPO Jobs</li>
<li><i class="fa fa-angle-right"></i> Networking Jobs</li>
<li><i class="fa fa-angle-right"></i> Java Jobs</li>
<li><i class="fa fa-angle-right"></i> Online Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Animation Jobs</li>
<li><i class="fa fa-angle-right"></i> Design Engineer Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Analytics Jobs</li>
<li><i class="fa fa-angle-right"></i> UI/UX Jobs</li>
<li><i class="fa fa-angle-right"></i> NLP Jobs</li>
<li><i class="fa fa-angle-right"></i> Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Jobs</li>
<li><i class="fa fa-angle-right"></i> MBA Jobs</li>
<li><i class="fa fa-angle-right"></i> Teaching Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Accounting Jobs</li>
<li><i class="fa fa-angle-right"></i> Retail Jobs</li>
<li><i class="fa fa-angle-right"></i> Travel Jobs</li>
<li><i class="fa fa-angle-right"></i> Merchandiser Jobs</li>
<li><i class="fa fa-angle-right"></i> Architecture Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Insurance Jobs</li>
<li><i class="fa fa-angle-right"></i> Music Jobs</li>
</ul>
</div>
For this I have seen the below answers and tried with the below code.
I have tried with the below code but it's not working as is should.
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
while($resEng = mysql_fetch_array($sqlEng)){
if ($count%3 == 1)
{
?>
<div class="col-md-4 padding-left">
<?php } ?>
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
</ul>
<?php if ($count%3 == 0)
{
?>
</div>
<?php } $count++; } ?>
Assuming you have only 21 records in the table.
try this.
<?php
//$sqlEng = mysql_query('select * from jobs_category');
$servername = "localhost:3306";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "stack1");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sqlEng = $conn->query('select * from jobs_category');
$count = 1;
while($resEng = $sqlEng->fetch_assoc()){
if ($count%7 == 1)
{
?>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<?php } ?>
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
<?php if ($count%7 == 0)
{
?>
</ul>
</div>
<?php } $count++;
} ?>
tested in my local.
If I get your question correctly, you are trying to spread the list items <li> A Job </li> across three lists <div> <ul> List Items </ul> </div>
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
$group_one = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_two = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_three = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
while($resEng = mysql_fetch_array($sqlEng)){
if($count > 3) {
$count = 1;
}
switch ($count) {
case 1:
$group_one .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 2:
$group_two .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 3:
$group_three .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
}
$count++;
}
$group_one .= "
</ul>
</div>";
$group_two .= "
</ul>
</div>";
$group_three .= "
</ul>
</div>";
echo $group_one . $group_two . $group_three;
?>
You want to consider using MySQl improved mysqli https://www.w3schools.com/php/php_ref_mysqli.asp

How to show menu according to session in php?

I have 2 tables admin and superadmin. I have same login page for both
users. i can login according to query.i had create sessions for both
users. each table have column name roleID. for superadmin roleID is
1, and for admin roleID is 2. Below is my code for login where i
create session. i used print_r and my session are working. Below is
my code for login page.
if (isset($_REQUEST['submit']))
{
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '".$username."' AND `acc_pass` = '".$password."'");
$data = mysqli_fetch_array($sql);
$_SESSION['role0']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$sql1 = mysqli_query($conn,"SELECT * FROM `superadmin` where `username` = '".$username."' AND `password` = '".$password."'");
$data1 = mysqli_fetch_array($sql1);
$_SESSION['role1']=$data1['roleId'];
if ($data>0)
{
header('Location: societyList.php');
}
elseif ($data1>0) {
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
Now on home.php i have some menus to show accoording to roleID my
code for menu.
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Create Society</li>
</ul>
</li>
<li><a><i class="fa fa-home"></i> Master <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Units</li>
<li>Members</li>
<li>Parking Lots</li>
<li>Charges</li>
<li>Chart of Account</li>
<li>Interest Penalties</li>
<li>Billing Templates</li>
<li>Tax Structure</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Transactions <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Bill</li>
<li>Collection</li>
<li>Expenses</li>
<li>Journal</li>
<li>Bank Reco</li>
<li>Drop Box</li>
<li>Online Payment</li>
</ul>
</li>
<li><a><i class="fa fa-desktop"></i> Reports <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Income & Expenses</li>
<li>Balance Sheet</li>
<li>Cash Flow</li>
<li>Interest Calculation</li>
</ul>
</li>
<li><i class="fa fa-table"></i> Notices <span class="fa fa-chevron-down"></span>
</li>
<li><a><i class="fa fa-bar-chart-o"></i> Registers <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Form-I</li>
</ul>
</li>
<li><a><i class="fa fa-clone"></i> Forum <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Cultural Activity</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Domestic Help <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request for Plumber</li>
<li>Request for Maid</li>
<li>Request for House Cleaner</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Emergency <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ambulance</li>
<li>Fire Brigade</li>
<li>Police</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Helpdesk <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request NOC for Tenancy</li>
<li>Request NOC for Home Loan</li>
<li>Request NOC for Mortgage as collateral</li>
<li>Request for Vehicle Parking</li>
<li>Application for Transfer</li>
<li>Request for waiver of interest</li>
<li>Request for waiver of a charge</li>
<li>Suggestion</li>
<li><a><i class="fa fa-edit"></i><span class="fa fa-chevron-down"></span>Complaint</a>
<ul class="nav child_menu">
<li>About leakage</li>
<li>About tenants issues</li>
<li>About parking nuisance
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> CFO Desk Assists <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Tenant Rating</li>
<li>Owners Rating</li>
<li>Your Reviews</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Masters <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Auto Bank reconcilliation</li>
<li>AMC Masters</li>
<li>Auto Adjust pending reference</li>
<li>Default GL for defined transactions</li>
</ul>
</li>
</ul>
</div>
</div>
As you can see lists. i want if $_SESSION['role0']=$data['roleId']; in session so first 4 lists will be visible for him only.
If $_SESSION['role1']=$data['roleId']; in session then rest of all lists will be display to him.
How to done it please help me with the same. I used if condition like
if{$_REQUEST($_SESSION['role0'])
echo 'some lists';
}
if i used like this if condition nothing will display.
The session data is note stored in the $_REQUEST.
$_SESSION['role0']=$data1['roleId'];
$_SESSION['role1']=$data1['roleId'];
Both 'role0' and 'role1' will have the same value. It can be simplified to:
$_SESSION['role']=$data1['roleId'];
Then use:
if( $_SESSION['role'] === 1) {
echo 'admin role 1';
echo 'Show first half of menu';
} elseif ($_SESSION['role'] === 2) {
echo 'Show second half of menu';
} else {
echo 'Other or missing admin value. show no menu';
}
SQL injection is possible the way it is currently written. Prepared statements would be a good thing to add next.

php within php using bootstrap

Basically what I'm requesting to output isn't showing, I've got a navbar written in php, so that if a user is logged in, it shows different options, than if they weren't logged in. However upon adding this to my website, it's now interfering with other php.
Here's the code I'm using to output the php,
$envelope = '
<ul class="nav navbar-nav pull-right">
<li class="dropdown pull-right" id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu2">
<i style="font-size: 20px; margin-right:5px;" class="glyphicon glyphicon-user Default"></i>
</a>
<div class="dropdown-menu" id="menu">
<div id="friendReqBox"><p style="font-size:20px; text-decoration:bold;">Friend Requests</p><hr/> <?php echo $friend_requests; ?> </div>
</div>
</li>
</ul>
';
Notice that within " $envelope = ' '; tag there's echo $friend_requests; wrapped within php tags, now I tried removing the php tags, but then it just outputs plaintext.
There is more php code to this, it's not just $envelope & there is php tags surrounding it, before you state I need them >.<, only reason I posted this was because the problem is within $envelope = ' ', now I can see what the problem is, I just don't know how to solve it.
You're creating a string, you don't ever need to run PHP inside that string. It will be evaluated when you return it to the browser (example: printing or echoing it)
$envelope = '
<ul class="nav navbar-nav pull-right">
<li class="dropdown pull-right" id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu2">
<i style="font-size: 20px; margin-right:5px;" class="glyphicon glyphicon-user Default"></i>
</a>
<div class="dropdown-menu" id="menu">
<div id="friendReqBox"><p style="font-size:20px; text-decoration:bold;">Friend Requests</p><hr/> ' .$friend_requests . ' </div>
</div>
</li>
</ul>
';
So what you're doing is essentially concatenating the string and adding the $friend_requests to the $envelope variable. Without the HTML, this is simple what it is:
$var1 = "Something";
$var2 = 'I like to do ' . $var1 . 'occasionally';
// which prints: I like to do Something occasionally
Your code should like this...
$envelope = '
<ul class="nav navbar-nav pull-right">
<li class="dropdown pull-right" id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu2">
<i style="font-size: 20px; margin-right:5px;" class="glyphicon glyphicon-user Default"></i>
</a>
<div class="dropdown-menu" id="menu">
<div id="friendReqBox"><p style="font-size:20px; text-decoration:bold;">Friend Requests</p><hr/>' . $friend_requests . '</div>
</div>
</li>
</ul>
';

How to create multi level menu using Yii

How do I do a query to the case of multi-level menus in yii?
For example I have a table like this :
menu_name will follow parrent_id it has, so it will form a hierarchical menu
How do I use Yii DAO on my layout so that the output as html above
My Code :
<?php
Yii::import('zii.widgets.CMenu', true);
class ActiveMenu extends CMenu
{
public function init(){
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu';
$criteria->params=array(':idpub'=>1, ':menu'=>'#');
$items = Menu::model()->findAll($criteria);
echo "<ul id='yw1' class='nav'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
}
echo "</ul>";
parent::init();
}
}
But in the above code I only get parents data. How do I want to loop to get the childs data include in every parents menu like this html code :
<ul id="yw1" class="nav">
<li><span class="icon-white icon-tasks"></span> Home</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Master<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Expedition</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Partner</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> User</li>
</ul>
</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Transaction<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process In</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process Out</li>
</ul>
etc ....
etc ....
etc ....
</li>
</ul>
You should first have the relations set up properly in your Menu model (Menu.php).
public function relations()
{
return array(
'parent' => array(self::BELONGS_TO, 'Menu', 'parent_id'),
'children' => array(self::HAS_MANY, 'Menu', 'parent_id'),
);
}
And then modify the codes:
//other codes
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
foreach ($item->children as $child) {
echo "<ul class='dropdown-menu'>
<li><a href='".$child->menu_controller."'><span class='icon-bar icon-list'></span> ".$child->menu_name."</a></li></u>";
}
}
//other codes

Create multi level menu using Yii

How do I do a query to the case of multi-level menus in yii?
For example I have a table like this :
menu_name will follow parrent_id it has, so it will form a hierarchical menu
How do I use Yii on my layout so that the output as html above
My code :
<?php
Yii::import('zii.widgets.CMenu', true);
class ActiveMenu extends CMenu
{
public function init(){
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu';
$criteria->params=array(':idpub'=>1, ':menu'=>'#');
$items = Menu::model()->findAll($criteria);
echo "<ul id='yw1' class='nav'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
}
echo "</ul>";
parent::init();
}
}
But in the above code I only get parents data. How do I want to loop to get the childs data include in every parents menu like this html code :
<ul id="yw1" class="nav">
<li><span class="icon-white icon-tasks"></span> Home</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Master<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Expedition</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Partner</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> User</li>
</ul>
</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Transaction<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process In</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process Out</li>
</ul>
etc ....
etc ....
etc ....
</li>
</ul>
Thanks
You can create a recursive function for loop your childs like this
<?php
class ActiveMenu extends CMenu
{
public function init(){
$this->renderChilds();
}
protected function getChilds($parent_id)
{
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu AND parent_id = :parent';
$criteria->params=array(':idpub'=>1, ':menu'=>'#', ':parent'=>$parent_id);
$items = Menu::model()->findAll($criteria);
return $items;
}
public function renderChilds($parent_id=0, $class='nav')
{
$items = $this->getChilds($parent_id);
if (!empty($items))
{
echo "<ul class='$class'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>";
$this->renderChilds($item->id, 'dropdown-menu');
echo "</li>";
}
echo "</ul>";
}
}
...
}

Categories