Selected category doesn't remain active after clicking - php

I have a website which is showing products online. In it, I have categories and subcategories listed. When I'm clicking on a category, it doesn't remain active/selected. I have a class="active" which is added to the selected category when clicked but it doesn't expand on selection.
This is my website www.sitimobil.mk. The example site is www.fixit.mk
On the left side you have the categories and subcategories.
<?php
$qKategori = ("SELECT * FROM kategori WHERE kprind = 0");
$rKategori = mysqli_query($dbc, $qKategori);
if ($rKategori) {
while ($exKat = mysqli_fetch_array($rKategori, MYSQLI_ASSOC)) {
$emrikategorise = $exKat['kemri'];
$idkategori = $exKat['kid'];
$idprind = $exKat['kprind'];
?>
<li><?= $emrikategorise; ?>
<ul <?php
if ($_GET['kid'] == $idkategori) {
echo 'class="active"';
}
?>>
<?php
$qPrind = ("SELECT * FROM kategori WHERE kprind = '" . $idkategori . "'");
$rPrind = mysqli_query($dbc, $qPrind);
while ($prind = mysqli_fetch_array($rPrind)) {
?>
<li><a href="kategori.php?kid=<?= $prind['kid'] ?>&kprind=<?= $prind['kprind'] ?>" <?php
if ($_GET['kid'] == 48) {
echo 'class="active"';
}
?>><?= $prind['kemri'] ?></a></li>
<?php
}
mysqli_free_result($rPrind);
?>
</ul>
</li>
<?php
}
mysqli_free_result($rKategori);
}
?>

Related

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

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
}
?>

Dynamic Dropdown menu using PHP & mysql

I've been trying to build a dropdown menu but I'm not getting my desired results. Here's my code:
<?php require_once 'core/init.php'?>
<?php
$sql = 'SELECT * FROM categories WHERE parent = 0';
$pquery = mysqli_query($db,$sql);
?>
<?php while($parent = mysqli_fetch_assoc($pquery)):?>
<?php
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = "parent_id"';
$cquery = mysqli_query($db,$sql2);
?>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<?php echo $parent['id'];?><span class='caret'</span</a>
<ul class='dropdown-menu' role='menu'>
<?php while($child = mysqli_fetch_assoc($cquery)):>
<li><a href='#'><?php echo $child['parent'];?></a>
</li>
<?php endwhile; ?>
</ul>
</li>
<?php endwhile;?>
My DB is like this and the result is this.
Try with console phpmyadmin before write codes in page.
I think your codes is wrong.
Your code must like this
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = '.$parent_id;
I suggest to you for use ajax to be beautiful again.

How to add active class to dynamic menu?

please help. How can I add active class to the dynamic menu? I use While for my menu list, but I cannot understand how to add it to active class. I kindly ask for your consult and advice. Thanks in advance.
picture of the database
<ul class="nav nav-pills" id="mainNav">
<?php
$query = "SELECT * FROM menu ORDER BY id ASC";
$menuQuery = mysqli_query($dbc, $query);
?>
<?php
while($menu = mysqli_fetch_array($menuQuery)):
echo '
<li><a href="'.$menu['menu'].'" class=\'active\'>'.$menu["menu_title"].'</a></li>' ?>
<?php endwhile; ?>
problem solved!
I'll leave this code in hopes of it helping somebody else in need.
<?php $link = $_SERVER['REQUEST_URI']; ?>
<ul class="nav nav-pills" id="mainNav">
<?php
$query = "SELECT * FROM menu ORDER BY id ASC";
$menuQuery = mysqli_query($dbc, $query);
?>
<?php
while($menu = mysqli_fetch_array($menuQuery)):
if($link == $menu['menu']){
$active = 'class=\'active\'';
}
?>
<li <?php if($link == $menu['menu']){ echo $active; } ?> > <a href="<?=$menu['menu'];?>" ><?=$menu["menu_title"];?></a></li>
<?php endwhile; ?>

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>
<?
}
?>

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