I'm trying to make a photo gallery with multiple galleries listed that I need to paginate.
So, this variable
$galleries = Photograph::find_by_sql($sql);
holds an array:
Array
(
[0] => Photograph Object
(
[id] =>
[gallery_name] => candies
)
[1] => Photograph Object
(
[id] =>
[gallery_name] => icecream
)
[2] => Photograph Object
(
[id] =>
[gallery_name] => pastries
)
[3] => Photograph Object
(
[id] =>
[gallery_name] => chocolate
)
)
(I've shortened it for convenience)
I'm using these two variables to set a selected gallery:
$newest_gallery = reset($galleries);
$gallery_name = (isset($_GET['subj']) ? $_GET['subj'] : $newest_gallery->gallery_name);
I was able to set the selected gallery by using the gallery_name, however, I'm unable to paginate the galleries since I need to somehow dynamically store a numerical index of an array element (which holds the gallery_name) into a variable in order to create the pagination function, since I need an integer value for this purpose. So, I basically need to get the index of an array element. Is there a way to do this?
If I understand your question correctly, you have to make a traverse over the array:
## subj is set - search for it in the array of the objects
if (isset($_GET['subj'])) {
foreach ($galleries as $id => $gallery) {
if ($gallery->gallery_name == $_GET['subj']) {
## we've found a match - note the id and the name
$gallery_name = $_GET['subj'];
$gallery_id =$id;
break;
}
}
}
## if subj was not specified or we found no match in the array
if (!isset($gallery_id)) {
reset($galleries);
$gallery_id = key($galleries);
$gallery_name = $galleries[$gallery_id]->gallery_name;
}
Just loop through your array of galleries and add set the value for id yourself:
$i = 0;
foreach($galleries as $gallery){
$gallery['id'] = $i;
$i++;
}