php within php using bootstrap - php

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>
';

Related

Bootstrap prevent multiple collapsable elements from being open at the same time

I have the following html sidebar menu:
<li {{{ (Request::is('bookings/*') }}} data-toggle="collapse" data-target="#bookings">
<i class="fa fa-address-book" aria-hidden="true"></i> Bookings <i class="fa fa-chevron-down" aria-hidden="true"></i>
</li>
<ul class="sub-menu collapse" id="bookings">
<li class="collapsed">All Bookings</li>
<li class="collapsed" >Add New</li>
</ul>
<li {{{ (Request::is('bookings/*') || Request::is('bookings') ? 'class=active' : '') }}} data-toggle="collapse" data-target="#item2">
<i class="fa fa-address-book" aria-hidden="true"></i> Item 2 <i class="fa fa-chevron-down" aria-hidden="true"></i>
</li>
<ul class="sub-menu collapse" id="item2">
<li {{{ (Request::is('bookings') ? 'class=active' : 'collapsed') }}}>All Bookings</li>
<li {{{ (Request::is('bookings/create') ? 'class=active' : 'collapsed') }}} >Add New</li>
</ul>
This is a basic Bootstrap collapse menu that contains a sub menu which expands when the li element is clicked.
The problem I have is lets say I have 2 or 3 of these menu items that all have sub menus. There is a possibility that all of them could open at the same time, I don't like this because this forces a scroll overflow as the height increases which then shows a scrollbar for the side menu.
Is there a way I can prevent multiple elements from being expanded in Bootstrap?
I am using Laravel 5 if that helps.
I think this should help you, just play a little with it to adapt it to your needs:
<div class="row">
<div class="col">
<ul class="nav nav-stacked" id="accordion1">
<li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#firstLink">Test12</a>
<ul id="firstLink" class="collapse">
<li>SubTest1</li>
<li>SubTest1</li>
<li>SubTest1</li>
</ul>
</li>
<li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#secondLink">Test2</a>
<ul id="secondLink" class="collapse">
<li>SubTest2</li>
<li>SubTest2</li>
<li>SubTest2</li>
<li>SubTest2</li>
</ul>
</li>
</ul>
</div>
</div>
See it here
https://codepen.io/anon/pen/ZaMOxN

Return multiple vars inside loop 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>
';
}

How do I keep the current tab active?

I use bootstrap theme bs-admin-bcore and I want to make menu in file "menu.php" to be more dynamic.
For example, I have this code :
<ul id="menu" class="collapse">
<li class="panel">
<a href="index.php" >
<i class="icon-table"></i> Dashboard
</a>
</li>
<li class="panel ">
<a href="#" data-parent="#menu" data-toggle="collapse" class="accordion-toggle" data-target="#component-nav">
<i class="icon-tasks"> </i> UI Elements<span class="pull-right"><i class="icon-angle-left"></i></span> <span class="label label-default">10</span>
</a>
<ul class="collapse" id="component-nav">
<li class=""><i class="icon-angle-right"></i> Buttons </li>
</ul>
</li>
</ul>
when I call index.php <li class="panel"> becomes <li class="panel active"> and when I call button.php EU Elements becomes active.
You have to add "active" class to the parent li of buttons as well n it will work.

display php variables in bootstrap Navbar

i want to display Usernames after login or signup in the Navbar using the function links () but the variable holding the username wont show, only displays "$acn";
function links()
{
if(!isset($_SESSION['username']))
{
echo ' <div class="collapse navbar-collapse" id="signup">
<!--<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>-->
<ul class="nav navbar-nav navbar-right" id="signup">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>';
}
else
{
$acn= $_SESSION['username'];
echo '<div class="collapse navbar-collapse" id="signup">
<ul class="nav navbar-nav navbar-right" id="signup">
<li> $acn </li>
<li><span class="glyphicon glyphicon-log-out"></span> Log Out</li>
</ul>
</div>';
}
}
and the function appears like this
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">OOOO</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#signup">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<?php echo links(); ?>
</div>
</nav>
How do i achieve this please, thanks.
In PHP, if you have a string wrapped in single quotes variables will not be parsed.
'$acn' is just a string with the value $acn. "$acn" will be parsed to be the value of the $acn variable.
Please also note that you are echoing the output of the links() function, but that function has no return value.
While your use of $acn would work fine in double quotesit does not behave in the same manor when contained inside single quotes.
If you wish to use single quotes you must concatenate the string:
<li> '.$acn.' </li>
Other things to note with your code, while using single quotes you will not need to escape double quotes and also there is an echo inside the links function so its not required here: <?php echo links(); ?>

Inserting a navbar into wordpress

Straigth to the point: I got the twentytwelve Theme from WordPress installed and changed its background + removed the default navbar. (commented it out)
Now how can I implement my bootstrap navbar that float on the top of the screen.
This is the navbar code:
<?php
if(!isset($nosession))
session_start();
}
/*if(isset($_COOKIE['username']))
{
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['loggedin'] = "true";
}*/
?>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">IHES</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li>Home</li>
<li>Adopteren</li>
<li>WebWinkel</li>
<li>Wall of Steen</li>
<li class="dropdown">
<i class="icon-film" style="margin-top:-1px"></i> Videos <b class="caret"></b>
<ul class="dropdown-menu">
<li><i class="icon-film"></i>Nieuws</li>
<li><i class="icon-film"></i>Interviews</li>
<li><i class="icon-film"></i>Overig</li>
</ul>
</li>
<li>Steenoloog Blog</li>
</ul>
<ul class="nav pull-right">
<?php
if(isset($_SESSION['loggedin'])){
if(isset($_COOKIE['cart']))
{
echo '
<li class="pull-right"><i class="icon-shopping-cart icon-white"></i></li>
';
}
echo '<li class="navbar-form pull-right">
'.$_SESSION['username'].'
</li>';
echo '<li class="navbar-form pull-right">
Loguit
</li>';
}else{
echo '<div class="navbar-form pull-right">
<button type="submit" class="btn">Login Pagina</button>
</div>';
}
?>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
That is my navbar code that I include on top of every .php page.
require "navbar.php";
But how can I use this with in the 2012 theme from wordpress?
I tried to just use the require and also link to the .css files from bootstrap.
But this only gives me the content of the navbar. It is missing the navbar itself.
(BTW: I pasted the code in the header.php file from the theme)
can you include css?
Also try include instead of require

Categories