如果类别行为空,如何显示默认图像

时间:2018-07-16 06:25:57

标签: php

如果用户没有图像,并且在我们的数据库中类别为nu​​ll,则行显示默认照片。而且我每个类别都需要4张图片。 我该怎么做,请帮助我谢谢

 // Create connection 
$conn = new mysqli($servername, $username,
 $password, $dbname); // Check connection if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error); } 


     $sql_pro = "SELECT imgname from providers where categories = 'Diet and Fitness' ";

     $run=mysqli_query($conn,$sql_pro);//here run the sql query.  
     while($row=mysqli_fetch_array($run)){//while look to fetch the result and store in a array $row.  

        $imgname = $row[0];
     $categories="Diet and Fitness";

     if($categories=='null')
     {
        echo "<img src='images/default.png'  width='139px' height='100px'>";
         }
     else
     {
         echo " <img src='uploads/providers/$imgname' width='139px' height='100px'>";  
     }


 $conn->close(); } ?> 

1 个答案:

答案 0 :(得分:0)

有两件事:

  • 首先,您需要使类别名称动态化,因为它应该通过用户提交的表单来自request-data

  • 第二,您需要更改if条件以检查$imagename变量是否具有任何值,这将决定是显示来自DB的实际图像还是显示默认图像。 / p>

这是它的样子:

    <?php
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    // Assuming Categoty name provided from request-data as user is 
    // submitting a form and it is coming in "category_name" key.
    // If it is not there in request-data then set it to 
    // default value as "Diet and Fitness"
    $categories = (isset($_REQUEST['category_name']) && !empty($_REQUEST['category_name'])) ? $_REQUEST['category_name'] : 'Diet and Fitness';

    // Use "$categories" set above instead of hardcoded category name
    $sql_pro = "SELECT imgname from providers where categories = '" . $categories . "'";
    $run = mysqli_query($conn,$sql_pro); //here run the sql query.

    // Just a guess if mysqli_num_rows() will provide us total number 
    // of rows in resultset. Please correct this function if not 
    // returning total row count.
    if(mysqli_num_rows($run) > 0) {
        while($row = mysqli_fetch_array($run)){
            //while look to fetch the result and store in a array $row.
            $imgname = $row[0];

            // This is not required now as we have category name 
            // in "$categories" variable.
            //$categories="Diet and Fitness";

            // Check if "imagename" is empty in database then use a 
            // default image otherwise use what is coming from DB.
            if($imgname == null OR $imgname == '') {
                echo "<img src='images/default.png'  width='139px' height='100px'>";
            } else {
                echo " <img src='uploads/providers/$imgname' width='139px' height='100px'>";
            }
        }
    } else {
        echo "<img src='images/default.png'  width='139px' height='100px'>";
    }
    $conn->close();
    ?> 

由于某个类别的图像来自名为providers的数据库表,因此while循环将自动显示与表中一样多的图像。