How to get the options? - php

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>'

Related

Retrieve data from two tables in PHP Codeigniter

I want to retrive count of datas from one table and some datas from another table , also i want to know how to display these in a single foreach loop.
Model
public function counter(){
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$cnt = $this->db->count_all_results();
}
Controller
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
}
View part :
<?php foreach ($records as $r) { ?>
<div class="col-sm-6 col-md-3 col-xs-12">
<div class="bg-purple">
<h4 class="text-white">
<?php echo $r->category_name; ?>
</h4>
<span>200</span> Packages
<br>
<a style="margin-top: 10px;" href="<?php echo site_url() . "/gt_package/listing/" . $r->category_id ?>" class="btn btn-default">View Packages</a>
</div>
</div>
<?php } ?>
As per your question to pass the data model to controller code bellow
Model:
public function counter()
{
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$query = $this->db->get('gt_package');
$cnt = $query->num_rows();
return $cnt;
}
Controller:
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
if($res){
$data['result'] = $res;
$this->load->view('manage_accounts_view', $data);
} else {
echo "Fail";
}
print_r($data['result']);
}

Displaying the blog pagetitle as a url in codeigniter php

I am having a blog page where the blogs are inserted from admin panel and blog title will be inserted into new column as separated by " - " if there are spaces between the page titles.For example if the page title is " welcome to something " then in the database it is will be inserted into two columns. In once column it will be inserted as same and in other column it will be inserted as welcome-to-something.
when clicking on readmore button i need to display in url as (www.example.com/blob/article/welcome-to-something) in this format i need to display the url.
Here is the code:
Controller:
public function index()
{
$this->load->model('blogs_model');
$data["records2"] = $this->blogs_model->get_all_blogs($config["per_page"], $page);
$data['mainpage'] = "blog";
$this->load->view('templates/template',$data);
}
public function article()
{
$this->load->model('blogs_model');
$data['records2']= $this->blogs_model->getblogsdata($this->uri->segment(3));
$data['mainpage']='blogs';
$this->load->view('templates/templatess',$data);
}
Model:
function get_all_blogs()
{
$this->db->select('B.*');
$this->db->from('blogs AS B');
$this->db->where(array('B.status'=>1));
$this->db->order_by("position", "asc");
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function getblogsdata($id)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blog_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div class="col-md-9 blogs">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="blog1">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" class="testimonials1"/>
<h3 class="heading1"><?php echo $r->blog_title;?></h3>
<div class="blogtext1 read">
<?php echo $r->description;?>
</div>
<a href="<?php echo base_url()?>blog/article/<?php echo $r ->blog_id ;?>" class="read_more7" target="_blank" >Read More</a>
</div>
<?php endforeach ;endif;?>
<div class="pagination"><?php echo $links; ?></div>
</div>
blogs table
blog_id | blog_title | blogtitle
1 welcome to something welcome-to-something
Model:
function getblogsdata($id,$slug)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blogtitle'=>$id));
$this->db->where(array('blogs.blogtitle' => $slug));
$this->db->order_by("ne_views", "asc");
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<a href="<?php echo base_url()?>blog/article/<?php echo $r->blogtitle;?>" class="read_more7" target="_blank" >Read More</a>

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!

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();
}

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