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!
Related
I'm trying to do a practice session. In the view page each question and the corresponding options has to be displayed.I am able to get all the questions but not the options. I think I need to pass each question id for displaying the corresponding options. But I'm not able to get that. Somebody plz help to get the options according to the question.
Controller code:
function index() {
$data['qn'] = $this->questions_model->questions_list();
foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);
$data['qq_id'] = $data['qn']['$qs_id'];
//print_r($data['qq_id']);
}
$this->load->view($this->session->userdata('web_view') . '/header', $data);
$this->load->view($this->session->userdata('web_view') . '/questions_list', $data);
$this->load->view($this->session->userdata('web_view') . '/footer', $data);
}
Model code:
function questions_list() {
$this->db->select('qbank.*,question_category.category_name');
$this->db->from('qbank');
$this->db->join('question_category','qbank.cid = question_category.cid');
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('qid',$qs_id);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
view code:
<form id="profilefrm" class="sky-form" name="profilefrm" method="post">
<ol>
<?php foreach($qn as $row){ ?>
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php foreach($qq_id as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>
<?php } ?>
</ol>
</form>
controller
'foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);'
model
'function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('q_id','$qs_id');
$this->db->limit(4);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
} '
view
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php
$qs_id = $row->qid;
foreach($row['$qs_id'] as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>'
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();
}
I'm using a jquery image slider that, display images 3 by 3. All the images are coming from database. This is my code:
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
$y=0;
while($y < 3) {
if($data[$y] != '') {
$imgNew = $data[$y];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
$y++;
}
?>
</div>
</li>
<?php
}
?>
</ul>
And this code is displaying just 1 image, and it's repeating.
plz help me guys. thanks.
Here I got this working... displayed 3x3 and you can check source... each img src increments by one each time. so that will give you 3 X 3.
heres my code, just need to adapt it a little (remove the array I made and replace it with yours.
<?php
$datas = array(1,2,3,4,5,6,7,8,9);
$y=0;
echo "<li>";
foreach ($datas as $data)
{
if($data != "")
{
$y++;
echo "<img src=" . $data . " alt='' />";
if($y % 3 == 0)
{
if($y < count($datas))
{
echo"</li>";
echo "<li>";
}
else
{
echo"</li>";
}
}
}
}
?>
</ul>
if you need any help, please comment.
Try this,
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
if($data[$x] != '') {
$imgNew = $data[$x];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
?>
</div>
</li>
<?php
}
?>
</ul>
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>
<?
}
?>
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.