• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of Images

#1
I simply trying to show LIST OF IMAGES on my website:


<b>Latest Projects:</b><br><br>

<img src="<?php echo base_url();?>images/large pic.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic3.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic4.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic5.jpg" width="200px" height="165px">


views/cms/index.php

PHP Code:
<?php 
            $query 
$this->db->query("SELECT * from cms_galleries_detail where id_galleries = '$content_images_2' ");
                foreach($query->result()  as $rec){
                    ?>
                    <li>
                        <span class="zoom">
                            <img  src="http://www.purelineliving.com/public/thumb.php?src=<?php echo $rec->src;?>&w=800&h=800&zc=1">
                        </span>
                    </li>
           <?php
           
}
?>


A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$db
Filename: cms/index.php
Line Number: 121
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php
Line: 121
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\controllers\Welcome.php
Line: 24
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\index.php
Line: 315
Function: require_once

Fatal error: Call to a member function query() on null in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php on line 121
A PHP Error was encountered
Severity: Error
Message: Call to a member function query() on null
Filename: cms/index.php
Line Number: 121
Backtrace:



How to fix the above error?
" If I looks more intelligence please increase my reputation."
Reply

#2
(04-09-2016, 03:38 AM)davy_yg Wrote: I simply trying to show LIST OF IMAGES on my website:


<b>Latest Projects:</b><br><br>

<img src="<?php echo base_url();?>images/large pic.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic3.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic4.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic5.jpg" width="200px" height="165px">


views/cms/index.php

PHP Code:
<?php 
            $query 
$this->db->query("SELECT * from cms_galleries_detail where id_galleries = '$content_images_2' ");
                foreach($query->result()  as $rec){
                    ?>
                    <li>
                        <span class="zoom">
                            <img  src="http://www.purelineliving.com/public/thumb.php?src=<?php echo $rec->src;?>&w=800&h=800&zc=1">
                        </span>
                    </li>
           <?php
           
}
?>


A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$db
Filename: cms/index.php
Line Number: 121
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php
Line: 121
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\controllers\Welcome.php
Line: 24
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\index.php
Line: 315
Function: require_once

Fatal error: Call to a member function query() on null in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php on line 121
A PHP Error was encountered
Severity: Error
Message: Call to a member function query() on null
Filename: cms/index.php
Line Number: 121
Backtrace:



How to fix the above error?

Do you load database library in your Controller? ($this->load->database())
It seems you do not do that!
Reply

#3
It's not good practice to perform a database query inside a view.
The program's logic should be in your controller.

PHP Code:
//controller:
$data['images'] = $this->db->query(....)->result_array();
$this->load->view('cms/index',$data); 

PHP Code:
//view:
foreach ($images as $image) {
 
 //code to display the images

Reply

#4
I have revised the controllers and views:

This is the controllers :

controllers/Site.php

PHP Code:
 
    
public function __construct()
    {
        parent::__construct();
        // Your own constructor code
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('encrypt');
        $this->load->database();
    }
     





public function index($get_name="",$get_content="")
    {
        $get_name str_replace('-',' 'strtolower($get_name));
        $this->load->model('model_pages');
        $this->model_pages->website_base_url('site/index/');
    
        $this
->model_pages->get_id($get_name,$get_content);
        
        
        
// echo $this->model_pages->check_get_id();

        $data['metadata'] =  $this->model_pages->metadata();
        $data['benchmark'] =  $this->model_pages->benchmark($get_content,' / ');
        
        
// PAGES //
        $data ['navigation']= $this->model_pages->navigation();
        //$data ['navigation_mysql']= $this->model_pages->navigation_mysql();
        
        
// Global website variable;
        $data ['current']= $get_name;
        $data ['id_pages']= $this->model_pages->id_pages();
        $data ['pages']= $this->model_pages->pages($get_name);
        $data ['pages_name']= $this->model_pages->pages($get_name);
        $data ['subpages']= $this->model_pages->subpages($data['id_pages']);
        $data ['cms_pages_content_group']= $this->model_pages->cms_pages_content_group();
        
        $data 
['pages_images_1']= $this->model_pages->pages_images(1);
        $data ['pages_images_2']= $this->model_pages->pages_images(2);
        $data ['get_content']= $get_content;
        
        
        
// CONTENT //
        $data ['id_content']= $this->model_pages->id_content();
        $data ['content_name']= $this->model_pages->content_name();
        $data ['content']= $this->model_pages->content();
        $data ['content_images_1']= $this->model_pages->images(1);
        $data ['content_images_2']= $this->model_pages->images(2);
        $data ['content_images_3']= $this->model_pages->images(3);
        if( $this->model_pages->id_content() ){
           $data ['h3'] = $this->model_pages->select_db('h3','cms_content','id="'.$this->model_pages->id_content().'"');
        }
        
        $data 
['q']= ''// For seacrh
        $data ['cms_pages_content']= $this->model_pages->cms_pages_content();
        $data ['post']= $this->model_pages->post();
        
        
        
// BANNER //
        $data ['slideshow']= $this->model_pages->cms_slideshow(1);
        $data ['footer']= $this->model_pages->footer();
        $data ['embed_code']= $this->model_pages->embed_code();
        $data ['memory_usage']=$this->benchmark->memory_usage();
        
       
       
        $msg 
rand(1000,9999);
        $key 'batman-VS-Superman';
        $encrypted_string $this->encrypt->encode($msg$key);
        $newdata = array(
            'token'  => $encrypted_string,
            'rand'   => $msg,
        );
        $this->session->set_userdata($newdata);
                      
        
      
        $this
->template $this->model_pages->template();
      
        
if($this->model_pages->template()){
            $template $this->model_pages->template();
        }else{
           $template "default";
        }
        $data['msg'] = $msg;
        $data['session']  $this->encrypt->decode($this->session->userdata('token'), $key);
        $data['token'] = $this->session->userdata('token');
        
        $data
['template']=$this->template;
        
        
        $data
['images'] = $this->db->query("SELECT * from cms_galleries_detail where id_galleries = '$content_images_2' ")->result_array();
        $this->load->view('cms/index',$data); 
       
       
        $this
->load->view('header',$data);
        $this->load->view('cms/'.$template,$data);
        $this->load->view('footer',$data);
        
        
        $this
->db->close();
    


views/cms/index.php


PHP Code:
<b>Latest Projects:</b><br><br>

<
img src="<?php echo base_url();?>images/large pic.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic3.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic4.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic5.jpg" width="200px" height="165px">

<?
php 
            
            
              
foreach ($images as $image) {
                    ?>
                    <li>
                        <span class="zoom">
                            <img  src="http://www.purelineliving.com/public/thumb.php?src=<?php echo $rec->src;?>&w=800&h=800&zc=1">
                        </span>
                    </li>
           <?php
           
}
?>



This is the result:

Latest Projects:

[Image: large%20pic.jpg]  [Image: large%20pic3.jpg]  [Image: large%20pic4.jpg]  [Image: large%20pic5.jpg]
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: images
Filename: cms/index.php
Line Number: 123
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php
Line: 123
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\controllers\Welcome.php
Line: 24
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\index.php
Line: 315
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: cms/index.php
Line Number: 123
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php
Line: 123
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\controllers\Welcome.php
Line: 24
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\index.php
Line: 315
Function: require_once


What should I do to fix the program?
" If I looks more intelligence please increase my reputation."
Reply

#5
Your error is saying $images is not found,

Before using a foreach loop you should at least check the item is set.

PHP Code:
<?php if($images)  ?>
   <?php foreach ($images as $image) { ?>
      // do loop
   <?php ?>
<?php 
} else { ?>
   // deal with no images found
<?php ?>

So the query you are using to define images does not seem to be working.

Hope that helps,

Best wishes,

Paul.
Reply

#6
(04-18-2016, 01:57 AM)PaulD Wrote: Your error is saying $images is not found,

Before using a foreach loop you should at least check the item is set.

PHP Code:
<?php if($images ?>
   <?php foreach ($images as $image) { ?>
      // do loop
   <?php ?>
<?php 
} else { ?>
   // deal with no images found
<?php ?>

So the query you are using to define images does not seem to be working.

Hope that helps,

Best wishes,

Paul.

Probably best to check using:

PHP Code:
if(isset($images)) { 

Or the warning will still show up.
Reply

#7


Parse error
: syntax error, unexpected '<' in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\innovationci3\application\views\cms\index.php on line 139
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '<'
Filename: cms/index.php
Line Number: 139
Backtrace:



------------------------------------------------------------------


<b>Latest Projects:</b><br><br>

<img src="<?php echo base_url('images/large pic.jpg');?>" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic3.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic4.jpg" width="200px">&nbsp&nbsp<img src="<?php echo base_url();?>images/large pic5.jpg" width="200px" height="165px">


views/cms/index.php

PHP Code:
<?php     
              
<?php if($images) { ?>
                    <?php foreach ($images as $image) { ?>
                        echo "loop"; // do loop
              <?php ?>
                    <?php } else { ?>
                        echo "no images";  // deal with no images found
                        
              <?php ?>
            
           <?php
              
foreach ($images as $image) {
           ?>        
                    <li>
                        <span class="zoom">
                            <img  src="http://www.purelineliving.com/public/thumb.php?src=<?php echo $rec->src;?>&w=800&h=800&zc=1">
                        </span>
                    </li>
           <?php
           
}
?>

Line 139:

              <?php if($images) { ?>

How to fix the error?

Thanks in advance.
" If I looks more intelligence please increase my reputation."
Reply

#8
It's could be because you are using a space in the image name some servers do not like spaces in a file name.

Try chataking the space out of the file name.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
PHP Code:
<?php
    
<?php 

Php can't handle this. Before using a <?php opening tag, the previous one should be closed with ?>
So delete the first <?php opening tag!

And a tip to make your code better to read:
PHP Code:
<?php if ($images) : ?>
  <?php foreach ($images as $image) : ?>
     //do whatever you need with each image
  <?php endforeach; ?>
<?php 
else : ?>
  // determine what to do if there are no images
<?php endif; ?>
Reply

#10
Quote:Probably best to check using:

PHP Code:
if(isset($images)) { 

Or the warning will still show up.

True, I assumed that if his controller was trying to get images then it would return false if none found.

However, if you are checking everything you should also check it is an array before the foreach too  Big Grin
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.