Codeigniter fotoalbum

23-09-2010 - Ricardo Monsees

Na ongeveer n half jaar met codeigniter gewerkt te hebben werdt het toch eens hoogtijd om maar eens iets positiefs te schrijven. laatst werdt er gevraagd of ik een fotoalbum wilde maken. Ben mischien nog ouderwets maar normaal betekende dat een helehoop gepruts met tabellen in codeigniter niet. onderstaand n simpele versie van dit foto album.

 

Database:

De database bestaat uit 2 verschillende tabellen een waar de categorieën worden opgeslagen de andere is voor de foto's. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE IF NOT EXISTS `gallery_category` (
`category_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`category_name` varchar(50) NOT NULL DEFAULT '0',
PRIMARY KEY (`category_id`),
KEY `category_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
CREATE TABLE IF NOT EXISTS `gallery_photos` (
`photo_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`photo_filename` varchar(100) DEFAULT NULL,
`photo_caption` text,
`photo_category` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`photo_id`),
KEY `photo_id` (`photo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Controller:

De controller laadt het model dat we later nodig hebben plus de tabel library waarmee we de tabellen opbouwen. We maken gebruik van het 2e url segment om te kijken of iemand op een foto album heeft geklikt, anders is de $album variable leeg.

Wanneer de variable $album leeg is gaan we de gegevens voor de albums instellen, allereerst wordt er ingesteld hoeveel albums er naast elkaar weergegeven worden. Vervolgens wordt via het photoalbum_model alle albums opgehaald, en deze gegevens laden we in in de view list_albums. Voor zover de controller.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function index()
// Foto album
{
$this->load->model('photoalbum_model');
$this->load->library('table');
 
$album = $this->uri->segment(2, '');  
 
if ( empty($album) )
{
$condata['cats_in_row'] = 4; // aantal categorieën in een rij
 
//overzicht categorieën ophalen
$condata['cats'] = $this->photoalbum_model->get_cats();
$data['content'] = $this->load->view('photoalbum/list_albums', $condata, true);
}
else
{
$condata['photo_in_row'] = 4; // aantal foto\'s in een rij
 
//foto's ophalen
$condata['photos'] = $this->photoalbum_model->get_photos_bycat($album);
$condata['catid'] = $this->photoalbum_model->get_cat_byid($album);
$data['heading'] = $condata['catid']['category_name'];
 
$data['content'] = $this->load->view('photoalbum/list_photos', $condata, true);
}
 
 
$this->load->view('page', $data);
}

 

Model:

Mischien niet helemaal de codeigniter manier maar ben nog iemand die queries voluit schrijft. In het photoalbum_model staan de volgende functies:

getcats: Deze functie haalt in de database alle foto categorieën op met van elke categorie een plaatje, en stuurt vervolgens een array met waarden terug of false.

get_photos_bycat: Deze functie haalt uit de database de categorie naam, en stuurt vervolgens een array met waarden terug of false.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function get_cats()
{
 
$query = $this->db->query( "SELECT `category_id`, `category_name`, `photo_filename`, `photo_id`,
`photo_caption`, COUNT(photo_id) as `num_photos`
FROM gallery_category
LEFT JOIN `gallery_photos` ON `photo_category` = `category_id`
GROUP BY `category_id`
ORDER BY `category_name` ASC
" );
 
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
}
 
function get_photos_bycat($cat = 0)
{
$query = $this->db->query( "SELECT * FROM `gallery_photos`
LEFT JOIN `gallery_category`
ON `gallery_photos`.`photo_category` = `gallery_category`.`category_id`
where `gallery_photos`.`photo_category` = ". $this->db->escape($cat) ."
" );
 
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
 
}
 
function get_cat_byid($catid = 0)
{
 
$query = $this->db->query( "SELECT * FROM gallery_category
WHERE `category_id` = ". $this->db->escape($catid) ." LIMIT 1" );
 
if ($query->num_rows() > 0)
{
return $query->row_array();
}
else
{
return false;
}
}

 

View:

De view list_albums loopt door alle categorien die van de controller meegestuurd zijn vervolgens worden deze met al hun html code in een array gezet, wanneer alle categorien doorlopen zijn gaat een 2e lus in dit geval de 1e 4 (opgeven in de controller) array waarden uit de array halen en zet deze in een rij. Dit gaat net zolang door tot de tabel array leeg is waarna de tabel opgebouwd wordt.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
if ( $cats )
{
$table_data = '';
 
foreach( $cats as $cats_val)
{
if ( !empty($cats_val['photo_filename']))
{
$location = '/photoalbum/'.$cats_val['category_id'].'/_thumbs/_'.$cats_val['photo_filename'];
$table_data[] = '
<a href="/fotos.html/'.$cats_val['category_name'].'">
'.( file_exists($_SERVER['DOCUMENT_ROOT'].$location) ? '<img border=0 src="'.$location.'"></img><br />' : '<div style="width:100px; height:100px;"></div>' ).'
<span>'.$cats_val['category_name'].'</span>
</a>
';
}
}
 
$table = false;
while ( !empty($table_data) )
{
$table = true;
$output = array_splice($table_data, 0, $cats_in_row);
 
$this->table->add_row($output);
}
 
if ( $table )
{
echo '<div id="gallery">';
echo $this->table->generate();
echo '</div>';
}
else
{
echo '<p>Er zijn nog geen fotoalbums toegevoegd!</p>';
}
}
else
{
echo '<p>Er zijn nog geen fotoalbums toegevoegd!</p>';
}

 

De view list_photos loopt door alle foto's die meegestuurd zijn vanaf de controller, het overzicht met foto's wordt vervolgens hetzelfde opgebouwd als de list_albums view.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$table_data = '';
if ( $photos )
{
foreach( $photos as $photo)
{
$table_data[] =
'
<a href="/photoalbum/'.$photo['category_id'].'/'.$photo['photo_filename'].'"
title="'.(!empty($photo['photo_caption']) ? $photo['photo_caption'] : $photo['photo_filename']).'">
<img border=0 src="/photoalbum/'.$photo['category_id'].'/_thumbs/_'.$photo['photo_filename'].'" />
</a>
 
   ';
}
$table = false;
while ( !empty($table_data) )
{
$table = true;
$output = array_splice($table_data, 0, $photo_in_row);
$this->table->add_row($output);
}
 
if ( $table )
{
echo '<div id="gallery">';
echo $this->table->generate();
echo '</div>';
 
}
else
{
echo '<p>Er zitten nog geen foto\'s in deze categorie</p>';
}
}
else
{
echo '<p>Er zitten nog geen foto\'s in deze categorie</p>';
}

 

 

De page view bevat alle informatie over de pagina opbouw hieronder een simpel voorbeeld hoe deze eruit ziet.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<body>
 
<!-- container voor de gehele pagina -->
<div id="container">
<?php $this->load->view('content/header'); ?>
 
 
<div id="content-container">
 
<?php $this->load->view('menu/main_menu'); ?>
 
<div id="content"><?=$content;?></div>
 
</div>
 
 
 
<?php $this->load->view('content/footer'); ?>
</div>
 
 
</body>