CodeIgniter Forums
Attempt 2 understand models and controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Attempt 2 understand models and controllers (/thread-51867.html)



Attempt 2 understand models and controllers - El Forum - 05-21-2012

[eluser]ns8814[/eluser]
I attempted to post this question earlier but for some reason it cut it off half way through so I am trying again. I'm new to codeigniter and php and MVC so I am just trying to figure out where I should put a piece of code like this

Code:
<?php
function filenamefrompath($input){
$filenameonly=basename($input);
echo $filenameonly;
}
$imagesize='width="553" height="300"';
$customerid = $this->uri->segment(3);
$projectimagepath= base_url('images/estimates/'.$customerid);
  
$directory = $_SERVER['DOCUMENT_ROOT'].'codeignitor1/images/estimates/'."$customerid";
  
  foreach (glob("$directory".'/'."*.jpg") as $filename){
  
   echo "<img title="#htmlcaption">';
  }
  
  

?&gt;

I have it In the view because it was easier to debug and get working. I don't believe it belongs in the view but putting it inside a model doesn't seem right either. and its not a controller. So it go in a controller I'm not sure. Just trying to get everything figured out the right way before I develop bad habits. Basically it is pulling checking a folder for images and using the images it finds to create a photo gallery. I would appreciate a shove in the right direction.


Attempt 2 understand models and controllers - El Forum - 05-21-2012

[eluser]dnc[/eluser]
Yes, it needs to go in a controller or at the least a common functions file possibly in your include_path. Models are designed for database operations, per the guide What is a Model


Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common_Functions extends CI_Controller {


function filenamefrompath($input){
$filenameonly=basename($input);
echo $filenameonly;

$imagesize='width="553" height="300";
$customerid = $this-&gt;uri-&gt;segment(3);
$projectimagepath= base_url('images/estimates/'.$customerid);
  
$directory = $_SERVER['DOCUMENT_ROOT'].'codeignitor1/images/estimates/'."$customerid";
  
  foreach (glob("$directory".'/'."*.jpg") as $filename){
  
   echo "<img title="#htmlcaption">';
  }

}


Quote:I have it In the view because it was easier to debug and get working. I don’t believe it belongs in the view but putting it inside a model doesn’t seem right either. and its not a controller. So it go in a controller I’m not sure. Just trying to get everything figured out the right way before I develop bad habits.


By putting a function in a view file is a bad habit and goes against the MVC approach and the approach of most other php frameworks for that matter.


Attempt 2 understand models and controllers - El Forum - 05-22-2012

[eluser]Samus[/eluser]
[quote author="dnc" date="1337655841"]Yes, it needs to go in a controller or at the least a common functions file possibly in your include_path. Models are designed for database operations, per the guide What is a Model

.[/quote]
I disagree.

Models were designed for dealing with data, yes that does include database operations, but it is t limited to that.

I don't see any reason why this can't go in a model, you're extracting data from a directory, sure sounds like you're dealing with data there.