Tuccinator
Tuccinator

Reputation: 67

Different links from same array?

A kind user on here gave me this code:

$singles = valley_images();
$groups = valley_group_images();
$images = array_merge($singles, $groups);

$sortArray = array(); 

foreach($images as $image){ 
    foreach($image as $key=>$value){ 
        if(!isset($sortArray[$key])){ 
            $sortArray[$key] = array(); 
        } 
        $sortArray[$key][] = $value; 
    } 
} 

$orderby = "timestamp"; //change this to whatever key you want from the array 

array_multisort($sortArray[$orderby],SORT_DESC,$images); 

foreach($singles as $single) {
    echo '<img src="1/'.$image['album_id'].'/'.$image['code'].'.'.$image['ext'].'"/>';
}

foreach($groups as $group) {
    echo '<img src="groups/'.$image['group_id'].'/'.$image['code'].'.'.$image['ext'].'"/>';
}

Now what the problem is, i will be grabbing images from 2 different tables, images and group_images. But images are stored in the "1" folder while the group_images are stored in the "groups" folder. How would I manage to change the link depending on the table with that code, since the code doesn't tell the table it comes from?

The last 2 foreach statements are seperate and are not sorted. I am unsure on how to merge the foreach like that and get sorted.

Upvotes: 0

Views: 51

Answers (1)

user557846
user557846

Reputation:

assuming this is the source of the data:

$stmt = $conn->prepare("SELECT images.*, group_images.* ORDER BY `timestamp` DESC");

i would change it to

$stmt = $conn->prepare("(SELECT images.*, 'single' as db from images) UNION  (SELECT group_images.*, 'group' as db from group_images) ORDER BY `timestamp` DESC");

all else going well you should have the array key of 'db' to use in the output to determine the link.

Upvotes: 1

Related Questions