Echo results of mysql query with php - 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.

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

creating class active dynamically in php

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.

How to print each row value one by one in php codeigniter

I tried to get the all the rows of a database and it should loop itself for expected output. The image of the output is [enter image description here][1]
And the code is as follows:
In model:
function get_menugroup()
{
$data=array();
$sql = $this->db->query("SELECT MenuName FROM MenuGroup;");
$menu_res = $sql->result();
if($sql -> num_rows() > 0 )
{
return $menu_res;
}
else{
echo "Nothing to process";
}
}`
In controller
public function menugroupname()
{
$menugroups=$this->UserRight_model->get_menugroup();
if ($menugroups){
$data['menu'] = $menugroups;
//$data['count'] = count($data['menu']);
$this->load->view('UserRight_view',$data);
}
}
In view:
<ul class="collapsible" data-collapsible="accordion">
<li>
<?php
if(is_array($menu)){
foreach($menu as $menus){
$menulist = $menus ->MenuName;
//$menulist = $menus['MenuName'];
$fun = explode(",",$menus ->MenuName);
}
$no_menu = count($menu);
//echo $no_menu;
for($i=0; $i<$no_menu ; $i++){
?>
<div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
<div class="collapsible-body"><p>Submenu.</p></div>
</li>
<?php }} else { echo "Wrong way";} ?>
</ul>
And thanks in advance
Try this.
<ul class="collapsible" data-collapsible="accordion">
<li>
<?php
if(($menu)){
foreach($menu[0] as $menus){
$menulist = $menus->MenuName;
$fun = explode(",",$menus ->MenuName); }
$no_menu = count($menu);
for($i=0; $i<$no_menu ; $i++){
?>
<div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
<div class="collapsible-body"><p>Submenu.</p></div>
</li>
<?php }} else { echo "Wrong way";} ?>
</ul>
Hope this helps you!

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

Add automatic classes to each <li>

I have a list of < li >'s but without classes, I want to make for EACH < li > a different class.. something like this
< li class="1" >
< li class="2" >
< li class="3" >
Here is the code that I have:
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li>
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
You can try:
<?php
$i = 1;
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo "className$i"; ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
$i++;
} ?>
Or, instead, you could use the $key from your foreach instead of creating, and incrementing, a counter variable.
So this is how you would do it using the key from $lists. Note I have added a prefix of li- as classes cannot start with a digit. See: Which characters are valid in CSS class names/selectors?
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo 'li-' . htmlentities($key); ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
As all of the classes are unique I would use IDs instead of a class though.
Something like this?:
<ul id="tralila">
<?php
$class_name = 0;
foreach($lists as $key=>$region)
{
?>
<li class="<?=++$class_name;?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>

Categories