I have function in controller with one argument and now I want to pass value for it from view through url.
courses.php
public function index($id){
print_r();
exit;
}
coursesview.php
<a class="jscroll-next" href="<?php echo base_url(); ?>/courses/index?page=<?php echo $nextPage; ?>&id=<?php echo $parentId ?>">next page</a>
How it possible?
From the user guide,
Simply put the parameter in an array and pass it to the view by using the second parameter of load->view().
Afterwards, in the view, the variable will be available as the key.
In this example: $id or $anothervar.
courses.php
public function index($id){
$data = array('id' => $id, 'anothervar' => 'yeahi');
$this->load->view('coursesview', $data);
// exit;
}
coursesview.php
echo $id; // the ID
echo $anothervar; // "yeahi"
You are going to use minimum arguments in URL, here is my suggestions
1.<a class="jscroll-next" href="<?php echo base_url('courses/$nextPage/$parentId/'); ?>">next page</a> //controller followed by arguments - if you call index method
(OR)
2.<a class="jscroll-next" href="<?php echo base_url('courses/methodname/$nextPage/$parentId/'); ?>">next page</a> //controller,method name and followed by arguments
How to get those arguments in controller
Example:
$nextPage = $this->uri->segment(3);
$parentId = $this->uri->segment(4);
Related
I am working on a project where I need to pass a variable from the View to a Controller's method where I'll be using that variable's value. I have tried the following.
View
...
$user = 3;
...
<ul class="nav navbar-nav navbar-right">
<li>
<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=$user">
My Dashboard
</a>
</li>
...
studentDashboardController (Method 1)
public function index()
{
...
if ( isset($_GET['user']) ) {
$user = $_GET['user'];
echo '<script type="text/javascript">alert("User taken from GET: ' . $user . '")</script>';
}
...
Output for Method 1
studentDashboardController (Method 2)
public function index()
{
...
if($this->input->get())
{
$user = $this->input->get('user');
echo '<script type="text/javascript">alert("Uid taken from Method 2 ' . $user . '")</script>';
}
...
Output for Method 2
Any suggestions on how to get the value of this passed variable will be highly appreciated.
You were missing the PHP notation while printing the $user variable. Update the below line in the View
<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=<?php echo $user; ?>">
I have this todo list project.
My model
class Model_lists extends CI_Model {
function Model_list(){
parent::__construct();
}
function list_get($id){
$this->load->database();
$query = $this->db->get_where('lists', array('id'=>$id));
return $query->row_array();
}
My Controller
public function view_list($id){
$this->load->model('model_lists');
$data["query"] = $this->model_lists->list_get($id);
$this->load->view('lists/view_list',$data);
}
List view
<?php
echo $query['list_by'];
?>
However, when I access the view I get 2 PHP errors
1) Message: Missing argument 1 for Lists::view_list()
2) Message: Undefined variable: id
This is how I call the list:
<a class="view_list" href="<?php echo site_url("lists/view_list?lid={$row->list_id}");?>"><i class="icon-eye"> </i></a>
Your site url is not correct. The error is because no id is being passed and in CI (unless you have set to use query arrays) then the url is of the format www.example.com/controller/method/argument
So try:
href="<?php echo site_url('lists/view_list/'.$row->list_id); ?>"
In the docs:
http://www.codeigniter.com/user_guide/helpers/url_helper.html#site_url
I like to preface this question by apologizing for being noob.
For codeigniter URL, format goes:
example.com/class/function/ID
If I were to have the urls below
website.com/books/chapter/1
website.com/books/chapter/2
I know how to create class named "books" under which I would create public function "chapter", so...
public funtion chapter() {
$this->load->view("content");
}
How would I add the ID's 1 and 2, assuming the only difference between the two website is I would have:
1.png within my "content" for website.com/books/chapter/1
and
2.png within my "content" for website.com/books/chapter/2, with 2.png replacing where 1.png was supposed to be.
Thanks!
Since you are using this URL: website.com/books/chapter/2, you are already passing the id after the last slash. Therefore your controller method should receive that ID as a parameter like this:
Controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
Controller:
public function chapter() {
$data["image_id"] = $this->uri->segment(3); //since your ID(number) is in third segment so 3
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
for more info on URI class
https://ellislab.com/codeigniter/user-guide/libraries/uri.html
The ids are just the method parameters. You can get it by passing the parameter in your method.
In the controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("your_view", $data);
}
In your view:
<img src="<?php echo $image_id;?>.png"/>
I want to make one delete function, but I do not know what steps did wrong?
This is the file model:
function delete()
{
$this->db->delete('ns_categories', array('cat_id' => $cat_id));
}
Controller:
function delete()
{
$this->cat_model->delete();
$cat_id=$this->uri->segment(3);
if($cat_id->delete()) return json_encode(array("success" => true));
}
View:
<td class="centeralign"><a class="deleterow" href="<?php echo anchor('admin/categories/delete' .<?php echo $row['cat_id']; ?>"><span class="icon-trash"></span></a></td>
Please help me.
You haven't done anything right, no offense. It's a mess.
View:
You missed a forward slash here:
href="<?php echo 'admin/categories/delete' .<?php echo $row['cat_id']; ?>"
^^^
...but you don't use anchor() this way anyways, it generates an entire link. And you have nested two <?php echos... What a mess. Do it like this:
<a href="<?php echo base_url('admin/categories/delete/'.$row['cat_id']; ?>">
Model:
You haven't defined $cat_id in the model's delete function. Also, don't return json from a model. Do it like this:
function delete($cat_id = null)
{
return $this->db->delete('ns_categories', array('cat_id' => $cat_id));
}
Controller:
You're calling delete as a method of $cat_id which makes no sense, as the variable contains a number - not an object you can call a method on. You also have to echo/print the json, and you should set json headers. Do it like this:
function delete($cat_id = null) {
$status = $this->cat_model->delete($cat_id);
header('Content-type: application/json');
echo json_encode(array("success" => $status));
}
You should use POST method to delete things, otherwise people can delete things accidentally or do stupid tricks like <img src="delete/item/1">
Remove extra echo from link:
<td class="centeralign">
<a class="deleterow" href="<?php echo anchor('admin/categories/delete' . $row['cat_id']); ?>">
<span class="icon-trash"></span>
</a>
</td>
In your Controller delete function you have got the category id after calling the delete function of model why? And in your model function from where you are getting $cat_id ?? it should be like this
function Category_delete(){
$cat_id=$this->uri->segment(3);
if($this->cat_model->delete($cat_id)) return json_encode(array("success" => true));
}
In your view you have opened the <?php again and also you are using anchor function in the href anchor function generates the complete <a> tag see reference for helpers CI Helpers
<td class="centeralign"><a class="deleterow" href="<?php echo site_url('admin/categories/Category_delete/'.$row['cat_id']; ?>">
<span class="icon-trash"></span></a></td>
And you should rename the functions because delete may be a keyword so use ordinary names for the functions
im creating a function to delete a contact record, then once deleted go to the view company page.
so here is my controller
public function delete($id) {
if (isset($_GET["delete"]))
{
$this->Contacts_model->delete($id);
$url = "/company/view/" . $cid;
redirect($url);
}
}
and here is my model
public function delete($id)
{
$this->db->where('id', $id);
$this->db->delete('contacts');
}
on my view:
<a href="/contacts/delete/<?php echo $data['id']; ?>
which this works and will delete the contact, but obviously wont redirect to the company/view page becuase the CID isnt being passed.
i thought about adding this to the delete link
<a href="/contacts/delete/<?php echo $data['id']; ?>?delete&cid=<?php echo $data['cid']; ?>"
so that cid is being passed through the url. would this work some way?
You can pass the cid in the url like
delete
In your controller function get the second parameter as $cid or from uri segment
public function delete($id,$cid) {
if (isset($_GET["delete"]))
{
$this->Contacts_model->delete($id);
$url = "/company/view/" . $cid;
redirect($url);
}
}
Here is uri segment documentation