-3

我一直在编写一些代码,根据教师上传的与同事和学生分享的项目数量向他们授予徽章。

当数组为空时,我遇到了问题。什么都没有出现。

我尝试了一个 empty() 函数,但我一定把它放在了错误的位置,因为它开始出现在有上传的用户身上。

所以基本上在下面的代码中,我遇到困难的是“如果 $uploads < 1”部分。

..由此产生的问题是如何在 mysql_fetch_array($upload_count) 为空的地方得到一条回显消息?

     <?php


$total_uploads = mysql_query("SELECT members.member_id, members.member_firstname,           members.member_lastname, COUNT(*)  
FROM uploads    
JOIN members
ON uploads.member_id = members.member_id
GROUP BY uploads.member_id   ");

$total_uploads_count    = @mysql_num_rows($total_uploads);

$upload_count = mysql_query("SELECT members.member_id, members.member_firstname,  members.member_lastname, COUNT(*)  
FROM uploads    
JOIN members
ON uploads.member_id = members.member_id AND members.member_id = ".     $_SESSION['member_id']  . "
GROUP BY uploads.member_id   ");

while($row_count=mysql_fetch_array($upload_count))  
{  


$uploads =  $row_count['COUNT(*)'];

 $unlock2 = 5 - $row_count['COUNT(*)'] ;
 $unlock3 = 20 - $row_count['COUNT(*)'] ;
 $unlock4 = 50 - $row_count['COUNT(*)'] ;
 $unlock5 = 100 - $row_count['COUNT(*)'] ;
 $unlock6 = 200 - $row_count['COUNT(*)'] ;           


 echo " you have <span class =\"blue_inherit\"> $uploads </span> uploads.<br/><br/>";

 if  ($uploads  < 1 )

 {

echo "Upload a resource to share in order to unlock your novice uploader badge!<br/>

 <img src=\"images/badges/badge_e_share_1_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" />";

 }

    else if  ($uploads >= 1 && ($uploads < 5 ))

 {

echo "Well done, you've unlocked your novice sharer's badge. <br/>

 <img src=\"images/badges/badge_e_share_1.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>


 If you upload $unlock2 more resources, you can unlock your E-sharing advanced beginner's badge. <br/>

  <img src=\"images/badges/badge_e_share_2_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>



 ";

 }


    else if  ($uploads >= 5 && ($uploads < 20 ))

 {

echo "Well done, you've unlocked your E-sharing advanced beginner's badge. <br/>

 <img src=\"images/badges/badge_e_share_2.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>


 If you upload $unlock3 more resources, you can unlock your E-sharing mini-hero badge. <br/>

  <img src=\"images/badges/badge_e_share_3_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";

 }


else if  ($uploads >= 20 && ($uploads < 50 ))

 {

echo "Well done, you've unlocked your E-sharing mini-hero badge. <br/>

 <img src=\"images/badges/badge_e_share_3.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>


 If you upload $unlock4 more resources, you can unlock your E-sharing hero badge. <br/>

  <img src=\"images/badges/badge_e_share_4_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";

 }   

else if  ($uploads >= 50 && ($uploads < 100 ))

 {

echo "Well done, you've unlocked your E-sharing hero badge. <br/>

 <img src=\"images/badges/badge_e_share_4.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>


 If you upload $unlock5 more resources, you can unlock your E-sharing super-hero badge. <br/>

  <img src=\"images/badges/badge_e_share_5_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";

 }       

else if  ($uploads >= 100 && ($uploads < 200 ))

 {

echo "Well done, you've unlocked your E-sharing super-hero badge. <br/>

 <img src=\"images/badges/badge_e_share_5.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>


 If you upload $unlock6 more resources, you can unlock your E-sharing Guru badge. <br/>

  <img src=\"images/badges/badge_e_share_6_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";

 }           


 else 

 {
    echo "You've now a Guru!! <br/>

      <img src=\"images/badges/badge_e_share_6.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";


 }



     }  



 ?>
4

2 回答 2

2

好吧,dam ..您不妨将变量名更改为

$please_inject_me、$please_inject_me1 等等...

第一课是请使用PDO

现在到您的问题(根据我对您问题的理解):

// Do your first query prepare it and then execute it, and then do a fetch and count the number of rows returned by your $query sql query
        $sql = "SELECT members.member_id, members.member_firstname, members.member_lastname, COUNT(*) FROM uploads JOIN members ON uploads.member_id = members.member_id GROUP BY uploads.member_id ";
        $query = $pdo->prepare($sql);
        $query->execute();
        $total_uploads_count = $query->fetch(PDO::FETCH_NUM);

// I guess this query you have going is counting how far the member / teacher is away from the unlock goals here we set the variable that has the num row count

        $sql2 = "SELECT members.member_id, members.member_firstname,  members.member_lastname, COUNT(*) as 'total' FROM uploads JOIN members ON uploads.member_id = members.member_id AND members.member_id = :username GROUP BY uploads.member_id";
        $query2 -> $pdo->prepare($sql2);
        $query2->bindParam(':username', $_SESSION['member_id']); // Do not forget to bind your parameters either in a array or hard code it out like I just did
        $query2->execute();

        $row = $query2->fetch();
        $upload_count = $row['total']; // Alias of COUNT(*);

Now that you PDO queries are set up now you have to create the PDO instance like so : 
You could make a file called connec.php

内容如下:

    //We connect to the database
    $host="xxxxxx"; // Host name
    $username="xxxxxxxxx"; // Mysql username
    $password="xxxxxxx"; // Mysql password
    $db_name="xxxxxxx"; // Database name

    // Connect to server via PHP Data Object
    $pdo = new PDO("mysql:host=xxxxx;dbname=xxxxxxxx", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    ?>

一旦你这样做了,那么在任何具有数据库交互(查询)的页面的顶部,你就可以这样做:

require ('..................(w/e 你的路径是)/connect.php');

现在您可以使用单个变量 ($upload_count) 对查询中的所有行进行计数,请执行以下操作:

// get the differences in the counts
    $unlock2 = 5 - $upload_count;
    $unlock3 = 20 - $upload_count;
    $unlock4 = 50 - $upload_count;
    $unlock5 = 100 - $upload_count;
    $unlock6 = 200 - $upload_count;

现在您说问题是如果返回的结果为空,或者如果结果为 0 / 无结果,则如何回显数据库内容。

然后就做`

 if($upload_count == 0) { // This is empty 
       .................... 
     } else { // This is not empty have fun :D
       ........... 
     }

我想我回答了你的问题......如果不让我知道。

于 2013-04-17T02:09:57.827 回答
-2

我相信如果 mysql 计数返回空而不是 0

试试这个

 if (empty($uploads) === true) {
echo "Upload a resource to share in order to unlock your novice uploader badge!<br/>

     <img src=\"images/badges/badge_e_share_1_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" />";
}
于 2013-04-17T01:31:30.337 回答