[PHP] user account type

Discussion in 'PHP' started by walidsu, Nov 4, 2012.

  1. #1
    Hi, everyone

    i have some problem getting the user account type from db
    i create a row on db called acc_type, it checks if user acc_type is 1 or greater highlight the user name.

    i wrote this code but it highlights all the username as red (admin)

    colors meaning:
    Blue = Regular member = 0
    Red = admin = 1
    Green = Mod = 2
    Orange = Super User = 3
    
    <?php
    $req = mysql_query('select acc_type from users where id="'.$id.'"');while($dn3 = mysql_fetch_array($req)){?><?php if(isset($dn3) && $dn3['acc_type'] == 1) { ?>                    <style>                        .acc{                            color:red;                            font-weight:bold;                        }                    </style>                    <?php } ?>                    <?php if(isset($dn3) && $dn3['acc_type'] == 2) { ?>                    <style>                        .acc{                            color:green;                            font-weight:bold;                        }                    </style>                    <?php } ?>                    <?php if(isset($dn3) && $dn3['acc_type'] == 3) { ?>                        <style>                        .acc{                            color:#FFA500;                            font-weight:bold;                        }                    </style>                    <?php } ?>                <?}?>    <tr>        <td class="forum_tops"><a href="read_topic.php?id=<?php echo $dnn2['id']; ?>"><?php echo htmlentities($dnn2['title'], ENT_QUOTES, 'UTF-8'); ?></a></td>        <td><a href="profile.php?id=<?php echo $dnn2['authorid']; ?>"><span style="" class="acc"><?php echo htmlentities($dnn2['author'], ENT_QUOTES, 'UTF-8');  ?></span></a></td>        <td><?php echo $dnn2['replies']; ?></td>
    PHP:

    Thanks
     
    walidsu, Nov 4, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    I hope you mean a field (column), not a record (row). You can't name records.

    Ignoring the fact that $dnn2 never gets any data (I assume that was done before any of this code):

    
    while($dn3 = mysql_fetch_array($req)){
    if(isset($dn3))
    
    PHP:
    is redundant. If $dn3 isn't set, it's not "while $dn3", so you wouldn't be there.
     
    Rukbat, Nov 5, 2012 IP
  3. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    didn't work

    however the page have while $dnn2 before while $dn3 , means its have 2 while.

    <?php
    while($dnn2 = mysql_fetch_array($dn2))
    {
    ?>


    this works on users profile :
    <?php
    $req = mysql_query('select acc_type from users Where id="'.$id.'"');
    while($dnn2 = mysql_fetch_array($req))
    {
    ?>
     
    walidsu, Nov 5, 2012 IP
  4. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #4
    ok now im getting the users account number, exp 0 = member , 1 = admin, 2 = mod, but the if statement is not working:

    
    if (isset($req) && $req['acc_type'] == 0) {echo "<style>        .acc{            color:blue;            font-weight:bold;                    }        </style>";        }        elseif (isset($req) && $req['acc_type'] == 1){echo "<style>        .acc{            color:red;            font-weight:bold;                    }        </style>";        }        elseif (isset($req) && $req['acc_type'] == 2){        echo "<style>        .acc{            color:green;            font-weight:bold;                    }        </style>";        }
    ?>
    PHP:

    or view the code here http://pastebin.com/CuXPAkqu





    .
     
    walidsu, Nov 7, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    What data type is acc_type?
     
    Rukbat, Nov 7, 2012 IP
  6. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #6
    the data type is Int

    img http://oi46.tinypic.com/avpmyv.jpg


    as you can seen in the code

    if $acc_type = 1 then its admin

    0 is regular member
    1 is admin
    2 is mod
     

    Attached Files:

    Last edited: Nov 7, 2012
    walidsu, Nov 7, 2012 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Try this:
    
    error_reporting(32767);
    echo '"'.$req.'"';
    if (isset($req) && $req['acc_type'] == 0) {
    
    
    Code (markup):
    What do you get? It should be "0" or "1" or some number in quotes. And do you get any error? (If $req isn't set you'll get a fatal error.)
     
    Rukbat, Nov 7, 2012 IP
  8. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #8
    it should be 0 means regular user, and i changed acc_type number to 1 in db 1 means admin, and for another user its acc_type 2 = mod.

    see img: http://oi48.tinypic.com/fvwznn.jpg
     
    walidsu, Nov 7, 2012 IP
  9. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #9
    Try putting the numbers in single quotes. It defaults to red because it is validating as TRUE and not the number 1. Perhaps the db column is not numeric.

    if($dn3['acc_type'] == '1') {

    not

    if($dn3['acc_type'] == 1) {
     
    DomainerHelper, Nov 7, 2012 IP
  10. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #10
    it changes all username color to green

    code:
    if($dn2['acc_type']== '1'){
    echo '<style>.acc{color:red;font-weight:bold;}</style>';
    }
    else{
    echo '<style>.acc{color:green;font-weight:bold;}</style>';
    }
    ?>

    <span class="acc"><?php echo $dnn2['author']; ?></span>

    im thinking there is something wrong in selecting from db/row

    $req = mysql_query('select acc_type from users where id="'.$id.'"');
    PHP:

    ..
    .
     
    walidsu, Nov 7, 2012 IP
  11. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #11
    Never mind guys i fixed, there was nothing wrong with the php code, it was the css problem.

    but thanks for trying to help
     
    walidsu, Nov 8, 2012 IP