image not displaying in Codeigniter - php

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.

Related

URI not working and just adding to URL

I'm trying to get my head around custom URIs. I would like my view_county to have the URL:
http://localhost/chipadvisor2/controller_ca/all_county/1
and my view_all to have the URL:
http://localhost/chipadvisor2/controller_ca/all_chippers
If anyone can help me I would greatly appreciate it. The link works for view_county but when I click on the view_all link it shows:
{
"status": false,
"error": "Unknown method"
}
and if I click into view_county first then the view_all link it just keeps adding controller_ca/all_chippers to the URL and doing nothing. Any ideas why?
model_ca
<?php
class Model_ca extends CI_Model {
function __construct() {
parent::__construct();
}
function get_chipper() {
$query = $this->db->get('chipper_reviews');
return $query->result();
}
function get_county($id) {
$this->db->where('location', 'Westmeath');
$query = $this->db->get('chipper_reviews');
return $query->result();
}
}
?>
controller_ca
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Controller_ca extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('model_ca');
}
public function index_get() {
$this->all_chipper_get();
}
public function all_chipper_get() {
$data['chipper'] = $this->model_ca->get_chipper();
$this->load->view('view_all', $data);
}
public function all_county_get() {
$id = $this->uri->rsegment(3);
$data['location'] = $this->model_ca->get_county('location',$id);
$this->load->view('view_county', $data);
}
}
?>
view_county
and
view_all
<html>
<head> </head>
<body>
<div class="container">
View All Chippers
<br/>
View County
<?php echo "<br/><br/>";
foreach ($chipper as $chipperdata) {
echo "<b>Name: </b>".$chipperdata->name." "."<br/>"."<b>County: </b>".$chipperdata->location." "."<br/>"."<b>Description: </b>".$chipperdata->description." "."<br/>"."<br/>";
} ?>
</div>
</body>
</html>
I have added this to the route.php file also but don't know how to put it into effect or how to add the value 'westmeath' to the end. Ideally I would like to pass it as a variable rather than hardcoding it:
$route['default_controller'] = 'Controller_ca';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['all_chipper'] = 'controller_ca/all_chippers';
$route['all_county'] = 'controller_ca/all_county/$1';
Try to give controller method name same as url. Change all_chipper_get() to all_chipper(). Same for other method and check.
Regarding the issue:
and if I click into view_county first then the view_all link it just
keeps adding controller_ca/all_chippers to the URL and doing nothing.
Any ideas why?
Please add base_url() at the starting of URL in anchor tag. It will resolve the append issue in URL. Example:
View All Chippers
<br/>
View County
Also, change your routing as:
$route['controller_ca/all_county'] = 'controller_ca/all_county/$1';
$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';

Is it possible to render a view to a rendered page from the controller using Yii?

It there any way to create a "placeholder" inside the main view file for rendering other view files with specified data using Yii?
I want to individually process data in the controller then place them to a specified location in the view file before rendering.
Here is a widget example:
The widget class:
class MyWidget extends CWidget
{
public $someData;
public $mainData;
public function init()
{
}
public function run()
{
$this->render('mainView',array('data'=>$someData));
foreach($data as $dat)
{
if(dat["color"]=="red")
{
$display = 4;
}
else if(dat["color"]=="blue")
{
$display = 6;
}
etc....
//this is the fictional method for that purpose
$this->addToPage('mainView','subView','placeholderName',
array('display'=>$display,'mainData'=>$main));
}
}
}
The mainView file:
echo("<div class='someDesign'>");
echo($data);
$this->placeholder('placeholderName');
echo("</div>");
The subView file:
if($display>0 && $display<=4)
echo("<div class='dataColorG'>");
else if($display>0 && $display<=4)
echo("<div class='dataColorD'>");
echo $mainData;
echo("</div'>");
The solution based on Nikola's answer:
The widget class:
class MyWidget extends CWidget
{
public $someData;
public $mainData;
public function init()
{
}
public function run()
{
$output ="";
foreach($data as $dat)
{
if(dat["color"]=="red")
{
$display = 4;
}
else if(dat["color"]=="blue")
{
$display = 6;
}
//If it's a widget we need to use $this->controller->renderPartial() instead of $this->renderPartial()
$output.= $this->controller->renderPartial('subView',array('display'=>$display,'mainData'=>$main),true);
}
$this->render('mainView',array('subView'=>$output,'data'=>$someData));
}
}
The mainView file:
echo("<div class='someDesign'>");
echo($data);
echo($subView); //the 'placeholder'
echo("</div'>");
The subView file:
if($display>0 && $display<=4)
echo("<div class='dataColorG'>");
else if($display>0 && $display<=4)
echo("<div class='dataColorD'>");
echo $mainData;
echo("</div'>");
You can use renderPartial for this purpose. You can place the code for renderPartial isntead $this->placeholder('placeholderName'); e.g.:
$this->renderPartial('placeholderView', array($data));
Chech the other params - you can save to string or process js/css from the partials.
You said "previously rendered file" which I interpret that you have the main file already in view in the browser, and then you would like to have new data processed by the controller and passed back the view, for which you would need an ajax call from the main view file somehow (maybe through an ajax button). If so have a look at CHtml::ajaxButton.
This allows you to call a controller action and push the resulting view to a DOM placeholder (using the HTML id). The replace key in ajaxOptions of CHtml::ajaxButton would replace the contents of the DOM placeholder. e.g.
'replace' => '#placeholder_id'
Edit
replace will actually replace the whole DOM element. If you want to replace the html content inside the DOM element you can use update key in ajaxOptions
<div id="subviewPlaceholder_id">html content</div>
Here is an example, which resides inside a form. The button push will send form data as get params. Use your controller action to read the params and $this->renderPartial('subView',array(... params ...)) to send back your sub view:
<?php
echo '<div id="subviewPlaceholder_id"></div>';
echo CHtml::ajaxButton('Get Sub View Button Name',array(
'controller/getSubView','param1'=>$presetParam),
array( // this is the ajaxOptions
'type'=>'GET',
'update'=>'#subviewPlaceholder_id', // id of DOM element for subview
), array( // this is the htmlOptions
'class'=>'normalButton',
));
?>
Added
If you would like to place a sub view inside another view at initial render. Use #Nikola's suggestion
$this->renderPartial('subView',array(...params...));
Example main view file (followed by subview file). Place both files in your view folder:
<?php // mainView.php
echo '<h1>MAIN VIEW</h1>';
echo '<div class="class">'.$mainData->attribute1.'</div>';
echo '<div class="class">'.$mainData->attribute2.'</div>';
// render anything else
// parameters for subView (processing should be done in controller actually)
$display = $condition==$criteria?1:2;
//this is where subview.php would be placed
//You could get '_subView' from a variable and switch your subView according
//to your criteria.
$this->renderPartial('_subView', array(
'display'=>$display,
'param2'=>$param2));
echo '<div>MAIN VIEW CONTINUED</div>'; // add any other rendering for main view
?>
And here is your sub view (prefix "_" for partial views by convention)
<?php // _subView.php
if($display>0 && $display<=4)
echo("<div class='dataColorG'>");
else if($display>0 && $display<=4)
echo("<div class='dataColorD'>");
echo $param2; // add anything else to be rendered.
echo "</div>";
?>

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).'">';

Function Help -> Organization

I am trying to get the following function to run when the link is clicked and then load the previous page, but I seem to end up with a blank page with no php errors:
View:
<?php if(is_array($get_images)): ?>
<?php foreach($get_images as $image): ?>
<img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image['thumbname']?>" alt="<?= $image['description']?>"> Delete
<?php endforeach; ?>
<?php endif; ?>
Controller:
function delete($id) {
$id = $this->uri->segment(3);
$this->image_model->deleteImage($id);
$page['get_images'] = $this->image_model->getImages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Delete Gallery Image';
$data['content'] = $this->load->view('admin/deleteimage',$page,TRUE);
}
}
Firstly, a destructive thing such as deleting an image should be POST, not GET.
As for the code, something like this should work (assuming that last closing brace means your function is a class method)...
// However you extract params in your framework.
$id = $request->getParam('id');
// Instantiate the class.
$controller = new ImageController;
// Call the method.
$controller->delete($id);

Zend Framework, passing variables to view

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

Categories