Welcome Guest, Not a member yet? Register   Sign In
Need help with image script
#1

[eluser]Unknown[/eluser]
I cant get this script to work in codeingniter. Ive tried different paths for images but it just doesnt find any. Last echo is executed. "Images cannot be found".

//controller
Code:
$this->load->model('loadimages');
//view
Code:
<?php $this->loadimages->printimages($this->uri->segment(3)); ?>


//model
Code:
<?php
class Loadimages extends CI_Model {

function printimages($id) {

    $thumb = "images/$id/{*.jpg,*.jpeg,*.png}";
    $big_img = "images/$id/big_img/$id/{*.jpg,*.jpeg,*.png}";
    $main_img = "images/$id/main_img/{*.jpg,*.jpeg,*.png}";

    if ((glob($thumb, GLOB_BRACE) != FALSE) && (glob($big_img, GLOB_BRACE) != FALSE))
    {
    $files = glob($thumb, GLOB_BRACE);
    $big_files = glob($big_img, GLOB_BRACE);
        echo "<table>";
        $x = 0;
        if (glob($main_img, GLOB_BRACE) != FALSE)
        {
            $main_files = glob($main_img, GLOB_BRACE);
            echo '<tr><th align="center" colspan="5"<img src="'.$main_files[0].'" alt="alttext" />'."&nbsp;&nbsp;</a></th></tr>";
        }
        for ($i=0; $i<count($files); $i++)
        {
            $num = $files[$i];
            $num2 = $big_files[$i];
            if($x % 4 == 0 && $x != 0) {
                echo '</tr><tr><td>cant post links<img src="'.$num.'" alt="alttext" />'."&nbsp;&nbsp;</a></td>";
            }
            else
            {
                echo '<td>cant post links<img src="'.$num.'" alt="alttext">'."&nbsp;&nbsp;</a></td>";
            }
            $x++;
        }
    echo "</table>";
    }
    else
    {
        echo "Images cannot be found!";
    }
    echo "</tr>";
}
}
#2

[eluser]egunay[/eluser]
First of all I see that you are accessing your model directly from your view which isn't the best way to do things.

Secondly, models are PHP classes that are designed to work with information in your database. So if you are not going to connect to a database and select, insert, delete etc. stuff don't use it. Instead do it in your controller or create a library.

If you are using ".htaccess" be sure that you add your images folder in the exceptions:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|something else...)
RewriteRule ^(.*)$ /index.php/$1 [L]
#3

[eluser]Unknown[/eluser]
Thank you for your answer egunay. I created a library for the image script. I also got it to work with a few changes.

Now when the script works its nicer to start improving it. You said that i should not access my model directly from my view. I guess its same with library... Maybe i try to modify that next.

here is the code:

//view
Code:
&lt;?php $this->loadimages->printimages($this->uri->segment(3), base_url()); ?&gt;

//controller
Code:
$this->load->library('loadimages');

//library
Code:
&lt;?php
class Loadimages {

function printimages($id, $urli) {
    $thumb = glob("images/$id/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    $big_img = glob("images/$id/big_img/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    $main_img = glob("images/$id/main_img/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    if ($thumb != 0) {
        echo "<table width='900'>";
        $x = 0;
        echo '<tr><th align="center" colspan="5"<img src="'.$urli.'/'.$main_img[0].'" alt="jia" />'."&nbsp;&nbsp;</a></th></tr>";
        for ($i=0; $i<count($thumb); $i++)
        {
            $num = $thumb[$i];
            $num2 = $big_img[$i];
            if($x % 4 == 0 && $x != 0) {
                echo '</tr><tr><td>links comes here<img src="'.$urli.'/'.$num.'" alt="jia" />'."&nbsp;&nbsp;</a></td>";
            }
            else
            {
                echo '<td>links comes here<img src="'.$urli.'/'.$num.'" alt="jia">'."&nbsp;&nbsp;</a></td>";
            }
            $x++;
        }
    echo "</table>";
    }
}
}
#4

[eluser]egunay[/eluser]
Your welcome,

Using a model from library is a bit more difficult then using it in your view. I mean you have to get CI instance:

In your library:

Code:
&lt;?php
class Loadimages {

function printimages($id, $urli) {
    //Get Instance
    $CI =& get_instance();
    $CI->load->model('your_model');
    
    //and from this point on instead of using "$this->" use $CI->



    $thumb = glob("images/$id/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    $big_img = glob("images/$id/big_img/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    $main_img = glob("images/$id/main_img/{*.jpeg,*.jpg,*.png}", GLOB_BRACE);
    if ($thumb != 0) {
        echo "<table width='900'>";
        $x = 0;
        /*
         ....
         ...
         ..
         .
        */

And if you want to make things still more MVC style,

In your controller

Code:
$this->load->library('loadimages');
$data['somename'] = $this->loadimages->printimages($this->uri->segment(3), base_url());
$this->load->view('your_view', $data) // Pay attention to $data here

In your view

Code:
&lt;?php
    echo $somename; //you are using the array's key name that you have created in your controller

?&gt;

//to make things more minimalistic;

&lt;?=$somename;?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB