-3

I've been working on some code to award badges to teachers based on how many items they upload to share with their colleagues and students.

I've run into a problem when the array is empty. Nothing shows up.

I tried an empty() function, but I must have put it in the wrong position, because then it started appearing for users who had uploads.

So essentially in the code below, it's the "if $uploads < 1" section that I'm having difficulty with.

..and the resultant question is how do I get a message to echo where mysql_fetch_array($upload_count) is empty?

     <?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/> ";


 }



     }  



 ?>
Dave C
  • 7
  • 2
  • 6
    So tired of saying this but **you should not be using the MySQL extension**. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Phil Apr 17 '13 at 01:23
  • so tired of reading it, so lets both take a break :-) –  Apr 17 '13 at 01:25
  • 1
    @Dagon It needs to be auto-commented. – Kermit Apr 17 '13 at 01:28
  • no. it needs to be left alone. –  Apr 17 '13 at 01:30
  • @Dagon I / we shouldn't have to say it. It's almost like people don't read the [documentation](http://www.php.net/manual/en/intro.mysql.php) anymore :( – Phil Apr 17 '13 at 01:33
  • 1
    @Phil *"Anymore?"* Are you meaning to say there was ever a time when people RTFM? – deceze Apr 17 '13 at 01:33
  • what about the other thousand bad practices, want to comment on every one on every post? –  Apr 17 '13 at 01:34
  • @Dave `var_dump($uploads)` - What's the value? Do some debugging. – deceze Apr 17 '13 at 01:34
  • 1
    @Dagon I leave that up to [Your Common Sense](http://stackoverflow.com/users/285587/your-common-sense) :) – Phil Apr 17 '13 at 01:35
  • well that wont get you very far! –  Apr 17 '13 at 01:36
  • Please read up on [SQL injection bugs](http://bobby-tables.com/) and follow the advice below on using PDO. `mysql_query` is not only deprecated and dangerous, it's being removed from PHP in the near future. – tadman Apr 17 '13 at 03:15

2 Answers2

2

Well dam.. you might as well change your variable names to

$please_inject_me, $please_inject_me1 and so on...

First lesson is to please use PDO

Now to your issue (from my understanding of your question that is):

// 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

with the following contents :

    //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);
    ?>

Once you do that then at the top of any page that has database interactions (queries) you would then just do :

require ('..................(w/e your path is)/connect.php');

Now that you have a way to count all the rows in your query with a single variable ($upload_count) do this :

// 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;

Now you said the issue was how to echo if the returned results are empty or in terms of database stuff if the results are 0 / no results.

Then just do `

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

I think I answered your questions... if not let me know.

Rixhers Ajazi
  • 1,303
  • 11
  • 18
  • Thanks a lot, I just hope I met all the guys requirements... it seems like I did (to the extent that I don't know his logic behind his work) ... oh well time will tell. I'll keep my eye open to this posting though! – Rixhers Ajazi Apr 17 '13 at 03:15
  • Thanks for your time Rixhers. Using the above code, I got an error: Fatal error: Call to a member function prepare() on a non-object in /home/myschool/public_html/admin_uploads2.php on line 15 I have since found a solution, which I'll paste in my next post. However, it doesn't involve PDO. I'll have to do a bit of research on this. Can I do an include function with PDO by the way, as I'm used to doing at the moment (include "mysql_connect.php" for example, rather than write up database details in every doc). Thank you again for your time. – Dave C Apr 17 '13 at 03:19
  • Well yes it will give errors I have not tested it or anything. Please let me know where the issues are and I will revise as I see fit. - Thanks! – Rixhers Ajazi Apr 17 '13 at 03:20
  • Sorry, please see the edited post above which states the error. Didn't mean to post before the edit. Accidentally hit enter. – Dave C Apr 17 '13 at 03:24
  • yes completely I am absolutely sorry for not explicitly stating that. Give me a minute to work on it – Rixhers Ajazi Apr 17 '13 at 03:30
  • Take a look at the update. Again this is no guarantee that it will work, but the general feel is there. – Rixhers Ajazi Apr 17 '13 at 03:43
-2

I believe that if a mysql count returns empty rather than a 0

Try this instead

 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\" />";
}
  • 3
    What is "empty"? Also, why arbitrarily stop at the first level of boolean comparison? Why not `if ((empty($uploads) === true) === true)`? – deceze Apr 17 '13 at 01:32