Need help getting info from mysql and using IF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icecandy
Forum Newbie
Posts: 5
Joined: Fri May 22, 2009 4:57 pm

Need help getting info from mysql and using IF

Post by icecandy »

alright, im very new to php. I have been using ASP vb and figured it was time to upgrade my knowledge.

I am using the phpmotion script. What im trying to do is get the script to read from the databse if the user is an admin and if they are to change the $badge to a certain image, how the script has it coded is to add up all the video, image, music uploads together and depending on the value will determine which badge they get.

can anyone help with this please? here is some code from the script below

Code: Select all

$sql = "SELECT user_group, user_name FROM member_profile WHERE user_id = '$member_id'";

Code: Select all

 
$member_site_total = ( $member_videos + $member_audios + $member_albums + $member_blogs );
 
    switch ( $member_site_total ) {
 
        case $member_site_total >= 150:
            $badge = 'badge_4.png';
        break;
 
        case $member_site_total >= 50:
            $badge = 'badge_3.png';
        break;
 
        case $member_site_total >= 25:
            $badge = 'badge_2.png';
        break;
 
        case $member_site_total >= 0:
            $badge = 'badge_1.png';
        break;
        
        
    }
 
mysql_free_result($result);
return $badge;
}
 
I just want to add an if statement in there some how so if its the admin it will have $badge = badge_admin.png

thanks in advance to everyone
Last edited by Benjamin on Fri May 22, 2009 6:54 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need help getting info from mysql and using IF

Post by califdon »

How is the admin defined?
icecandy
Forum Newbie
Posts: 5
Joined: Fri May 22, 2009 4:57 pm

Re: Need help getting info from mysql and using IF

Post by icecandy »

califdon wrote:How is the admin defined?
in the column user_group each row is defined with either admin or member as the value
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need help getting info from mysql and using IF

Post by califdon »

icecandy wrote:
califdon wrote:How is the admin defined?
in the column user_group each row is defined with either admin or member as the value
So your logic is as follows?
If user is an admin, that determines the badge.
If user is not an admin, the badge depends on the total of some numbers.
How about:

Code: Select all

if ($user_group == "admin") (
    $badge = "badge_admin.png";
} else {
    switch ( $member_site_total ) {
        ...
        ...
   }
}
icecandy
Forum Newbie
Posts: 5
Joined: Fri May 22, 2009 4:57 pm

Re: Need help getting info from mysql and using IF

Post by icecandy »

yeah thats what i thought it would be but when i add that nothing on my website will load, because the badge function is in a functions file that is loaded throughout the site this is what i have did i do somethign wrong?

Code: Select all

 
 // get admin badge
      $sql = "SELECT user_group FROM member_profile WHERE user_id = '$user_id'";
      $user_group = mysql_query($sql);
 
      
 
 
      $member_site_total = ( $member_videos + $member_audios + $member_albums + $member_blogs );
      
        if ($user_group == "admin") (
        $badge = "admin_badge.png";
        } else {
        switch ( $member_site_total ) {
 
        case $member_site_total >= 150:
            $badge = 'badge_4.png';
        break;
 
        case $member_site_total >= 50:
            $badge = 'badge_3.png';
        break;
 
        case $member_site_total >= 25:
            $badge = 'badge_2.png';
        break;
 
        case $member_site_total >= 0:
            $badge = 'badge_1.png';
        break;
        
        
    }
 
 
mysql_free_result($result);
 
 
 
return $badge;
 
}
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need help getting info from mysql and using IF

Post by califdon »

Yes, you're missing the mysql command to fetch the row. What you have called $user_group is the result of the mysql_query() command, which means it's the pointer to the recordset that, in your case, is an array that has one row, but generally could contain thousands of rows, each with dozens of fields! You must use one of the mysql_fetch_xxxx() commands to obtain one row at a time, then you must either select the field as an array element or use the php function extract() to convert the field data into php variables. It can be done in various ways, but what I would probably do is:

Code: Select all

...
  $sql = "SELECT user_group FROM member_profile WHERE user_id = '$user_id'";
  $result = mysql_query($sql) or die(mysql_error());
  $row = mysql_fetch_array($result);  // this fetches one row of $result
  $user_group = $row['user_group'];
  ...
icecandy
Forum Newbie
Posts: 5
Joined: Fri May 22, 2009 4:57 pm

Re: Need help getting info from mysql and using IF

Post by icecandy »

THANK YOU VERY MUCH!!!
Post Reply