douzuan5365 2015-08-14 12:30
浏览 8
已采纳

如何使用php按类别订购上传的图片?

I uploaded some images to a MySQL database using a simple form and some php. The form has the following selection for catagory inputs.

<select name="image_ctgy">
<option value="animals">animals</option>
<option value="vegetables">vegetables</option>
<option value="minerals">minerals</option>
</select>

Each image has one of these categories in the MySQL table 'image_ctgy'. Now I want to echo the images in the category animals to my webpage.

I use the following code to get all the images on my webpage:

<?php
/*** Check the $_GET variable ***/
   try    {
          /*** connect to the database ***/
          $dbh = new PDO("mysql:host=;dbname=", '', '');

          /*** set the PDO error mode to exception ***/
          $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

          /*** The sql statement ***/
          $sql = "SELECT image_id, thumb_height, thumb_width, image_ctgy, image_type, image_name, title FROM testblob";

          /*** prepare the sql ***/
          $stmt = $dbh->prepare($sql);

          /*** exceute the query ***/
          $stmt->execute(); 

          /*** set the fetch mode to associative array ***/
          $stmt->setFetchMode(PDO::FETCH_ASSOC);

          /*** set the header for the image ***/
          foreach($stmt->fetchAll() as $array)
              {
            echo '<div class="thumb" style="width: '.$array['thumb_width'].'px; height: '.$array['thumb_height'].'px;">
            <p class="title">' . $array["title"] . '</p>
            <p><a href="showfile.php?image_id='.$array['image_id'].'">
            <img src="showthumbs.php?image_id='.$array['image_id'].'" alt="'.$array['image_name'].' /">
            </a></p>
            </div>';

        }
   }
     catch(PDOException $e)
        {
        echo $e->getMessage();
        }
     catch(Exception $e)
        {
        echo $e->getMessage();
        }
?>

I think I need an if statement in the foreach loop to check if the image has to catagry Animals. Can anyone help me with this? Thanks

  • 写回答

1条回答 默认 最新

  • dqy1265 2015-08-14 15:58
    关注

    Raveenanigam posted an awnser to my question.

    you can change with query with WHERE clause : WHERE category = 'Animal'

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法