list <ul> and <li> php mysql with id - php

here I want to create a list of <ul> and <li> which is not limited when input in the database, here I do not find any problem in creating <li> and <ul> in one or two lists and three lists using <ul> and < li>, but here I want to create an infinite list of <li> and <ul>, can someone help me?
my database :
my code :
<?php
$get_main = $db->query("SELECT * FROM tb_akun WHERE parent_akun = '0'");
foreach($get_main AS $main){
$main_kode = preg_replace('/0+/','',$main['kode_akun']);
?>
<div class="ulli-none">
<ul>
<li><?= $main_kode ?> - <?= $main['nama_akun'] ?>
<ul>
<?php
$get_parent = $db->query("SELECT * FROM tb_akun WHERE parent_akun = {$main['kode_akun']}");
foreach($get_parent AS $parent){
?>
<li><?= $main_kode ?>-<?= $parent['kode_akun'] ?> <?= $parent['nama_akun'] ?>
<ul>
<?php
$get_subs = $db->query("SELECT * FROM tb_akun WHERE parent_akun = {$parent['kode_akun']}");
foreach($get_subs AS $subs){
?>
<li><?= $main_kode ?>-<?= $subs['kode_akun'] ?> <?= $subs['nama_akun'] ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
</ul>
</div>
<?php } ?>
the final result :

You'll need a recursive function for this.
<?php
$get_main = $db->query("SELECT * FROM tb_akun WHERE parent_akun = '0'");
?>
<div class="ulli-none">
<?php
recursiveList($get_main, $db);
?>
</div>
<?php
function recursiveList($items, $db, $mainPrefix = ''){
if (empty($items)){
return;
}
?>
<ul>
<?php
foreach($items AS $item){
if (!$mainPrefix) {
$prefix = $mainPrefix = preg_replace('/0+/','', $item['kode_akun']);
} else {
$prefix = $mainPrefix . ' - ' . $item['kode_akun'];
}
?>
<li>
<span><?= $prefix ?> - <?= $item['nama_akun'] ?></span>
<?php
$subs = $db->query("SELECT * FROM tb_akun WHERE parent_akun = {$item['kode_akun']}");
recursiveList($subs, $db, $mainPrefix);
?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
Keep in mind that you're querying the subs for every record. This will become time consuming when there're a lot of records. To further optimize this you might want to fetch all records and collect them within an array; E.g.
$get_main = $db->query("SELECT * FROM tb_akun");
$all = [];
foreach ($get_main as $item) {
if (empty($all[$item['parent_akun']])) {
$all[$item['parent_akun']] = [];
}
$all[$item['parent_akun']][$item['kode_akun']] = $item;
}
?>
<div class="ulli-none">
<?php
recursiveList($all);
?>
</div>
<?php
function recursiveList($all, $parentId = 0, $mainPrefix = ''){
if (empty($all[$parentId])){
return;
}
$items = $all[$parentId];
?>
<ul>
<?php
foreach($items AS $item){
if (!$mainPrefix) {
$prefix = $mainPrefix = preg_replace('/0+/','', $item['kode_akun']);
} else {
$prefix = $mainPrefix . ' - ' . $item['kode_akun'];
}
?>
<li>
<span><?= $prefix ?> - <?= $item['nama_akun'] ?></span>
<?php
recursiveList($all, $item['kode_anun'], $mainPrefix );
?>
</li>
<?php
}
?>
</ul>
<?php
}
?>

Related

my query in codeigniter cannot produce result

i've been debbuging it many times based on my knowledge, i am new to codeigniter, any solution idea would be much appreciated... ty
here's my code
MODEL:
function userdisplay_info($id){
$result = $this->db->query("SELECT uid FROM user WHERE userIdentificationNumber LIKE '$id'");
//if($result->num_rows() > 0){
$info_data = $result->result();
$row = $info_data[0];
$result->free_result();
}
CONTROLLER:
function r_book(){
$data = array();
$bid = $this->input->post('bid');
$u_id = $this->input->post('userinputid');
$data = array(
'book_reserved' => 1
);
if($this->module2->reserved_book($bid,$data))
{
$data['info'] = $this->module2->userdisplay_info($u_id);
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpage',$data);
$this->load->view('User/template/footer');
return true;
}else
{
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpagefail');
$this->load->view('User/template/footer');
return false;
}
}
VIEW:
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<?php if($info) : foreach($info as $u_info) : ?>
<ul class="list-unstyled">
<li>Last Name : <?php echo $u_info->uid; ?></li>
<li>First Name : <?php echo $u_info->userFirstname; ?></li></li>
<li>Middle Name : <?php echo $u_info->userMiddlename; ?></li></li>
<li>Course : <?php echo $u_info->userCourse; ?></li></li>
</ul>
<?php endforeach; ?>
<?php else :?>
<h4> No Record Found</h4>
<?php endif; ?>
</div>
</div>
</div>
Rewrite your model function as
function userdisplay_info($id){
$this->db->select('uid');
$this->db->like('userIdentificationNumber', $id);
$query = $this->db->get('user');
return $query->result_array();
}

Insert code for only first queried row then nothing

I need the first class to be active and then empty from then on.
<?
$slidesinfo = mysql_query("SELECT * FROM slides ORDER BY id ASC LIMIT 4");
while ($slidesingo = mysql_fetch_array($slidesinfo))
{
?>
<li class="active"><span><? echo $slidesingo['title'] ?></span></li>
<?
}
?>
So the result should be:
<li class="active"><span>Title 1</span></li>
<li><span>Title 2</span></li>
<li><span>Title 3</span></li>
<li><span>Title 4</span></li>
try this code.
$i = 1;
while ($slidesingo = mysql_fetch_array($slidesinfo))
{
$cls = "";
if($i==1)
{
$cls = "active";
$i++;
}
?>
<li class="<?php echo $cls;?>"><span><? echo $slidesingo['title'] ?></span></li>
<?
}
?>

Menu Items Not Closing?

I'm coding a staff panel but I'm stuck I've added a menu (navigation) but I'm stuck on how to go about opening it when clicked and closing it when clicked if open.
Here is the code i have so far;
<ul id="menu" class="nav">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="menustyle" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<div class="menutext"><?php echo $array['name']; ?></div>
</div>
<ul>
<li class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> onclick="Radi.menuToggle('<?php echo $array['id']; ?>');" style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' AND visible = '1' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<li>
<?php echo $array2['text']; ?>
</li>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</ul>
</li>
<?php
}
}
?>
</li>
</ul>
</div>
And this is the defualt code when you download radipanel;
<div style="float: left; width: 200px;">
<?php
$url = $_GET['url'] ? $core->clean($_GET['url']) : 'core.home';
$query3 = $db->query("SELECT * FROM menu WHERE url = '{$url}'");
$array3 = $db->assoc($query3);
if (!$array3['usergroup']) {
$array3['usergroup'] = "invalid";
}
$query = $db->query("SELECT * FROM usergroups ORDER BY weight ASC");
while ($array = $db->assoc($query)) {
if (in_array($array['id'], $user->data['uGroupArray'])) {
?>
<div class="box">
<div class="square menu" style="background: #<?php echo $array['colour']; ?>;" onclick="Radi.menuToggle('<?php echo $array['id']; ?>');">
<img id="menutoggle_<?php echo $array['id']; ?>" class="menutoggle" src="_img/<?php echo ( $array['id'] != $array3['usergroup'] ) ? 'plus' : 'minus'; ?>_white.png" alt="Toggle" align="right" />
<strong><?php echo $array['name']; ?></strong>
</div>
<div class="menuitems"<?php if ($array['id'] != $array3['usergroup']) { ?> style="display: none;"<?php } ?> id="mitems_<?php echo $array['id']; ?>">
<?php
$query2 = $db->query("SELECT * FROM menu WHERE usergroup = '{$array['id']}' ORDER BY weight ASC");
$i = "a";
while ($array2 = $db->assoc($query2)) {
?>
<a href="<?php echo $array2['url']; ?>" class="<?php echo $i; ?>">
<?php echo $array2['text']; ?>
</a>
<?php
$i++;
if ($i == "c") {
$i = "a";
}
}
?>
</div>
</div>
<?php
}
}
?>
</div>
So any ideas on why my code is not doing it?

Change Magento Menu Subcategory Sort Order

I am using the following code to display Categories and Subcategories in a drop down menu, however I want the subcategories to be sorted alphabetically... how can I do this?
<ul id="custom-menu">
<li><b>Home</b></li>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul style="width:940px;">
<?php foreach($_categories as $_category): ?>
<li>
<b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a></b>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<div class="dropdown_1column" style="width:235px;">
<?php foreach($_subcategories as $_subcategory): ?>
<div class="col_1">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</div>
<?php endforeach; ?>
<div class="clr" clear="all"></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
<li class="normal"><b>Sale</b></li>
<li class="normal"><b>Help</b></li>
<li class="normal"><b>Open Account</b></li>
</ul>
<?php endif; ?>
What I've always done as our clients don't usually have categories that change frequently is dragged and dropped them in alphabetical order in the admin backend: Catalog > Categories > Manage Categories.
If that's not your style, you can always take some advice from this answer to first run through the categories, place them in a new array, and then key sort the array and finally output: https://stackoverflow.com/a/4273912/823549
First backup your topmenu.phtml then replace the following code in your new topmenu.phtml file
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
?>
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div class="nav-container">
<ul id="nav">
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0){ ?>
<?php foreach($_categories as $_category){ ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<li><span><?php echo $_category->getName(); ?></span>
<?php $catList = array();?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php foreach($_subcategories as $_subCategory){ ?>
<?php $catList[] = array('name' => $_subCategory->getName(), 'url' => $_subCategory->getUrl(), 'id' => $_subCategory->getId());?>
<?php } ?>
<?php $catList = array_sort($catList, 'name', SORT_ASC);?>
<ul>
<?php if (count($catList) > 0){ ?>
<?php $subcat=0?>
<?php foreach($catList as $_subCategory){ ?>
<li><span><?php echo $_subCategory['name'] ?></span>
<?php $subCatList = array();?>
<?php $_subSubCat = Mage::getModel('catalog/category')->load($_subCategory['id']);
$_subSubCategories = $_subSubCat->getChildrenCategories();?>
<?php foreach($_subSubCategories as $_subSubCategory){ ?>
<?php $subCatList[] = array('name' => $_subSubCategory['name'], 'url' => $_subSubCategory['url']);?>
<?php } ?>
<?php $subCatList = array_sort($subCatList, 'name', SORT_ASC);?>
<?php if (count($subCatList) > 0){ ?>
<ul>
<?php foreach($subCatList as $_subSubCat){ ?>
<li><span><?php echo $_subSubCat['name'] ?></span>
<?php } ?>
</li>
</ul>
<?php } ?>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</div>

Echo results of mysql query with php

I have this query
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);
I can echo the results within a unordered list using a while loop like this
<ul>
<?php {do { ?>
<li><?php echo $row_people['name'];?></li>
<?php } while ($row_people = mysql_fetch_assoc($people));}?>
</ul>
But I can't used this as my html does not allow it.
<ul>
<li class="first">
Kate
<li>
<li class="second">
<img src="john.jpg" />John
<li>
<li class="third">
<span>Max</span>
<li>
</ul>
My question is how can echo the name that was retrieved from the database into the appropriate place within this html?
Thanks for your help.
Try this:
<?php
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
if(mysql_num_rows($people) > 0){
?>
<ul>
<?php
while ($row_people = mysql_fetch_assoc($people)){
?>
<li><?php echo htmlentities($row_people['name']);?></li>
<?php
}
?>
</ul>
<?php
}
?>
You'll just have to create a renderer for each "type" of user (assuming you have a type property on the user rows) or based on their attributes. For example, let's say you're going to have to filter based on the attributes:
<?php
function render_simple($person) {
return '' . $person['name'] . '';
}
function render_with_image($person) {
return '<img src="' . $person['image'] . '.jpg"/>' . $person['name'] . '';
}
function render_special($person) {
return '<span>' . $person['name'] . '</span>';
}
function render_person($person) {
if ($person['image']) {
return render_with_image($person);
}
if ($person['special']) {
return render_special($person);
}
return render_simple($person);
}
$i = 0;
while ($row_people = mysql_fetch_assoc($people)){ ?>
<li class="index<?php echo ++$i; ?>">
<?php echo render_person($person); ?>
</li>
<?php
}
?>
This should work, with the exception that instead of class names first, second, etc, you'll now have index1, index2, etc.

Categories