I want to print an attribute (i.e. active) on the first element of an array.
The ugly way is to use a variable:
<ul class="nav nav-pills">
<?php $firstItem = true; ?>
<?php while ($row = $res_sections->fetchArray()) { ?>
<li class="nav-item">
<a class="nav-link<?php if ($firstItem) echo ' active' ?>" data-toggle="pill" href="#nav<?= $row['section']?>"><?php echo "{$row["section"]}"?></a>
</li>
<?php $firstItem = false; ?>
<?php } ?>
</ul>
I wonder if there's something more elegant.
Is there a way to know if $row is the first element (or any) of the fetchArray() result?
EDIT
Here where fetchArray() comes from:
$db = new SQLite3("/db.sqlite");
$sql_sections = "SELECT DISTINCT section FROM settings";
$res_sections = $db->query(SQLite3::escapeString($sql_sections));
Related
I'd like to add condition to my code so that after click on label it link me to other page. Now this code link to other article on the website. So I guess what should I add to get from my label to other page.
Thank you for answers!
My code is below:
<ul class="nav nav-tabs aboutUsTabs nav-justified" role="tablist">
<?php foreach ($this->items as $key=>$item ){
if($item['children']){ ?>
<li role="presentation" class="<?php if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){ echo 'active'; } ?>"><?php echo $item['menu']->title;?></li>
<?php }else{ ?>
<li role="presentation" class="<?php if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){ echo 'active'; } ?>"><?php echo $item['menu']->title;?></li>
<?php } ?>
<?php } ?>
</ul>
All this start/stop of PHP code hurts my eyes! Anyways, linking to other pages is done with tags. Not quite sure I understand your question, but you could (if you mean labels for form elements) do something like this;
<label for=""> Something Meaningful </label>
I'm sorry, but I took the liberty to clean up your code a little. Hopefully this will make it more readable;
<ul class="nav nav-tabs aboutUsTabs nav-justified" role="tablist">
<?php
foreach ($this->items as $key=>$item ){
$active = NULL;
if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){
$active = 'active';
}
$menu = NULL;
if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){
$menu = 'active';
}
if($item['children']){
echo'<li role="presentation" class="'.$active.'">
'.$item['menu']->title.'
</li>';
}else{
echo'<li role="presentation" class="'.$menu.'">
'.$item['menu']->title.'
</li>';
}
}
?>
</ul>
For your comment question, if you know what item needs a different value, it could be the title f.ex. then you can check against that value and change the output of your element. Maybe something like;
if($item['children']){
if($item['title'] == 'Unique Identifier for your element') {
// In here you could manipulate the output of that one item you want to exclude/change
} else {
// Your normal output
echo'<li role="presentation" class="'.$active.'">
'.$item['menu']->title.'
</li>';
}else{ ... }
And even better would be to perform the check beforehand, so maybe something like;
$link = '#about-us-page-'.$item['menu']->id;
if($item['title'] == "That identifier") {
$link = 'somethingElse';
}
and then change the value of your href tag to;
'.$item['menu']->title.'
I am creating a menu dynamically based on the categories registered on the dataBase, What I need is:
When someone click in the link, this option should have the css class 'active', showing in with page the user is and even the pages are created dynamically.
I have no idea how to do this because I am php student and I couldn't find this information in google for "Dynamically menus" Thank you very much.
<nav class="menu">
<ul class="list">
<li class="line"><a id="item" href="index.php">Home</a></li>
<?php
$categories = $db->select("select * from tbl_category");
if($categories){
while ($result = $categories->fetch_assoc()){
echo <<<HTML
<li id="line" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
HTML;
}
}
?>
</ul>
<nav>
First of all, you're looping through that while loop and adding the id of "line" to each and every <li> element. I suggest you create unique id's for each list item. I don't necessary advocate using the code the way you have it, just as an example, here's your code edited to do just that:
if($categories){
$i = 1;
while ($result = $categories->fetch_assoc()){
$i++;
echo <<<HTML
<li id="line$i" ><a class="item" href="categories.php?category=$result[id]">$result[name]</a></li>
HTML;
}
}
Then when someone clicks on your link, just use jQuery with something like this:
$(".item").click(function() {
$(this).parent('li').addClass('active');
});
Try something like that:
<nav class="menu">
<ul class="list">
<li class="line"><a id="item" href="index.php">Home</a></li>
<?php
$current_page = isset($_GET['category']) ? $_GET['category']: -1;
$categories = $db->select("select * from tbl_category");
if($categories){
while ($result = $categories->fetch_assoc()){
echo '<li id="line">';
if($result['category'] === $current_page) {
// If we're currently in the active page then add the active class to the <a>
echo '<a class="item active" href="categories.php?category='. $result[id] .'">';
} else {
echo '<a class="item" href="categories.php?category='. $result[id] .'">';
}
echo $result['name'];
echo '</a>';
echo '</li>';
}
}
?>
</ul>
<nav>
Thank you all for help, based on your code, I solved doing this:
menu.php:
<nav class="menu" id="menu">
<ul id="list" class="list">
<li id="line" class="<?php if($presentPage == '0')echo 'active';?>"><a id="item" class="item" href="index.php">Home</a></li>
<?php
function checked($pp, $id){
if($pp == $id){
$status = 'active';
return $status;
}
}
$query = "select * from tbl_category";
$category = $db->select($query);
if($category){
while ($result = $category->fetch_assoc()){
echo "
<li id='line' class='".checked($presentPage, $result['id'])."'><a id='item' class='item' href='categories.php?category=".$result['id']."'>".$result['name']."</a></li>
";//end echo
}//end while
}//end if
?>
</ul>
</nav>
In my index.php:
<?php $presentPage = 0;?>
<?php require_once 'header.php';?>
<?php require_once 'menu.php';?>
and in my categories.php:
<?php
if (!isset($_GET['category']) || $_GET['category'] == NULL) {
$presentPage = 0;
}else{
$presentPage = intval($_GET['category']);//just to make sure that the variable is a number
}
?>
<?php require_once 'header.php';?>
<?php require_once 'menu.php';?>
///...my code...
and in my CSS of course:
.active{
background: linear-gradient(#821e82, #be5abe);
}
Thats is working perfectly for me, Thank for all help.
I'm currently working on a php script and stuck at this part. I have a database table which I want to display information from each row. I have two dropdown menus which I want to have the exact same names with just with a different url. I'm assuming this is possible, and I'm doing something wrong, as I'm not that advanced with php.
I found this question that was similar but didn't seem to see anything that helped much.
Use same foreach multiple times
Here is what I currently have
Top of page
<?php
require("common.php");
if(empty($_SESSION['user']))
{
header("Location: login.php");
die("Redirecting to login.php");
}
$header = "SELECT id, location FROM table";
try
{
$headerstmt = $db->prepare($header);
$headerstmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
?>
Then here is the section with the foreach loops
<ul class="nav navbar-nav">
<li class="dropdown <?php if ($page == 'view') {echo 'active';} ?>">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">View <b class="caret"></b></a>
<ul class="dropdown-menu">
<?php foreach ($headerstmt->fetchAll() as $hrow) { ?>
<li><?php echo $hrow['location']; ?></li>
<?php } ?>
</ul>
</li>
<li class="dropdown <?php if ($page == 'edit') {echo 'active';} ?>">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Edit <b class="caret"></b></a>
<ul class="dropdown-menu">
<?php foreach ($headerstmt->fetchAll() as $hrow) { ?>
<li><?php echo $hrow['location']; ?></li>
<?php } ?>
</ul>
</li>
</ul>
The first foreach loop works correctly, however the second one is blank.
Thanks in advance if anyone is able help.
You should store the result of $headerstmt->fetchAll() in a variable and then use the resulting variable for the foreach loops.
$results = $headerstmt->fetchAll();
foreach( $results as $row ) {
}
foreach( $results as $row ) {
}
foreach( $results as $row ) {
}
I'm new to coding with php and using MySQL. I am having trouble to display a list of categories by their ID so that each category is displayed individually as a heading. Instead I got it to display a category name but its only echoing out a category name twice that's the same. Here is my code...
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
}
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $cat_name;?></li>
<li class="nav-header"><?php echo $cat_name;?></li>
</ul>
Your li tag is outside the while loop. So the $id and $cat_name is only the last record in the DB, then you echo them twice. That's way you got the same name twice.
Try echo the li tag in the loop (but not the ul):
<ul class="nav nav-list">
<?php
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
echo '<li class="nav-header">' .$cat_name. '</li>';
}
?>
</ul>
You can use this code as-is :
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); ?>
<ul class="nav nav-list">
<?php while($row = mysql_fetch_array($query)) : ?>
<li class="nav-header"><?php echo $row['cat_name'];?></li>
<?php endwhile; ?>
</ul>
This will loop through your records and for each record, it will print an entire li with the required data.
Note that separating your PHP code from your HTML code like this has several benefits. It will be better colored in your editor and it is also easier to integrate.
The reason you are printing the same value out twice is because $cat_name is a string variable and will only hold one value at a time you may want to save the items an array and loop at a seperate time, like such
<?php
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
$category = array();
while($row = mysql_fetch_array($query))
{
$array = array(
id => $row['id'],
name => $row['cat_name']
);
array_push($category,$array);
}
?>
<ul class="nav nav-list">
<?php
foreach($category as $c)
{
echo '<li class="nav-header">'.$c['name'].'</li>';
};
?>
</ul>
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $row['id']; ?></li>
<li class="nav-header"><?php echo $row['cat_name']; ?></li>
</ul>
<?php } ?>
EDIT FIXED by user #jeroen
You have to move the li and a tags inside the while loop.
Thank you for this, F4LLCON
So the final code (not so clean, but works now):
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
?>
<li>
<?php
$meer = $query_row['TITLE'];
$desc_inject = '';
$sub_string = substr($desc_inject, 0, 200);
echo $sub_string." " . '' . '' . $meer . '';
?>
</li>
<br />
<?php
}
?>
I want a drop-down menu that will show the TITLES out of my MySQL database.
I know how to do everything concerning the ID etc.
The only problem is that the PHP version of the drop-down menu will SELECT all the TITLES in my database and parse it as ONE link.
So instead of
Home
>About
>Contact
>Another
it will look like
Home
>About
Contact
Another
With other words
Home
>About Contact Another
It will not make multiple drop-down links but only one drop-down link
If you for example do:
<div>
<?php
echo $query_row['TITLE'];
?>
</div>
It will make a individual <div></div> for every TITLE in my database, so I thought this method would also work for the drop-down links..
Does anybody know how to fix it so it will make individual <li></li> for every TITLE?
Here is a normal drop-down menu:
<li id="media">
<ul>
<li id="1a">About</li>
<li id="1b">Contact</li>
<li id="1b">Another</li>
</ul>
</li>
And here is the PHP drop-down menu:
<li id="media">
<ul>
<li>
<a href="">
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
echo $query_row['TITLE'];
?>
<br />
<?php
}
?>
</a>
</li>
</ul>
</li>
You have to move the li and a tags inside the while loop.
echo '<li>' . $query_row['TITLE'] . '</li>';
<li id="media">
<ul>
<?php
$query = mysql_query("SELECT * FROM `apps` ");
while ($query_row = mysql_fetch_assoc($query))
{
echo "<li><a href=''>" . $query_row['TITLE'] ."</a></li>";
}
</ul>
</li>