Page 1 of 1

Need help getting info from mysql and using IF

Posted: Fri May 22, 2009 5:06 pm
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

Re: Need help getting info from mysql and using IF

Posted: Fri May 22, 2009 7:02 pm
by califdon
How is the admin defined?

Re: Need help getting info from mysql and using IF

Posted: Fri May 22, 2009 8:06 pm
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

Re: Need help getting info from mysql and using IF

Posted: Fri May 22, 2009 8:28 pm
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 ) {
        ...
        ...
   }
}

Re: Need help getting info from mysql and using IF

Posted: Fri May 22, 2009 9:07 pm
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;
 
}
 

Re: Need help getting info from mysql and using IF

Posted: Sat May 23, 2009 12:21 pm
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'];
  ...

Re: Need help getting info from mysql and using IF

Posted: Sat May 23, 2009 3:53 pm
by icecandy
THANK YOU VERY MUCH!!!