Codeigniter returns view with null variable - php

When i post the data from the other form , and when i test it in print($id)it works, but when i pass it in the view, when the view loads , it returns as null.
Here is my code :
Jquery post
function viewAccount(orId)
{
account_id=orId;
// alert(account_id);
var postData={
"id":account_id
};
$.post(base_url+"admin/viewAccount", postData).done(function(data){
inlcudeViews('admin/viewAccount');
});
}
Controller:
function viewAccount()
{
$id = $this->input->post('id');
$data = array('id' => $id);
// print_r($data); IT WORKS TILL HERE
$this->load->view('admin/viewAccount', $data);
}
View:
// HERE IS WHERE I GET A NULL VARIABLE
var data='<?= $id ?>';
$(document).ready(function() {
alert(data);
});

Instead of using alert in viewAccount view you just need to print or echo your $id like,
<?php
echo isset($id) ? $id : '';
?>
Also, verify you have already enable PHP short tags in php.ini. If your are using <?= $id ?>

Your Jquery and View is perfect. But in Controller you have to write
$this->load->view('admin/viewAccount', $data[0]);
instead of
$this->load->view('admin/viewAccount', $data);
or you can write
function viewAccount()
{
$id = $this->input->post('id');
$data['id'] =$id;
// print_r($data); IT WORKS TILL HERE
$this->load->view('admin/viewAccount', $data);
}

You need to pass your id into data array like below
Controller
$id = $this->input->post('id');
$data['id']=$id;
//print_r($data); IT WORKS TILL HERE
$this->load->view('admin/viewAccount', $data);

You have not echo $id in your javascript this should be like echo $id:
Update here :
// HERE IS WHERE I GET A NULL VARIABLE
var data='<?php echo $id; ?>';
$(document).ready(function() {
alert(data);
});

thanks for the fast responses, noone of the answers above work.
I managed to find a solution , but its completely different method i just put id as a global variable.
But i would be still interested in the answer of this, i assume it will help other people to

Related

How to display data from MYSQL in codeignitor?

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

passing variables in form_open() in codeigniter

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

Can't storing data in database received from ajax call in laravel 4

Route::get('/comment/{data}', function($data){
if (Request::ajax()) {
//echo $data; *i checked here echo prints the content of $data but i cant store it in database. i dont know why its not store by using below code plz help me to find error.
$comm = new Data;
$comm->details = Input::get('data');
$comm->save();
}
});
Route::post('/comment/{data}', function($data){
}

CodeIgniter URL issue

I am having difficulty getting the correct URL when I call a method to load a view.
Heres my controller:
public function post() {
$title = $this->input->post('title');
$data = $this->p_Model->post($title);
$this->qs($data);
}
public function qs($id){
$title = $this->s_Model->getTitle($id);
$result = $title->result();
$this->load->view('q_View', array('results' => $result));
}
Heres my view:(note this view is not the view which gets loaded from the qs function, but one which calls the qs function)
<html>
<body>
<table>
<?php
if (isset($qs)) {
foreach ($qs as $row) {
$id = $row->qID;
echo '<a href="'.site_url('myController/qs/'.$id).'">';
echo $row->title;
echo "<br>";
}
}
?>
</table>
</body>
</html>
So in my controller I have two functions, the qs function works separately by itself so can be called in the view and give the following url myController/qs/1 however when I use the post function I get a url like this myController/post so my question is how can I get my url to be like the first example?
Instead of using the line:
$this->qs($data);
You can use a redirect:
redirect('/mainController/qs/'.$data);
That should work in the same way that you have used in your view
Try base_url and also you can use current_url() returns the full URL (including segments) of the page being currently viewed.
echo '<a href="'.base_url('myController/qs/'.$id).'">';

AJAX AND JSON within Codeigniter

I have a website where I am wanting to get some query results through AJAX and JSON, but I have absolutely no idea how to do it, I have this function,
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
$data['content'] = $this->site_model->get_content($table, $content_id);
$this->load->view('template/right-content', $data);
}
Essentially the query that is run is dynamic depending on what url is being passed, what I need to is, for the user clicks a link something like
Read the blog
From this link I get blog and 1 passed to the query, but I need to load the results in a view that is loaded in to my main template and then everytime a link is clicked do the same thing without overwriting the previous data, does anyone have any idea how to do this?
If I understand you correctly, you need to send an Ajax request to that url and then append it to the appropriate place in your document. Something like this:
$("#blog").click(function () {
var url = $(this).attr("href");
$.ajax ({
url: url,
type: "POST",
success : function (html) {
$("#someDiv").append(html);
}
});
});
So that Codeigniter view should only contain the content, really, and perhaps some markup necessary to style it. The containers where the content goes should already be in the page where the link is originating.
Or, if you want actually want your data to come back as JSON you could do something like this
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
echo json_encode($this->site_model->get_content($table, $content_id));
}
AND, if you use the above authors method of using $.append, you'd want to modify your controller as such:
public function category() {
$table = $this->uri->segment(2);
$content_id = $this->uri->segment(3);
$data['content'] = $this->site_model->get_content($table, $content_id);
echo $this->load->view('template/right-content', $data, TRUE);
}

Categories