Codeigniter: html output in wrong order

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

webmasterInTraining
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Apr 09, 2014 1:45 am

Codeigniter: html output in wrong order

Postby webmasterInTraining » Wed Apr 09, 2014 1:57 am

Hi all,

I seem to be having a problem with my view as it is some how nesting my footer view inside my gallery view. I would really appreciate if anyone could help me figure out what I did wrong here. I have wasted hours on end messing around with it and need a fresh set of eyes as it is probably something very simple. Anyway, here is the code...

gallery view

Code: Select all

<div id="gallery">
<?php $count = 1;
foreach($records->result() as $row) {

   $id = 'image'.$count;
   
   if($count % 5 === 1) { ?>
   
      <div class="row">
   
      <div id="<?php echo $id; ?>" class="image-wrap"><img src="<?php echo $row->url; ?>" alt="<?php echo $row->alt ?>" /></div>
   
   <?php }else if($count % 5 != 0 && $count % 5 != 1) { ?>
   
      <div id="<?php echo $id; ?>" class="image-wrap"><img src="<?php echo $row->url; ?>" alt="<?php echo $row->alt ?>" /></div>
   
   <?php }else if($count % 5 === 0) { ?>
   
      <div id="<?php echo $id; ?>" class="image-wrap"><img src="<?php echo $row->url; ?>" alt="<?php echo $row->alt ?>" /></div>
   
      <!--</div>-->
   
   <?php }
   
   $count++;
}

echo $this->pagination->create_links();   ?>   

</div><!-- end gallery-->


footer view

Code: Select all

<div id="footer">
                  
            <span>&copy; 2014</span>

            <p>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod
            tincidunt ut laoreet dolore magna aliquam erat volutpat. 
            </p>

         </div><!-- end footer -->

      </div><!-- end container -->

    </body>
   
</html>


site_controller

Code: Select all

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site_controller extends CI_Controller {

   public function __construct(){

      parent::__construct();
      
      $this->load->model('site_model');
      
      $this->load->helper('form');
      
      
   }

   public function index() {
      
      $config['base_url'] = base_url().'home';
      $config['total_rows'] = $this->db->get('logo')->num_rows();
      $config['per_page'] = 2;
      $config['num_links'] = 20;
      $config['full_tag_open'] = "<div class=pagination>";
      $config['full_tag_close'] = "</div>";
      $config['cur_tag_open'] = '<b>';
      $config['cur_tag_open'] = '</b>';
      $config['uri_segment'] = 2;
      
      $this->pagination->initialize($config);
      
      $this->db->select('url, alt');
      
      $data['records'] = $this->db->get('logo', $config['per_page'], $this->uri->segment(2));
   
      $data['logos'] = $this->site_model->getGallery();
      $this->load->view('header');
      $this->load->view('main');
      $this->load->view('gallery', $data);
      $this->load->view('footer');
   
   }

/* End of file site_controller.php */
/* Location: ./application/controllers/site_controller.php */

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Codeigniter: html output in wrong order

Postby seandisanti » Wed Apr 09, 2014 9:29 pm

The point of using an MVC framework is to separate your php from your html, and your business rules from your data. I'm not saying that to pick on you or your effort, but to remind you and to save you some headaches.

First, identify your objects, presumably you'll have a gallery object, and an image object (assuming it's just a gallery of images, but it would work the same with any other kind of thing that you're posting in your gallery).

For each of your objects, identify what properties you need to access, store, and modify from the data. Those become the properties that you define in the model for the object.

Your controller, again for each object would be a collection of functions that define the functionality of the object, business rules etc. For example, instantiating a gallery object could create image objects for each of the images associated with that gallery, Loading a gallery view would be as simple as having the gallery display the view of each of it's member images.

The last thing to worry about is the view. Assuming that you've done the rest carefully and methodically, your view is going to be a very simple display. Think of it like a mail merge in the old days, you would write a letter to be sent to a customer, and would design the whole layout with placeholders for the data that you knew would be populated by another step in the process. The view is the same way, by the time the view is being loaded, all of the information for the object is already available because it's been retrieved in the model, manipulated and formatted by the controller, and then displayed by the view.

Any type of MVC framework is going to take a little bit of time to get used to, but you'll make your job a lot easier by using the components as they're intended, and the rest will pretty much fall into place. There are a lot of good resources online for codeigniter and other MVC frameworks, check out the 'jream' channel on youtube, and jream.com for some plain language explanations and demonstration of MVC, his stuff helped me a lot when i was fist getting started.

webmasterInTraining
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Apr 09, 2014 1:45 am

Re: Codeigniter: html output in wrong order

Postby webmasterInTraining » Fri Apr 11, 2014 9:46 am

Hey thanks a lot for the reply I am just checking out JREAM, and trying to straighten out some of the OOP flaws.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Codeigniter: html output in wrong order

Postby seandisanti » Fri Apr 11, 2014 5:54 pm

I'm glad to help, and jream has some great stuff, even a premium course on code igniter, but the premium courses aren't free

webmasterInTraining
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Apr 09, 2014 1:45 am

Re: Codeigniter: html output in wrong order

Postby webmasterInTraining » Sat Apr 12, 2014 12:41 pm

Yeah I am actually going to buy that one. I really liked the free stuff he does, so I can't wait to see the premium course. I do have another question for you though. How would you go about separating the php code from the view? Like, if I built a gallery object composed of image objects would I include some of the html in a display method so I can just construct the object in the view and call display? I have a bit of java background, and I have done a fair amount of reading on OOP, but can't seem to go from the theoretical concepts to an actual OOP design.

Thanks again :)

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Codeigniter: html output in wrong order

Postby seandisanti » Sat Apr 12, 2014 8:09 pm

basically your view of a member of the gallery would just be a small block of html, that formats and displays the data. Then the Gallery view would load the views of each of it's members. I think it'll make more sense when you're going through the videos


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron