Hello Everyone I Want To Ask How To Make Output Using Json_Encode To An Text
Here's My Code
Model
function count_topup($ID)
{
$this->db->select_sum("Jumlah");
$this->db->from('topup');
$this->db->where('id_user',$ID);
$query= $this->db->get();
return $query->result_array();
}
Controller
$data['Count'] = $this->user_model->count_topup($ID);
View
<?php echo json_encode($Count);?>
it will give output [{"Jumlah":"150000"}]
How To Make Only showing 150000? Thanks For The Help
There is no need of json_encode();
Controller:-
$data['Count'] = $this->user_model->count_topup($ID);
$this->load->view('Your_view', $data);
View:-
foreach ($Count as $c)
{
<?php echo $c['Jumlah'];?>
}
Note:- For regarding how Array data is passed from controller to view is
https://codeigniter.com/userguide3/general/views.html
Related
I want to create a dynaic page, I have created model and controller and also data subitted in database successfully. Now, i'm having problem while displaying that data on front end.
Here is my Modal:
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
return $query->result_array();
}
Here is my Controller:
function corporate()
{
$popular['popular'] = $this->auth_model->getPopularcourses();
$data1['corporate'] = $this->auth_model->getcorporate();
$data["institute_details"] = $this->auth_model->getInstitutedetails();
$data1['course'] = $this->auth_model->getcoursesdetailes();
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/corporate', $data1);
$this->load->view('nulearnFront/footer', $popular);
}
Try this
First you can print_r() the data you receive.
print_r($corporate);
After that you can use foreach to display all the data
foreach($corporate as $value)
{
////do code according to your requirement
}
I hope this may be help out to solve your problem
Add View file this code
<?php
if (isset($corporate) && !empty($corporate)) {
foreach ($corporate as $cdata) {
echo $cdata->YourValue(db column name);
}
}
?>
You are so close to the answer. You are passing the data from your Controller class. So what you have to do is just get that data as the follows,
I get the corporate values as it is returning an array data. So here you go,
In your view.php file,
<?php
if (isset($corporate)) { // Check if the data is set or not
foreach ($corporate as $corporateData) {
?>
// Your HTML goes here, table or etc.
<?php echo $corporateData->databaseColumnName // Value that need to print from the database ?>
<?php
}
}
?>
Hope this helps you.
print the query and run it to check
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
print_r($this->db->last_query());die();
return $query->result_array();
}
if query works fine then you can foreah the query
foreach($corporate as $corporate)
{
echo corporate;
}
if it does not return result then change result_array() to result() in model
My database definitely contains values and I am trying to read these values for a specific column (name) into an array like so:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
}
}
then referencing to this function like so:
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
$fieldl = $fields;
$data= array();
$data['fieldl'] = $fieldl;
$this->load->view('clientlist', $data);
}
This basically completes the database query in the model, passes the information into the controller where it puts the array into another array with a key (key being the same name as the array) so that I can pass it into my view, then my view which looks like this :
<html>
<body>
<p> <?php print_r($fieldl); ?></p>
<ul>
<?php
$fieldl = array();
?>
<p> <?php print_r($fieldl); ?> </p>
<?php
foreach($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
Then lists it
but even though I KNOW I have data in "name" the print_r is showing that my array is empty? Help please and thank you!
Are you sure your model is returning any value? If not, the return some:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
return $this->db->get('table_name')->result_array(); // return value
}
Please try the following
public function listcli(){
$this->db->distinct();
$this->db->select('name');
$query = $this->db->get('table_name');
return ($query->num_rows > 0 ? $query->result() : array());
}
If it does not help ...
Please give me table structure with some dummy date, So that I can help you resolve this.
Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}
I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.
It was hard to come up with a title. I am using CodeIgniter with models/views/controller. I have the following tables in my MySQL database that are relevant:
In my model I have the following function:
function get_shoptable() {
$this->db->from('productshop')->where('productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
In my controller I use the above function like
$data['bookshop'] = $this->Product_model->get_shoptable();
In my view I am foreaching $bookshop. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.
Try some like this:
function get_shoptable() {
$this->db->from('productshop')
->join('shop', 'productshop.shopId = shop.shopId')
->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
Model:
function get_products() {
$this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName');
$this->db->from('productshop');
$this->db->join('shop', 'productshop.shopId = shop.shopId');
$this->db->where('productshop.productId', $this->productId);
return $this->db->get()->result_array();
}
Controller:
function products() {
$data['products'] = $this->model_name->get_product();
$this->load->view('products', $data);
}
VIEW:
<?php foreach($products as $p): ?>
<h1><?php echo $p['productUrl']; ?></h1>
<h1><?php echo $p['shopName']; ?></h1>
<?php endforeach(); ?>
get an overlook to active class of codeigniter for details of functions
function get_shoptable()
{
$this->db->from('productshop')
$this->db->join('shop', 'productshop.shopId = shop.shopId')
$this->db->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}