Make a menu with the id of the sections with php? - php

I'm very noob in php.
I want create a page with menu dynamically.
If in my code there are:
<section id="one" >...</section>
<section id="two" >...</section>
<section id="three" >...</section>
I want generate this menu:
<li ><a id="menu-one" href="#one"> <span> One </span></a></li>
<li ><a id="menu-two" href="#two"> <span> Two </span></a></li>
<li ><a id="menu-three" href="#three"> <span> Three </span></a></li>
It's very easy but i've tried without succeed in this way:
$html = file_get_html('http://mysite.it');
foreach($html->find('section') as $element){
$idSection=$element->id;
$menuElement .= '<li><a id="menu-'.$idSection.'" href="#'.$idSection.'" ><span>'.$idSection.'</span></a></li>';
echo '<ul>'.$menuElement.'</ul>';
}
EDIT: this could generate an array of section but doesn't work. Is it wrong?
$els = $document->getElementsByTagName('section');
Thanks!

Now you have ONE ul and many li
echo '<ul>'
foreach($html->find('section') as $element){
$idSection=$element->id;
$menuElement .= '<li><a id="menu-'.$idSection.'" href="#'.$idSection.'" ><span>'.$idSection.'</span></a></li>';
echo $menuElement;
}
echo '</ul>'

I think you want something like this ...
$sections = array(
'one' => 'Something',
'two' => 'Something Else',
'three' => 'Something Awesome'
);
// output <section>s
foreach ($sections as $k=>$v) {
echo '<section id="'. $v .'">'. $v .'</section>';
}
// output menu
foreach ($sections as $k=>$v) {
echo '<li ><a id="menu-'. $k .'" href="#'. $k .'"> <span>'. $v .'</span></a></li>';
}

Try this an little change to neokio code the gives what you're looking gor:
$sections = array('one','two','three');
// output <section>s
foreach ($sections as $section) {
echo '<section id="'. $section .'"><li ><a id="menu-'. $section .'" href="#'. $section .'"> <span>'. $section .'</span></a></li></section>';
}

Related

How to add active class in dynamic menu list in codeigniter?

I want to display <li class = "active"> in my current URL when I click any menu.
Here is what I have tried:
<?php
$uri_string = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
if($cekSub->num_rows() > 0){
echo '<li>';
echo '
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($cekSub->result() as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>
The problem is, that if not in the curent URL class, active is hidden - just <li> is displayed.
How to fix this?
it's solved with :
<?php
$getUri = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
$getSub = $cekSub->result();
if($cekSub->num_rows() > 0){
echo '<li';
foreach($getSub as $c)
if($c->controller_link==$getUri){echo ' class="active"';}
echo '>
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($getSub as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li';
if($m->controller_link==$getUri){echo ' class="active"';}
echo '>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>

simplehtmldom return the content of 2 divs inside another

here is my html :
<div id="main">
<div id="child1">
child1
link1
</div>
<div id="child2">
child2
link2
</div>
</div>
I am trying to return (echo in php) child1 and child2 as links
this is part of a HUGE file so I need to loop through it.
this is what I have so far but its not working :
$linkObjs = $html->find('#main');
foreach ($linkObjs as $linkObj) {
$title = trim($linkObj->fildchild()->plaintext);
$link = trim($linkObj->fildchild()->href);
echo '<p class="titro" ><a href="' . $link . '" >' . $title . '</a></p>';
}
Not sure exactly which part of the elements you needed so here's everything dissected.
// Find all divs in #main
foreach ($html -> find('#main div') as $div)
{
// Find plain text in div
foreach ($div -> find('text') as $text)
{
echo $text;
}
// Find <a> tags and href
foreach ($div -> find('a') as $a)
{
echo $a -> href;
}
}

Dynamic Bootstrap Tabs using PHP/MySQL

So I've been tackling this problem for a while now and I can't seem to get it to work. I have a category table and a link in my database. I'm trying to display the "category" title as the tab and the "link" as my tab content.
Let me share my code and I'll explain the problem:
<ul class="nav nav-tabs" id="lb-tabs">
<?php $sqlCat = $db->query('SELECT `tab_title` FROM `category`'); ?>
<?php
foreach ($sqlCat as $row):
echo '<li><a href="#' . $row['tab_title'] . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
endforeach;
?>
</ul>
<div class="tab-content">
<?php foreach ($sqlCat as $row2):
$tab = $row2['tab_title'];?>
<div class="tab-pane active" id="<?php $row['tab_title']; ?>">
<div class="links">
<ul class="col">
<?php
$items = $db->prepare('SELECT u_links.title, u_links.link, u_links.tid, category.id, category.tab_title
FROM u_links, category
WHERE category.id = u_links.tid
ORDER BY category.id ');
$items->execute();
while ($r = $items->fetch(PDO::FETCH_ASSOC)) {
echo '<li>' . $r['title'] . '</li>';
}
?>
</ul>
</div>
</div><!-- /tab-pane -->
<?php endforeach; ?>
</div>
This current code is not displaying the content in the "tab-content" div. I've tried different ways like this for example:
$tab = '';
$content = '';
$link = '';
$tab_title = null;
while($row = $items->fetch(PDO::FETCH_ASSOC)) {
$link = '<li>' . $row['title'] . '</li>';
if ($tab_title != $row['tab_title']) {
$tab_title = $row['tab_title'];
$tab .= '<li><a href="#' . $row['tab_title'] . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
$content .= '<div class="tab-pane active" id="' . $row['tab_title'] . '"><div
class="links"><ul class="col">' . $link . '</ul></div></div><!-- /tab-pane //
support -->';
}
}
With this code I either get as too many tabs(as many items within the category) which I only want one tab for many items(links). Or I'll only get one link per section and doesn't output that row from the database.
If anyone can help me out with this, it will be much appreciated! :) Thank you.
Ok, so I think the issue is how you set your .tab-pane id's. Right now there is but no "echo" so nothing is being output there.
I put together a working demo, I did change some part of your code, but very minor stuff which I tried to comment:
<!-- START OF YOUR CODE -->
<ul class="nav nav-tabs" id="lb-tabs">
<?php
// I just made an array with some data, since I don't have your data source
$sqlCat = array(
array('tab_title'=>'Home'),
array('tab_title'=>'Profile'),
array('tab_title'=>'Messages'),
array('tab_title'=>'Settings')
);
//set the current tab to be the first one in the list.... or whatever one you specify
$current_tab = $sqlCat[0]['tab_title'];
?>
<?php
foreach ($sqlCat as $row):
//set the class to "active" for the active tab.
$tab_class = ($row['tab_title']==$current_tab) ? 'active' : '' ;
echo '<li class="'.$tab_class.'"><a href="#' . urlencode($row['tab_title']) . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
endforeach;
?>
</ul><!-- /nav-tabs -->
<div class="tab-content">
<?php foreach ($sqlCat as $row2):
$tab = $row2['tab_title'];
//set the class to "active" for the active content.
$content_class = ($tab==$current_tab) ? 'active' : '' ;
?>
<div class="tab-pane <?php echo $content_class;?>" id="<?php echo $tab; //-- this right here is from yoru code, but there was no "echo" ?>">
<div class="links">
<ul class="col">
<?php
// Again, I just made an array with some data, since I don't have your data source
$items = array(
array('title'=>'Home','tab_link'=>'http://home.com'),
array('title'=>'Profile','tab_link'=>'http://profile.com'),
array('title'=>'Messages','tab_link'=>'http://messages.com'),
array('title'=>'Settings','tab_link'=>'http://settings.com'),
array('title'=>'Profile','tab_link'=>'http://profile2.com'),
array('title'=>'Profile','tab_link'=>'http://profile3.com'),
);
// you have a while loop here, my array doesn't have a "fetch" method, so I use a foreach loop here
foreach($items as $item) {
//output the links with the title that matches this content's tab.
if($item['title'] == $tab){
echo '<li>' . $item['title'] . ' - '. $item['tab_link'] .'</li>';
}
}
?>
</ul>
</div>
</div><!-- /tab-pane -->
<?php endforeach; ?>
</div><!-- /tab-content -->
<!-- END OF YOUR CODE -->
This help me a lot. I have two static tabs for content creation which drive the dynamic tabs. It is definitely still a little rough but at least the concept is working.
<ul class="nav nav-tabs">
<li class="active">Clusters</li>
<li>Activities</li>
<?php
$sql = "<insert query here>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $rowtab = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$tab_class = ($rowtab['tab_title']==$current_tab) ? 'active' : '' ;
$nospaces = str_replace(' ', '', $rowtab['tab_title']);
echo '<li class="'.$tab_class.'"><a href="#' . urlencode($nospaces) . '" data-toggle="tab">' .
$rowtab['tab_title'] . '</a></li>';
}
?>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
Tab1 Content
</div>
<div class="tab-pane" id="tab2">
Tab2 Content
</div>
<?php
$sql = "<insert query here>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $rowtab = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$tab = $rowtab['tab_title'];
$nospaces = str_replace(' ', '', $rowtab['tab_title']);
$content_class = ($tab==$current_tab) ? 'active' : '' ;
echo '<div class="tab-pane'. $content_class.'" id="'.$nospaces.'">';
echo 'You are looking at the '.$tab.' tab. Dynamic content will go here.';
echo '</div><!-- /.tab-pane -->';
}
?>
</div>
foreach ($files as $file):
$filename = preg_replace("/\.html$/i", "", $file);
$title = preg_replace("/\-/i", " ", $filename);
$documentation = 'usage/'.$type.'/'.$file;
$tab1 = 'usage/'.$type.'/'.$file;
echo '<div class="sg-markup sg-section">';
echo '<div class="sg-display">';
echo '<h2 class="sg-h2"><a id="sg-'.$filename.'" class="sg-anchor">'.$title.'</a></h2>';
//TAb Set up
echo '<div class="row"><div class="col-md-12">';
echo '<ul class="nav nav-tabs" role="tablist">';
echo '<li role="presentation" class="active">Visual</li>';
echo '<li role="presentation">Rules</li>';
echo '</ul>';
echo '</div>';
echo '</div>';
//Visual Tab Content
echo '<div class="row"><div class="col-md-12">';
echo '<div class="tab-content">';
echo '<div role="tabpanel" class="tab-pane active" id="Visual">';
echo '<h3 class="sg-h3">Visual</h3>';
include('markup/'.$type.'/'.$file);
//View Source
echo '<div class="sg-markup-controls"><a class="btn btn-primary sg-btn sg-btn--source" href="#">View Source</a> <a class="sg-btn--top" href="#top">Back to Top</a> </div>';
echo '<div class="sg-source sg-animated">';
echo '<a class="btn btn-default sg-btn sg-btn--select" href="#">Copy Source</a>';
echo '<pre class="prettyprint linenums"><code>';
echo htmlspecialchars(file_get_contents('markup/'.$type.'/'.$file));
echo '</code></pre>';
echo '</div><!--/.sg-source-->';
echo '</div>';
//Documentation Tab Content
if (file_exists($documentation)) {
echo '<div role="tabpanel" class="tab-pane" id="Rules">';
echo '<h3 class="sg-h3">Rules</h3>';
include($documentation);
//View Source
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
}
//Documentation Tab1 Content
echo '<div role="tabpanel" class="tab-pane" id="Tab1">';
echo '<h3 class="sg-h3">Tab1</h3>';
include($tab1);
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
//Documentation Tab2 Content
echo '<div role="tabpanel" class="tab-pane" id="Tab2">';
echo '<h3 class="sg-h3">Tab2</h3>';
// include($tab2);
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
echo '</div>'; //End Tab Content
echo '</div>'; //End Column
echo '</div>'; //End Row
//echo '<div class="row"><div class="col-md-12">';
//echo '<h3 class="sg-h3">Example</h3>';
//include('markup/'.$type.'/'.$file);
//echo '</div>';
// if (file_exists($documentation)) {
// echo '<div class="col-md-12"><div class="well sg-doc">';
// echo '<h3 class="sg-h3">Rules</h3>';
// include($documentation);
// echo '</div></div></div>';
// }
echo '</div><!--/.sg-display-->';
//echo '</div><!--/.colmd10-->';
echo '</div><!--/.sg-section-->';
endforeach;
}

Outputting array contents as nested list in PHP

I have the array array ( [0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10)) and I would like to display it like this:
<ul>
<li>
<a href=""/>FIRST ELEMENT OF THE array ==> 1</a>
<a href=""/>2ND ELEMENT OF THE TAB ==> 2</a>
<a href=""/>3THIRD ELEMENT==> 3</a>
<a href=""/>FORTH ELEMENT OF THE TAB ==> 4</a>
<a href=""/>FIFTH ELEMENT==> 5</a>
</li>
<li>
<a href=""/>6th ELEMENT==> 6</a>
<a href=""/>7th ELEMENT OF THE TAB ==> 7</a>
<a href=""/>8th ELEMENT==> 8</a>
<a href=""/>9th ELEMENT OF THE TAB ==> 9</a>
<a href=""/>10th ELEMENT OF THE TAB ==> 9</a>
</li>
</ul>
How can I achieve this in PHP? I am thinking of creating a sub array with array_slice.
Updated to take into account your actual array structure
Your solution is a simple nested foreach.
$tab = array(array(1,2,3,4,5), array(6,7,8,9,10));
echo '<ul>';
foreach ($tab as $chunks) {
echo '<li>';
foreach($chunks as $chunk) {
echo '' . $chunk . '';
}
echo '</li>';
}
echo '</ul>';
try
echo "<ul>";
$i=0;
$theCount = count($tab);
while($i<$theCount){
echo "<li>";
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
$i++;
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
echo "</li>";
$i++;
}
echo "</ul>";
Here is another way to do this (demo here):
<?php
$tab = array(1,2,3,4,5,6,7,8,9,10);
//how many <a> elements per <li>
$aElements = 2;
$totalElems = count($tab);
//open the list
echo "<ul><li>";
for($i=0;$i<$totalElems;$i++){
if($i != 0 && ($i%$aElements) == 0){ //check if I'm in the nTh element.
echo "</li><li>"; //if so, close curr <li> and open another <li> element
}
//print <a> elem inside the <li>
echo "<a href =''>".$tab[$i]."</a>";
}
//close the list
echo "</li></ul>";
?>
Tip explanation: $i%n (mod) equals 0 when $i is the nTh element (remainder of division is 0)
EDITED: made a general solution
<?php
for($i = 0 ; $i < count($tab) ; $i += 2) {
echo "<a href>" . $tab[$i] . "</a>";
echo "<a href>" . $tab[$i+1] . "</a>";
}
?>
Like that.
try this:
$sections = array_chunk(array(1,2,3,4,5,6,7,8,9,10), 2);
echo '<ul>';
foreach($sections as $value)
{
echo '<li>';
echo '<a href=""/>'.$value[0].' ELEMENT OF THE TAB ==> '.$value[0].'</a>';
echo '<a href=""/>'.$value[1].' ELEMENT OF THE TAB ==> '.$value[1].'</a>';
echo '</li>';
}
echo '</ul>';

How do I add different class for my ordered lists

I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>

Categories