passing variables in form_open() in codeigniter - php

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

Related

How To Make Json_encode PHP To An Text String Number

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

Codeigniter php MVC

I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();

Displaying data from database using Codeigniter

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.

CodeIgniter $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

specific data is not coming in codeigniter

i am doing some practice on the codeigniter to retrieve the data from database and i am success to do this.but the problem arise when i want to fetch data of a specific field.
to retrieve the specific value i am using the following URL on my local host:
localhost/codeigniter/index.php/news/view/city-news
where news is controller,view is method of controller and city-news is argument.
Here is my code of controller:
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model.here is the code of this method:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;//here is i m also getting the slug value.
$query = $this->db->get_where('news', array('slug' => $slug));//i think this is not working properly
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
but still my view shows "404 page not found". my view code is:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
?>
Now please tell me where i am going wrong.
your error is you are setting $data['news'] and testing $data['news_item'] :
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item'])) // <=== HERE IS THE ERROR
{
show_404();
}
have you tried with simple where query as follows
$data = $this->db->where('slug', $slug)->from('news');
you must get something in $data. Means at least mysql object. Its been another part that whether rows are available in database or not.
secondly i am worried about the true and false. Means as per i know Boolean store as INT(1) means either 0 or 1. You needs to check with that as well.
you can debug your query with
$this->db->last_query();
Hope this helps.
Here is the cleaned code of the controller:
public function view($slug)
{
$news = $this->news_model->get_news($slug);
if (empty($news))
{
show_404();
}
$data['title'] =$news['title'];
$data['text'] = $news['text'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;
$query = $this->db->get_where('news', array('slug' => $slug));
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
<?php echo '<h2>'.$title.'</h2>'; echo
$text; ?>
A small error:
Here
if (empty($data['news_item'])) // <=== put "news" here
{
show_404();
}
and at your view try to get data as
echo $news;
die() is a function use exit instead.
set this at the beginning of script
ini_set('display_errors', 1);
error_reporting(E_ALL);
also use var_dump($query); to check if it return any result if not exception or fatal error was raised when calling query
if array is empty it means that no news with this slug is in db. You should set some conditions when particular news is not found
also what is the value of this slug, does it contain any special chars etc.?

Categories