I am trying to generate the bellow menu dynamically using php and mysql
<ul id="prod_nav" class="clearfix">
<!-- top --> <li class="active"><span class="down">Clothes</span>
<ul>
<h1> Men </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>Formal shoes </li>
<li>Sport shoes </li>
<li>Suit </li>
<li>Underwear </li>
<li>Socks </li>
<li>Pants </li>
</ul>
<ul>
<h1> Women </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>High heel shoes </li>
<li>Sport shoes </li>
<li>Wedding clothes </li>
<li>Underwear </li>
<li>Leather </li>
<li>Socks </li>
<li>Pants </li>
</ul>
</li>
</ul>
but I am not sure which way is the best principals for generating the menu?
should I use while loop and if or Case or for loop?
which way is the best way?
Thanks
You should have an array
$a=array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
and use it like this:
<ul>
<h1>MEN</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
<ul>
<h1>womaen</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
$gender = array('men','women');
$productlist = array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product) {
<li><?php echo $product; ?></li>
}
echo "</ul>";
}
can you please used this:
$gender = array('men','women');
$productlist = array('shirt'=>'shirt_url.php','t-shirt'=>'t-shirt_url.php','polo shirt'=>'polo_shirt.php');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product_key=>$product_url) {
?>
<li><?php echo $product_key; ?></li>
<?php
}
echo "</ul>";
}
Related
I want to fetch some data (student details) from database but my code is not working.
This is my Controller
public function Student_Detail()
{
$student_id = $this->uri->segment(3);
$record = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
}
This is my Model
public function Student_Details($student_id)
{
$query = $this->db->query("SELECT s.`student_id`,
s.`std_email`,
s.`std_fname`,
s.`std_lname`
FROM `student_main` AS s
WHERE s.`student_id` = $student_id");
return $query->result();
}
This is my View
<section class="panel">
<div class="user-heading">
<img src="<?=base_url();?>images/profile-avatar.jpg" alt="">
</div>
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
</section>
Note that there is not problem with the query. I want to know how to fetch student details. It gives me the following error. Message : Undefined variable: record
Try this:
In your controller:
$data['record'] = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/students/student_details', $data);
And in your view:
<ul class="nav nav-pills nav-stacked">
<?php
foreach($record->$row){
?>
<li> <?php echo $row->std_fname; ?></li>
<li> <?php echo $row->std_lname; ?></li>
<li> <?php echo $row->std_email; ?></li>
<?php
}
?>
</ul>
You need to pass your model data into record array then pass into view
Controller
$student_id = $this->uri->segment(3);
$record['data'] = $this->WebAdmin_Model->Student_Details($student_id);// store data into record array
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
Use foreach loop to retrieve data
Views
<?php
foreach($data as $record)// use foreach loop
{ ?>
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
<?php } ?>
You need to do it like this, because codeigniter try to extract variable from $record and it fails. So to make possible, make it,
$record['record'] = $this->WebAdmin_Model->Student_Details($student_id);
and then in your view,
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
Because codeigniter will extract variable from $record array
I have a navigation menu in my includes file, but I am having trouble echoing out the menu across my site. If I try and place the code as so, the page renders out mark up where the nav menu should be.
The code works somewhat if I do not add the <?php function before the menu.
I'm wondering how do I echo this code out within a function. I've tried changing the single quotes to double, but I think I am getting confused by the echo that is within the li in the menu.
A solution would be great.
Thanks
<?php
function nav() {
echo
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
}?>
The simplest is not to use the initial echo. Notice the ?> after the function name and <?php added again at the bottom for the ending function curly brace.
<?php
function nav() {
?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
<?php
}?>
Replace with this:
<?php
function nav() { ?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
You dont need echo for the showing the html just close the php tag before the html
Hope this will help!!
<?php
function nav() {
?>
<nav class='container'>
<ul>
<?php if(strpos($_SERVER['PHP_SELF'],'current-journal' )) {echo "<li><a class='current' href='current-journal'>CURRENT JOURNAL<span class='sub-nav'>Our latest and greatest!</span></a></li>"; }?>
<?php if(strpos($_SERVER['PHP_SELF'],'submit' )) {echo "<li> <a class='current' href='submit'>SUBMIT<span class='sub-nav'>Your writing</span></a></li>"}?>
<?php if(strpos($_SERVER['PHP_SELF'],'submission-guidelines' )) {echo "<li> <a class='current' href="submission-guidelines'>SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'contributors' )) {echo "<li> <a class='current' href='contributors'>CONTRIBUTORS<span class='sub-nav'>See who\'s in our magazine\'s latest issue</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'past-journals' )) {echo "<li> <a class='current' href='past-journals'>PAST JOURNALS<span class='sub-nav'>Browse our issue archives</span></a></li> ";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'blog' )) {echo "<li> <a class='current' href='blog'>BLOG<span class='sub-nav'>Just a blog</span></a></li>";}?>
</ul>
</nav>
<?php
}
?>
This should do the work.
I have a page that displays an organizational tree however in level 2 there are repeated items. What I wanted to is to avoid the duplicates...
Here's the example display:
<ul id="org" style="display: none;">
<li>Brit School
<ul>
<li>Amy Winehouse
<ul>
<li>Carina Round</li>
</ul>
</li>
<li>Adele Adkins
<ul>
<li>Kreayshawn K</li>
<li>Leona Lewis</li>
</ul>
</li>
<li>Adele Adkins
<ul>
<li>Kreayshawn K</li>
<li>Leona Lewis</li>
</ul>
<li>Arctic Monkey
<ul>
<li>PJ Harvey</li>
</ul>
</li>
</ul>
</li>
I wanted to omit the 2nd record Adele this is my example code in PHP
<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
<li>
<?php echo $genesA->LevelFullName1?>
<?php if($genesA->lvlMemF) {?>
<ul>
<?php if($genesA->lvlMemF) {?>
<?php foreach ($lvl2 as $genesB) {?>
<?php if($genesB->lvlMemS) {?>
<li><?php echo $genesB->LevelFullName2 ?>
<?php if($genesB->lvlMemS) {?>
<ul>
<?php if($genesB->lvlMemS) {?>
<?php foreach ($lvl3 as $genesC) {?>
<?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
<li>
<?php echo $genesC->LevelFullName3?>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } else { ?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php }?>
</li>
<?php } ?>
I'm not so sure if I am heading on the right track I just want a dirty fix that's all :)
that should do it:
<?php
$seen = array();
?>
<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
<li>
<?php echo $genesA->LevelFullName1?>
<?php if($genesA->lvlMemF) {?>
<ul>
<?php if($genesA->lvlMemF) {?>
<?php foreach ($lvl2 as $genesB) {?>
<?php if($genesB->lvlMemS) {?>
<li><?php echo $genesB->LevelFullName2 ?>
<?php if($genesB->lvlMemS) {?>
<ul>
<?php if($genesB->lvlMemS) {?>
<?php foreach ($lvl3 as $genesC) {?>
<?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
<?php if(!in_array($genesC->LevelFullName3, $seen)?>
<li>
<?php echo $genesC->LevelFullName3?>
<?php $seen[] =$genesC->LevelFullName3;?>
</li>
<?php }?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } else { ?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php }?>
</li>
</ul>
you might want to check if my if is at the right place there because it could be that you get empty ul blocks if all the names already were seen.
I have 10 users in an array:
$array = array(
"aaa",
"bbb",
"ccc",
"ddd",
"eee",
"fff",
"ggg",
"hhh",
"iii",
"jjj",
);
And I want to display lists based on 5 or fewer users e.g.:
<ul>
<li>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</li>
<li>
<ul>
<li>fff</li>
<li>ggg</li>
<li>hhh</li>
<li>iii</li>
<li>jjj</li>
</ul>
</li>
</ul>
At the moment I have:
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user ?></li>
<?php endforeach; ?>
</ul>
However I am not creating the inner uls. What is the best way to approach this? Using a for loop and counting out 5? Or is there a neater method?
Use array_chunk() to split an array into multiple arrays with a specified number of items.
<ul>
<?php
$users = array_chunk($array, 5);
foreach ($users as $user) {
echo "<li><ul>";
foreach ($user as $idv) {
echo "<li>" . $idv . "</li>";
}
echo "</ul></li>";
}
?>
</ul>
Look an alternative solution :)
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,0,5)).'</li>';
?>
</ul>
</li>
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,5,10)).'</li>';
?>
</ul>
</li>
<ul>
<?php
$lenght = count($users);
for( $i=0; $i<$lenght; $i++ ){
if( $i%5 == 0 ){
echo '<li><ul>';
}
echo '<li>'.$users[0].'</li>';
if( $i%5 == 4 || ($i+1==$lenght) ){
echo '</ul></li>';
}
}?>
</ul>
Hello :) I realy need your help here. I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this:
<ul>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
</ul>
Here is code I have:
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i++%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
</li>
<?php endforeach; ?>
</ul>
But as the result I get structure like this:
<ul>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
</ul>
Thank you for help
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
<?php if ($i%$itemCount==2): ?>
</li>
<?php endif; $i++; ?>
<?php endforeach; ?>
</ul>
You can try this.
<ul class="some-ul-class">
<?php $itemCount = 3;
$i=0;
foreach ($p->getItems() as $pt):
if ($i%$itemCount==0):
echo '<li class="item">';
endif;
echo "<div>$pt</div>";
if ($i%$itemCount==2):
echo '</li>';
endif; $i++;
endforeach; ?>
</ul>
Try something like this:
<ul class="some-ul-class">
<?php $itemCount = 4; ?>
<li>
<?php $i = 1; foreach ($p->getItems() as $pt): ?>
<?php if ( $i % $itemCount == 0): ?>
</li><li>
<?php endif; ?>
<?php $i++; ?>
<div><?php echo $pt; ?></div>
<?php endforeach; ?>
</li>
</ul>
This generates:
<ul class="some-ul-class">
<li>
<div>1</div>
<div>2</div>
<div>3</div>
</li><li>
<div>4</div>
<div>5</div>
<div>6</div>
</li>
</ul>
Demo
Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. The solution is to break the initial DB result set in to chunks with array chunk.
This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6)
Run through the loop below you will get two LI with 3 nested DIV. The code is not tested but should be something like operational.
<?php
$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);
// Your initial item array
$rows = 3;
// Number of rows in each li
$items = array_chunk($items, $rows);
// Final nested array in blocks of 3
if ($items) {
echo "<ul class='some-ul-class'>\n";
foreach ( $items as $item ) {
echo "<li class='items'>\n";
foreach ($item as $divs) {
echo "<div>{$divs}</div>\n";
}
echo "</li>\n";
}
echo "</ul>\n";
}
?>