dshmvqnl98119 2013-04-03 03:20
浏览 16
已采纳

来自同一阵列的不同链接?

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.

  • 写回答

1条回答 默认 最新

  • douqianzha6213 2013-04-03 03:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大