So i have this in html
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
</ul>
And i want to add this php code in the list:
<?php
...
elseif($_SESSION['logged']==false)
echo 'Login</li>';
echo 'Register</li>';
?>
But instead of displaying 2 separate list items, one with Login and one with Register i only get one with those two links.
What can I do?
You missed starting li tags and brackets.
do this:
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
If $_SESSION['logged'] is going to be not set or false then use empty(), also your missed you opening li tags:
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
<?php if(empty($_SESSION['logged'])): ?>
<li>Login</li>
<li>Register</li>
<?php endif; ?>
</ul>
You have forgotteh curly bracket { and li
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
Related
I'm trying to make an active navigation bar using PHP where the current page will be highlighted or colored in the navigation bar. The code I used:
<ul class="topmenu">
<li <?php if($_GET['page']="home") { ?> class="active" <?php } ?>>
<b>Bienvenue</b>
</li>
<li <?php if($_GET['page']="livres") { ?> class="active" <?php } ?>>
<b>Livres</b>
</li>
<li <?php if($_GET['page']="bibliotheque") { ?> class="active" <?php } ?>>
<b>Bibliothèque</b>
</li>
<li <?php if($_GET['page']="universite") { ?> class="active" <?php } ?>>
<b>L'université</b>
</li>
<li <?php if($_GET['page']="contact") { ?> class="active" <?php } ?>>
<b>Contact</b>
</li>
</ul>
The code will put the attribut in the class active if the page is the current page in the navigation bar. However, all the attributs will be given the class active in this case. How do I fix this?
P.S: I am not looking for any JS or jQuery alternatives, I'm trying to make this work with PHP only.
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><b>Bienvenue</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><b>Livres</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><b>Bibliothèque</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><b>L'université</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><b>Contact</b></li>
</ul>
I don't use PHP, but give the current page the active tag, and change it per file. Then a single class definition in the CSS handles changing the color for each page.
HTML:
<nav>
<a class="active" href="/home/">Home</a>
Calendar
Community
</nav>
CSS:
.active {
background-color: #4CAF50;
}
Declare page variables at the very top of each individual page.
Example: <? $page="home";?>
For each list item in your nav bar add if statment with corresponding variable and active class.
Example: <li class="<?if($page=="home"){?>active<?}?>"><b>Bienvenue</b></li>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I'm using bootstrap 4 class. I've achieved Active Class selection something like this.
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile1.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile1.php">Home
</a>
</li>
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile2.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile2.php">Home
</a>
</li>
Repeat this logic for further li tags. Hope this helps someone else.
The code work correctly if you use "==" instead of "=" in The if construct
Еxample:
if($_GET['page'] **==** "home")...
Instead:
if($_GET['page'] **=** "home")...
Hope this helps someone else...
I tried to show a login/logout link in my header based on the value in session. i tried some thing like this
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{?>
<li>Logout</li>
<?php}
else
{?>
<li>Login</li>
<?php } ?>
</ul>
then both links didn't appeare in the header(login/logout). then after a lot of trying i came up with this code
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{
?>
<li>Logout</li>
<?php
}
else
{
?>
<li>Login</li>
<?php
}
?>
</ul>
the code is actually same but i have added some spaces between the curly brackets'{'. And it works as i intended. Is space an issue when we use html and yii2 code combined?
This has nothing to do with Yii, it is simply a php syntax problem (you should always have a space after <?php)...
If you want to mix condition and html output, and have a better readability, you should use this :
<?php if ($user_id != null) : ?>
Output 1
<?php else : ?>
Output 2
<?php endif; ?>
Read more : http://php.net/manual/en/control-structures.alternative-syntax.php
There HAS to be a space after the opening <?php tag, which means <?php} is not valid:
without space:
$ cat z.php
<?php if(true) {?>
true
<?php} else {?> // note, no space after <?php
false
<?php }?>
$ php z.php
true
<?php} else {?>
false
with space:
$ cat y.php
<?php if(true) {?>
true
<?php } else {?>
false
<?php }?>
$ php y.php
true
Note the difference in output. This has nothign to do with Yii, and everything to do with your core PHP coding.
I'm trying to iterate through my array and group li's with their relative ul, based on a common id.
expected result is as follows
<ul>
<li>b</li>
<li>c</li>
<li>a</li>
<li>g</li>
<li>e</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
I have tried following code
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<ul>
<li><?php $name ;?></li>
</ul>
<?php
}
?>
but above code gives following result
<ul>
<li>c</li>
</ul>
<ul>
<li>a</li>
</ul>
<ul>
<li>d</li>
</ul>
<ul>
<li>i</li>
</ul>
You'll want to use two loops.
One outer for generating the <ul> elements, another for generating the <li> elements within the <ul>
Such code might look like this
<?php
$result = []; //grouped by ul ID
for ($i = 1; $i < count($ids); $i++) {
echo '<ul>';
while ($row = $result[$i])
echo '<li>' . $result[$i]['name'] . '</li>';
echo '</ul>';
}
?>
It's valid PHP but it doesn't work ofcourse.
This is the structure you're looking for tho since you'll want an outer loop to go through the amount of <ul> elements you need and within that loop you'll want to loop through the list-items themselves.
The outer loop wouldn't be needed if you only had one <ul> ofcourse.
Your code is wrong. Put ul tag out of the body of while loop. Try this:
<ul>
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<li><?php $name ;?></li>
<?php
}
?>
</ul>
My coding is like
<?php
if (!isset($_SESSION["usr"]))
{
?>
<ul id="top_nav">
<li >Login</li>
<li>About us</li>
</ul>
<?php
}
else
{
echo '<ul id="top_nav">';
echo '<li id="logout">Logout</li>';
echo '<li>' . $_SESSION["usr"] . '</li> </ul>';
}
?>
In php I want to destroy session when user click on logout(which I have used li).
so I want to check using isset function.
or any other method then let me know.
to unset session var use unset($_SESSION['usr'],$usr);. Both vars are needed to workaround with register_global issue. Your code have to be kind of this
<?php
if ($_GET['logout']=='1') unset($_SESSION['usr'],$usr);
if(!isset($_SESSION["usr"])){?>
<ul id="top_nav">
<li>Login</li>
<li>About us</li>
</ul>
<?php }else{ ?>
<ul id="top_nav">
<li id="logout">Logout</li>
<li><?=$_SESSION["usr"];?></li>
</ul>
<?php } ?>
I want to add some markup in the while loop, so that each three items are wrapped in a <ul> and each of the ul should be wrapped in a div. There can be maximum 6 items, and I want to get following output:
<div class="one">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
<div class="two">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
I am trying following code:
<div class="one">
<ul>
<?php
$i = 0 ;
while (have_posts()) : the_post();
$i++; ?>
<li>...</li>
<?php
if ($i === 3){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
?>
}
if ($right_div){
</ul></div>
<?php } ?>
?>
It works fine if there are at least 3 items, but if there are less than 3, then breaks the code as it does not close the ul and div.
It is important to use the while loop because its part of a WordPress theme which uses the while loop to get the posts.
Im not sure I get it, but I think the problem is:
if ($right_div){
</ul></div>
<?php } ?>
That should not be a condition, that should be always printed.
If I'm understanding correctly, you're saying you want to
group things in group of 3's, but if you have 11 items or something
like that it breaks. If that's the case, then on the last iteration
of the while loop you should close the ul and div elements.
Like so:
if(condition){
echo "</ul></div>";
}
Taking reference from http://codex.wordpress.org/Function_Reference/have_posts
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
if (!more_posts()){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
Oh and i am not a wordpress guy :P