My View :-
<html>
<?= link_tag(base_url().'css/simple.css'); ?>
<body>
<?php $this->load->helper('form'); ?>
<?php $this->load->view('commentform'); ?>
<?php $id=$this->uri->segment(3);?>
<?php echo $id;?>
</body>
</html>
i would like to use the variable $id in my controller.I'm using codeigniter by the way, and am a beginner. I would appreciate any help on this.
you should not call the $id from the View, you should get it at the controller level and pass it to the View.
as Bulk said. you URL will be something like that
www.mysite.com/thecontrollername/thefunction/id
for example your controller if home and there is a show_id function in it and your view is call show_id_view.php.
you will have your url like this: www.mysite.com/home/show_id/id
your function in home will read the id"
in your home controller:
function show_id(){
$id=$this->uri->segment(3);
$view_data['id'] = $id;
$this->load->view('show_id_view',$view_data);
}
in the view (show_id_view):
<?php echo $id ?>
nothing else..
hope this helps.
Well, ideally you wouldn't do it this way. You should assign the variable first in the controller and pass it to the view if you need to use it there.
$data['id'] = $this->uri->segment(3);
$this->load->view('view_file', $data);
$id would then be available in your view as well.
in my view i add link
<li><a href="<?php echo base_url()?>link/show_id/mantap"> coba </li>
in my link.php controler i add function show_id
function show_id(){
$id=$this->uri->segment(3);
$data['coba'] = $id;
$this->mobile->view('**daftarmember_form**',$data);
in my next view daftarmember_form.html
)
<?php echo $id;?>
they print mantap,
Related
using form post method i need to pass a value of numIDto a controller
for example assigning a value to numID inside view,
$numID= 25;
i need to use value of numID in a controller and assign that to temp variable. so i use following line of code,
$temp= $_POST[numID];
but its unable to get the exact value. Please help me on this. If have any other alternate way please let me know.
Here is an example of sending info from view to the controller:
View:
<?php echo form_open('invoice'); ?>
<?php echo validation_errors(); ?>
<?php echo form_input('order_num', $this->input->post('order_num')); ?>
<?php echo form_submit('submit','submit'); ?>
<?php echo form_close(); ?>
Controller:
public function invoice()
{
$order = $this->input->post('order_num');
$this->General_Model->get_customer_order($order);
}
Model:
function get_customer_order($order)
{
. . .
$this->db->where('client_orders.id', $order);
$result = $this->db->get('client_orders');
. . .
}
Note we are using the input name and id as = "order_num"
Then we ask the controller to find it as $this->input->post('order_num');
I'm trying to make dropDownList in _form view.
This is the Controller part
public function actionCreate()
{
$modelCountry = Country::model()->findAll();
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,
));
}
Now in _form view I have a TextField which must be modified to dropDownList
<div class="row">
<?php echo $form->labelEx($model,'name_en'); ?>
<?php echo $form->textField($model,'name_en',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name_en'); ?>
</div>
That means I must modify it to something like this
<?php echo $form->dropDownList($model,'name_en',array('!Variable!A!From_modelCountry!','!Variable!B!From_modelCountry!' )); ?>
I can use foreach loop and inside that loop create some DropDownList but maybe there is some better way to take data out of an array $modelCountry and put them straight to array of that dropDownList?
yes you can do that. In your controller you can write
$list=CHtml::listData($modelCountry,'id','name_en');
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,'list'=>$list
));
Now in your View you can write
<?php echo $form->dropDownList($model,'name_en',$list); ?>
You can use Chmlt::listData in your view but that should be a part of controller, thats what MVC is all about.
<?php echo $form->dropDownList($model,'name_en',CHtml::listData(Country::model()->findAll(),'id','name')); ?>
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).'">';
I have a model that returns a list of artist's names from a database along with their ID. I want to loop through these artists in my view and create links to their pages with the following format:
http://www.example.com/the-artist-name/artist-portfolio/ID.html
What is the best way to do this?
Controller
$data['artists'] = $this->artists_model->get_all(); // should return array
$this->load->view('yourview', $data);
View
<?php foreach($artists as $artist): ?>
<a href="http://example.com/<?php echo $artist['name']; ?>/artist-portfolio/<?php echo $artist['id']; ?>.html">
<?php echo $artist['name']; ?>
</a>
<?php endforeach; ?>
Pass the data from the model into the view and loop through it like you normally would.
In your controller:
$view_data['artists'] = $this->artist_model->get_artists();
$this->load->view('view.php', $view_data);
In your view:
foreach ($artists as $artist) {
echo "{$artist['name']}";
}
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