Function Help -> Organization - php

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);

Related

image not displaying in Codeigniter

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.

Distribute JSON data into different HTML elements with PHP

I am parsing a JSON Object and using a foreach loop to output the data.
function do_api_call() {
$place_id = get_theme_mod('place_id_setting_field');
$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" . $place_id . "&key=myapikey";
$data = file_get_contents($url);
$rev = json_decode($data, true);
$reviews = $rev["result"]["reviews"];
foreach($reviews as $review) {
$review_snippet = $review["text"];
echo $review_snippet . '<br>';
}
}
This works fine when I call it within an HTML element with:
<?php echo do_api_call() ?>
The short of it is that I get back 5 reviews from this loop and I need each review to go to their own html element in a different file called reviews.php, this file contains 5 unique bootstrap cards with a div that needs to hold a unique review so I need to output a unique review into each of these cards.
Like so:
<div> review 1 text </div>
<div> review 2 text </div>
<div> review 3 text </div>
<div> review 4 text </div>
<div> review 5 text </div>
You access a direct review with $rev["result"]["reviews"][0] (for the first) $rev["result"]["reviews"][1] (for the second) etc. So you can pass which review as a function arg.
However to cut down on re-loading an external source with every call of the function, you may want to do the data loader outside the function:
$place_id = get_theme_mod('place_id_setting_field');
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.
$place_id .'&key=myapikey';
$data = file_get_contents($url);
$rev = json_decode($data,true);
$reviews = $rev['result']['reviews'];// this is now setup and ready to use
And then setup the anonymous function using the global (php 5.3+):
$get_review = function ($r) use (&$reviews) {
if (isset($reviews[$r])) {
return '<div>'. $reviews[$r]['text'] .'<div>';
}
return '';// no review to return
};
Then down in your html where you want to begin outputting them, you call it as such (note the $ is intentional with anonymous functions assigned to variables):
<body>
blah blah other stuff
<?php echo $get_review(0);?>
more blah
<?php echo $get_review(1);?>
</body>
Or if you need to loop on how many reviews you have:
<body>
<?php for($r=0;$r < count($reviews);$r++) { echo $get_review($r); } ?>
</body>
If you are afraid of using anonymous functions as I have above, you can adjust it to this instead:
function get_review ($r,&$reviews) {
if (isset($reviews[$r])) {
return '<div>'. $reviews[$r]['text'] .'<div>';
}
return '';// no review to return
}
// call it as thus
echo get_review(0,$reviews);
echo get_review(1,$reviews);
// etc
Class Method:
Of course you COULD also turn this into a small class object, where you first load_api, then get_review as methods of the class:
class Reviews {
public static $reviews;
public static function load_api() {
$place_id = get_theme_mod('place_id_setting_field');
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.
$place_id .'&key=myapikey';
$data = file_get_contents($url);
$rev = json_decode($data,true);
self::$reviews = $rev['result']['reviews'];// this is now setup and ready to use
}
public static function get_review($r) {
if (isset(self::$reviews[$r])) {
return '<div>'. self::$reviews[$r]['text'] .'<div>';
}
return '';// no review to return
}
}
// to initialize
Reviews::load_api();
// to call and output
echo Reviews::get_review(0);

Display an array data from two tables in codeigniter

I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}

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

Zend fetchAll foreach 2 times run dont loop 2nd time;

This is very simple to understand
Image Class
<?php
class Image extends Zend_Db_Table_Abstract {
protected $_name = 'images';
public function getList() {
return $this->fetchAll();
}
}?>
My PHP Code
<?php
require 'config.php';
$imgTable = new Image(); // Create Object
$imgList = $imgTable->getList(); // fetch Data
$template = new Template('portfolio'); // Initialize Template and tell which template to pick
$template->imgList = $imgList; // set template variable
$template->render(); // Generate Template output
?>
I can access template variable inside template using $this
Below code is from inside the template
$xback = 0;
foreach ($this->imgList as $images) {
echo 'imageArray[' . $xback . '] = "' . $images['sef'] . '";';
$xback++;
}
?>
.......
<?php
foreach ($this->imgList as $images) {
?>
<div class="portfolio_item">
<img src="<?php echo PATH_WEB . $images['image_thumb'] ?>" height="146" width="209" />
<div class="title"><?php echo $images['image_title'] ?></div>
<div class="right link">
View Details
</div>
</div>
<?php
}
?>
Above code is working fine, but below some few lines, I have to iterate over the same data again dont output any thing. If I comment the first one, 2nd starts working.
First one is to create the JS array and is in head section,
Second part is in HTML to display images
I hope its a pointer issue, I may have to set the loop current item to start, but I am not understanding it right now .... reset($this->imgList) didnt worked
please help
I think it has something to do with the fetchAll call, try this:
<?php
class Image extends Zend_Db_Table_Abstract {
protected $_name = 'images';
protected $images;
public function getList() {
// Employ lazy loading pattern to load images if they aren't set yet
if (!isset($this->$images)) {
$this->images = $this->fetchAll();
}
return $this->images;
}
}?>

Categories