My welcome controller coded like this,
function index()
{
// some code here to set up my table
echo $this->table->generate();
$data['heading'] = "My Real Heading";
$this->load->view('view_page', $data);
}
When I open http://localhost/ci/index.php/welcome/
I found the table generated successfully. but how can I place my table showed under my Heading?
My confused that, the table class generated by module function. It will finished before the view page load. That's why the issue occurred.
[Updated].
I found echo $this->table->generate(); could be place into my view page. It works now.
function index()
{
// some code here to set up my table
$data['myTable'] = $this->table->generate();
//assign result to a variable in the $data array
$data['heading'] = "My Real Heading";
$this->load->view('view_page', $data);
}
in your view:
<!-- html -->
echo $heading;
echo $myTable;
Related
What I am trying to do is that, whenever the user clicks on a specific assignment, they will redirect to the detail page. The detail page will have different information according to what assignment they clicked on. However, I have no idea how to implement this feature.
My home function in Controller/project is as follows
public function home() {
/*if($this->input->cookie('login_status')=='0'){
header("location: Welcome/login");
} else{
*/
$this->load->database();
$this->load->model('scheduler_model');
$this->load->view('header.php');
$data['username'] = $this->input->cookie('username');
$data["getnames"]=$this->scheduler_model->get_users();
$this->load->view('home/homepage',$data);
$data["getassignments"]=$this->scheduler_model->get_assignment();
$data["getselectedcourses"]=$this->scheduler_model->get_select_course();
$this->load->view('home/assignments',$data);
//}
}
My assignment.php in view is as follows:
foreach($getassignments as $getassignment){
if(in_array($getassignment->courseID,$courseArray)){
if($getassignment->due>date("Y-m-d")){
?><a href="detail">
<?php
$assignName='assignment'.$i;
echo $getassignment->name," ",$getassignment->due;
//$this->input->set_cookie('assignName',$getassignment->name,time()+3600);
$assignList =array(
$assignName=>$this->input->set_cookie($assignName,$getassignment->name,time()+3600)
);
?></a>
<?php
echo "<br>";
$i=$i+1;
}
}
}?>
What I am trying to do is that, whenever the user clicks on a specific
assignment, they will redirect to the detail page. The detail page
will have different information according to what assignment they
clicked on. However, I have no idea how to implement this feature. My
home function in Controller/project is as follows
This is how it works!
You will have a method in a controller that list all assignments for your example
function home() {
$data["getassignments"]=$this->scheduler_model->get_assignment();
$this->load->view('home/homepage',$data);
}
You should have a view to listing all those assignments.
foreach($getassignments as $getassignment){
echo "<p><a href = 'controller/details/".$getassignment->id."'>".$getassignment->name."</p>"
}
you should have another method inside a controller to view that selected assignment
function detail($id){
$data["getassignment"]=$this->scheduler_model->get_assignment($id);
$this->load->view('details', $data);
}
Finally, you need a detail view to display the one you need for the selected assignment
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.
I have this model:
public function get_products_by_slug($slug)
{
$this->db->select('*');
$this->db->from('products');
$this->db->where('slug', $slug);
$query = $this->db->get();
return $query->result();
}
And this controller:
public function products($category)
{
$data['products_by_category'] = $this->products_model->get_products_by_slug($category);
if (empty($data['products_by_category']))
{
show_404();
}
$data['main_content'] = 'pages/products_by_category';
$this->load->view('templates/template', $data);
}
And in the view for the title of the page as a banner in the page I want to use both those function to grab just the first category field in the database to use it as the title. I know that there is way in the model that I can grab just the first row. But what I want is not make another model just for that I want to use those functions above to use it for the title and the rest of the page to show the all products.
the view page:
<?php foreach($products_by_category as $row){
echo '<div class="container">';
echo '<h1>'.$row->category(1).'</h1>';
echo '<hr/>';
echo '</div>'; } ?>
now this way above it will make as much with the same name of the category as there is in the database. what I want is only one name for the title.
After a lot of searching around the web the answer was so simple. You don't need the foreach loop just use the passed variable with the number of the row in the array and the name of the field. just like that.
<?php
echo '<div class="container">';
echo '<h1>'.$products_by_category[1]['category'].'</h1>';
echo '<hr/>';
echo '</div>';
?>
Codes:
View
<html>
<body>
<?php echo form_open('samplecontroller/sample'); ?>
<input type="text" name="samplename"/>
<input type="submit" value="go"/>
<?php echo form_close(); ?>
</body>
</html>
Controller
<?php
Class samplecontroller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Sample_insert');
}
function index()
{
$this->load->view('sample.php');
}
function show($data)
{
$this->load->view('sampleresult',$data);
}
function sample()
{
if($result = $this->Sample_insert->insert_into_sample())
{
$this->show($result);
}
}
}
?>
Model
<?php
Class Sample_insert extends CI_Model
{
function insert_into_sample()
{
$sample = array(
'samplename' => $this->input->post('samplename'),
);
$this->db->insert('sample', $sample);
$latest_id = $this->db->insert_id();
return $latest_id;
}
}
?>
The flow is that, i have 2 views. The other one contains only 1 text input, after which goes to controller then controller passes it on to model then model gets the value within the text input then inserts to my sample table.
The sample table has 2 columns, id(PK,AI) and name
after it inserts, on my model. i added a line, return $latest_id. Then passes goes back to my controller then passes it off to another view that only has
print_r($data);
After all that, it displays an error.
Message: Undefined variable: data
Im not sure where the flow went wrong or if i made a syntax mistake. If someone knows/expert on insert_id(), could you pin point where exactly i am wrong? anyways, ive made my research and been getting results on the web how buggy insert_id is. Im not sure if its true or not but ive been redirected mostly to forums wherein insert_id returns null and some say its a bug. Im hoping it isnt.
As per your comment, You have to do some changes in your controller.
function show($data)
{
$this->load->view('sampleresult',array("data"=>$data));
}
You can get inserted id in view in the name of $data.
For ex:
echo $data;
Edit: As per your comment in answer, For multiple row insert in model, add below code
$id_arr = array();
foreach($sample_data as $sample)
{
$this->db->insert('sample', $sample);
$id_arr[] = $this->db->insert_id();
}
return $id_arr;
Now in your view, you will get ids in $data
foreach($data as $id)
{
echo $id;
}
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