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).'">';
Related
Controller has index() method and display() method.
Method index displays the image. and this method called display() doesn't display the images.
Both methods are in same controller.
Both methods are calling the same view file called
$this->load->view('home/portfolio2');
.
index() method is displaying the image but display() method not displaying the image.
index method code is
public function index() { //$this->load->view('images/homeimage'); //home image.
$this->load->view('home/portfolio2');
$this->load->view('home/header');
$this->load->view('home/viewcategory');
$result['msg'] = $this->W_model->latestupdates();
$result['title'] = 'latest updated';
if($result['msg'] == NULL) {
echo "check for albums in DB";
}else{
$this->load->view('home/latestsongsupdated', $result);
}
$this->load->view('home/footer');
}
display method code is
Public function displayalbums($albums, $lang){
$result['title'] = $lang;
$result['msg'] = $this->W_model->displayalbum($albums);
if($result['msg'] == NULL) { ?>
<h3 style = "margin-left: 20px;">
<?php echo "Songs will be updated soon, Please check for other songs"; ?>
</h3> <?php
} else{
$this->load->view('home/portfolio2');
$this->load->view('home/viewcategory');
$this->load->view('home/albums', $result);
}
}
Any advise, help will be more appreciated.
The correct syntax for image src is:
<?php echo base_url('assets/images/glow_in_worship_by_riyovincent-d571aon.jpg'); ?>
If that does not work echo base_url() somewhere. It should point to localhost/worship if your entire CI is in an htdocs or HTML subfolder that's called workshop. If it doesn't then you should edit your base_url correctly in the config as http://localhost/workship.
You can also use the image helper to generate the image tag (load HTML helper):
<?php echo img('assets/images/glow_in_worship_by_riyovincent-d571aon.jpg'); ?>
Note: this method also requires the base_url to be set correctly.
#raviraj123456
Use the full URL like this: 'http://www.example.com/assets/images/…; in src of the img tag and it will work.
Here is my controller:
class CommonController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('common_model'); //load your model my model is "common model"
}
public function add_work(){
$names = $_POST['name'];
$works = $_POST['work'];
$allValues = array(); // array to contains inserted rows
foreach($names as $key => $name){
$name= "your specified name";
$insertdata = array();
$insertdata['work'] = $works[$key];
$insertdata['name'] = $name;
$this->common_model->insert($insertdata);
array_push($allValues,$insertdata);
//$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
}
foreach($allValues as $insertRow){
echo $insertRow['work'];
echo $insertRow['name'];//this shows data well. but how to pass data in view.php
}
//view code will add here to show data in browser
}
Basically I want to pass all data to view.php for printing or exporting purpose. How can I do so.
To load a view you should do like this.
$this->load->view("filename");
If you want to pass data to view, you should do like this.
$this->load->view("filename",$data);
$data should have all parameters which you want to print in view.
The syntax goes like this.
$this->load->view("filename","data to view","Returning views as data(true / false");
If third parameter is true, view will come as data. It will not go to browser as output.
Edit:
Change
$this->load->view('print_view',$insertdata);
to
$data['insertdata'] = $insertdata;
$this->load->view('print_view',$data);
For more info, check this link
How CI Classes Pass Information and Control to Each Other
Calling Views
We will see.how the controller calls a view and passes data to it:
First it creates an array of data ($data) to pass to the view; then it loads and calls the view in the same expression:
$this->load->view('testview', $data);
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a
model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
$foo = $this->mymodel->myfunction();
and the function is set to return a value, then that value will be passed to the variable $foo inside the controller.
//sample
public function display()
{
$data['text_to_display'] = $this->text_to_display;
$data['text_color'] = $this->text_color;
$this->load->view('display_view',$data);
}
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view
loading method. Here is an example using an array:
$data = array(
’title’ => ’some’,
’heading’ => ’another some’,
’message’ => ’and another some’
);
$this->load->view(’view’, $data);
And here’s an example using an object:
$data = new Someclass();
$this->load->view(’view’, $data);
Sending Multiple Dimensional array
if we pull data from your database it will typically be
in the form of a multi-dimensional array.
<?php
class foo extends CI_Controller {
public function index()
{
$data[’Books’] = array(’POEAA’, ’TDD’, ’Clean C’);
$data[’title’] = "Title";
$data[’heading’] = "Heading";
$this->load->view(’view’, $data);
}
}
in view
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
<h3>My Books List</h3>
<ul>
<?php foreach ($Books as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>
More Learning
NOTE:
There is a third optional parameter lets you change the behavior of the method so that it returns data as a string rather
than sending it to your browser.The default behavior is false, which sends it to your browser. Remember to
assign it to a variable if you want the data returned:
$string = $this->load->view(’view’, ’’, TRUE);
Above will not solve your problem directly but definetly help in understanding concepts.
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
please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I have a problem with displaying a view. When I pass var to view, view doesn't render.
Controller:
public function indexAction()
{
$branchModel = new Application_Model_Branches();
$branches = $branchModel->getAllBranches();
$this->view->menu = $branches;
}
View (index.phtml):
<h2>Menu</h2>
<?php
$this->htmlList($this->menu);
?>
When I try debug $branches without assign it to view, all seems to be ok, but when I try push it to view,index.phtml don't appear.
Regards
You're just missing an echo in your code, the htmlList view helper returns a value - it doesn't echo it. Some examples of the various form view helpers can be seen here
<h2>Menu</h2>
<?php
echo $this->htmlList($this->menu);
?>
controller
$this->view->variableName = "Hello World!";//assign here
$this->view->assign('variableName1', "Hello new World!");//assign here
view
echo $this->variableName;//echo here
echo $this->variableName1;//echo here