Welcome Guest, Not a member yet? Register   Sign In
File manager with icons
#1

[eluser]rafabayona[/eluser]
Hi, I'm new to CodeIgniter.

I'm building a light file uploader/manager for a project and the uploading stuff is done, but now I would show an icon for each file, depending on its extension/type.

Should I store into the DB the file_type or the file_ext, or both? Can I reuse the config/mimes.php for this? I just allow uploading a few filetypes (~10-20 maybe).

Now I'm using this code in the view:
Code:
foreach ($ficheros as $f):?>
    <div class="fichero">
        <img src="&lt;?php echo base_url();?&gt;assets/img/file_48x48.png" alt="Documento" class="icono"/>
        <h3 title="&lt;?php echo$f['titulo'];?&gt;">&lt;?php echo character_limiter($f['titulo'], 40);?&gt;&lt;?php if (!$f['activo']) echo "<sup>Inactivo</sup>";?&gt;</h3>

        <p title="&lt;?php echo$f['desc_corta'];?&gt;">&lt;?php echo character_limiter($f['desc_corta'], 60);?&gt;</p>
        <div class="ocultar peroMostrarEnHover">
           &lt;?php echo anchor("ficheros/editar/$f[id]", 'Editar');?&gt; | &lt;?php echo anchor("ficheros/descargar/$f[id]", 'Descargar');?&gt;
        </div>
        <div class="clearboth"></div>
    </div>&lt;!--fichero--&gt;
&lt;?php endforeach ?&gt;

Thanks in advance and sorry for my english.
#2

[eluser]sheri.nust[/eluser]
Well.
There can be many ways to solve your problem.

you want to show an icon for each uploaded file.

I would suggest that

when uploading file , you can find the file type at PHP level.

And save that file type in DB in a separate column alongwith filename.

This would reduce your cost of opening file each time and finding its file type at run time.

Now when you read file name,file type from DB,like

file name : file123.doc
file type : doc

At PHP level ,

suppose your file type is doc, like
$DBFileType='doc';


$arrIcon['doc']='doc_48x48.png'
$arrIcon['pdf']='pdf_48x48.png'
$arrIcon['gif']='gif_48x48.png'

$iconImg=$arrIcon[$DBFileType];

change the image for icon dynamically,

<div class="fichero">
<img src="&lt;?php echo base_url();?&gt;assets/img/&lt;?=$iconImg?&gt;" alt="Documento" />
</div>

I would not suggest

config/mimes.php for your problem.
#3

[eluser]rafabayona[/eluser]
Ok, thank you. I have made this function in Files_model

Code:
function getExtIcon($ext)
{
    $icons = array
        (
            '.doc' => 'doc.png',
            '.pdf' => 'pdf.png',
            '.xls' => 'xls.png',
            '.odt' => 'doc.png',
            '.xslt' => 'xls.png',
            '.docx' => 'doc.png',
            'default' => 'file.png'
        );

        $path = base_url() . 'assets/img/';

        if (isset($icons[$ext]))
        {
            $fullPath = $path . $icons[$ext];
        }
        else
        {
            $fullPath = $path . $icons['default'];
        }

        return $fullPath;

}

And then in the view
Code:
<img >Ficheros_model->getExtIcon($f['extension'])?&gt;" alt="Icono" class="icono"/>

Now it's working, but should I leave the icons array in this function?
#4

[eluser]sheri.nust[/eluser]
You are welcome,

And it is okay with the array inside your function,
When you want to add new entry only update that array inside function.
#5

[eluser]sheri.nust[/eluser]
And
go to link for CI documentation

http://ellislab.com/codeigniter/user-gui...pflow.html

first have a good understanding of CI application Flow.

You shouldn't call your Model function directly within your View.

Rather you have to call through your controller function.

In your controller function

function index($extension)
{
$iconImage=$this->Ficheros_model->getExtIcon($Imgname);// $Imgname or whatever you want to pass
}

And use this in your View

<img src="&lt;?=$iconImage?&gt;" alt="Icono" class="icono"/>
#6

[eluser]Colin Williams[/eluser]
Better to just put a class on the element and let CSS do the formatting.
#7

[eluser]sheri.nust[/eluser]
@Williams.

Sorry can u explain a bit, what u said.?
#8

[eluser]rafabayona[/eluser]
I think I know what you mean, Colin, something like getExtClass($ext) and using a div instead of an img tag; then define in the css file a selector for each class setting the background, am I wrong?

But I can't see the advantage of doing that, seems like coding the same twice.
#9

[eluser]rafabayona[/eluser]
[quote author="sheri.nust" date="1254393984"]And
go to link for CI documentation

http://ellislab.com/codeigniter/user-gui...pflow.html

first have a good understanding of CI application Flow.

You shouldn't call your Model function directly within your View.

Rather you have to call through your controller function.

In your controller function

function index($extension)
{
$iconImage=$this->Ficheros_model->getExtIcon($Imgname);// $Imgname or whatever you want to pass
}

And use this in your View

<img src="&lt;?=$iconImage?&gt;" alt="Icono" class="icono"/>[/quote]

Oops, I haven't seen this post, sorry!

Thank you for the advise, but I need an icon for each file. I show 30 files per page.

Maybe making a helper?




Theme © iAndrew 2016 - Forum software by © MyBB