dongwei1921 2018-03-29 10:06
浏览 49
已采纳

在不知道当前位置的情况下查找上一个/下一个ID(PHP和MySQL)

This is my first time posting here so please bear with me.

I'm creating an image gallery using HTML & CSS, where I show one image on the screen and it has a prev/next button to toggle through the images (at no point does this take you to a new webpage). I'm using PHP & MySQL to populate the images.

So, I have a table like this:

    | ID | Name       | Genre      |
    --------------------------------
    | 1  | Apple      | Fruit      |
    --------------------------------
    | 2  | Cabbage    | Veg        |
    --------------------------------
    | 3  | Lettuce    | Veg        |
    --------------------------------
    | 4  | Pear       | Fruit      |
    --------------------------------
    | 5  | Banana     | Fruit      |
    --------------------------------
    | 6  | Leek       | Veg        |
    --------------------------------
    | 7  | Kiwi       | Fruit      |

I want to only images where genre=fruit, so now my table is like the following:

    | ID | Name       | Genre      |
    --------------------------------
    | 1  | Apple      | Fruit      |
    --------------------------------
    | 4  | Pear       | Fruit      |
    --------------------------------
    | 5  | Banana     | Fruit      |
    --------------------------------
    | 7  | Kiwi       | Fruit      |

Since the IDs are no longer incremental, I can no longer simply use $id-1 or $id+1 to get the prev/next photos.

This is what my PHP code looks like (simplified for this post):

 <?php
 $sql = "SELECT * FROM photo WHERE genre LIKE '%fruit%' ORDER BY id DESC";
 $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

 while ($rows = mysql_fetch_assoc($result)) {
     $id=$rows['id'];
     $name=$rows['name'];
     $genre=$rows['genre'];

     echo   "<div id=\"img$id\">".
            "<a href=\"#img".($id+1)."\">&lt;</a>".
            "<a href=\"#img".($id-1)."\">&gt;</a>".
            "<img src=\"img/full/$name.jpg\">".
            "</div>";
 } // end while 
 ?>

Since I'm loading all of the fruit photos, I don't know what is the current ID and I won't know how many rows there will be displayed in total. I'd like to find the ID of the row item that is before and after the current row/photo the user sees.

I've tried using current(), next(), and prev() but they all seem to select the first 3 rows only. Any help would be so greatly appreciated! Been researching this all day and haven't been able to solve this :( Thanks so much in advance.

  • 写回答

1条回答 默认 最新

  • dousha4804 2018-03-29 10:19
    关注

    Here's a sample code with comments:

    $images = [];
    while ($rows = mysql_fetch_assoc($result)) {
        // first we store images in an array
        $images[] = [
            'id' => $rows['id'],
            'name' => $rows['name'],
            'genre' => $rows['genre'],
        ];
    }
    
    // next we iterate over this array
    foreach ($images as $index => $image) {
        echo '<div id="img' . $image['id'] . '">';
        // as array is numerically indexed 
        // we can get previous item's index as `$index - 1`
        // and check if it is set
        if (isset($images[$index - 1])) {
            // if it is set - we output it
            echo '<a href="#img' . $images[$index - 1]['id'] .'">&lt;</a>';
        }
        // as array is numerically indexed 
        // we can get next item's index as `$index + 1`
        // and check if it is set
        if (isset($images[$index + 1])) {
            // if it is set - we output it
            echo '<a href="#img' . $images[$index + 1]['id'] .'">&gt;</a>';
        }
        echo '<img src="img/full/' . $image['name'] . '.jpg">';
        echo '</div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么