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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错