SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: undefined index

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    undefined index

    why would this code function the way I want but say the $status is undefined?
    PHP Code:
    $res mysql_query("SELECT * FROM members where username = '$user'");
                while (
    $row=mysql_fetch_array($res)) {
                
    $status $row['status'];
                if (
    $status == 1) {
                echo 
    "<div align='left'>You are a site admin.</div>";
                
    $res mysql_query("SELECT * FROM pcourse");
        
    $num_rows mysql_num_rows($res); {
        if (
    $num_rows == 1
            echo 
    "There is $num_rows pending course. <a href='pending.php'>View</a>"
        elseif (
    $num_rows == 0
            echo 
    "There are $num_rows courses pending. <a href='pending.php'>View</a>"
        elseif (
    $num_rows 1
            echo 
    "There are $num_rows pending courses. <a href='pending.php'>View</a>"; }}}; 

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    When your grabbing a result set from MySQL (or whatever db server) set up the array variable before the start of the while loop, using the code in your post as an example, between the line where the query is run and the first line of the while loop you need to add:

    PHP Code:
    $status=array(); 
    That would set up status as an empty array ready to have the result set added to it.

    From the looks of your code you might not actually need an array. If all you need is to look up a member and see what status (user group?) they're in then you only need to get the status of the user concerned:

    PHP Code:
    $query "
        SELECT
            status
        FROM
            members
        WHERE
            username = '
    $user'
    "
    ;
    $res mysql_query($query);

    $users_matched mysql_num_rows($res);

    if ( 
    $users_matched <> ) {
        
    // Handle error state of no match for the username
        
    echo "ERROR: Unable to located $user in the database!";
        die;
    }

    $row mysql_fetch_assoc($res);
    $status $row['status'];
    if (
    $status === 1) {
        echo 
    "<div align='left'>You are a site admin.</div>";
        
    $res mysql_query("SELECT * FROM pcourse");
        
    $num_rows mysql_num_rows($res);
        if (
    $num_rows == 1) {
            echo 
    "There is $num_rows pending course. <a href='pending.php'>View</a>";
        } elseif (
    $num_rows == 0) {
            echo 
    "There are $num_rows courses pending. <a href='pending.php'>View</a>";
        } elseif (
    $num_rows 1) {
            echo 
    "There are $num_rows pending courses. <a href='pending.php'>View</a>";
        }

    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks spacePhoenix..That worked and yes the status is the (user group) either 0 or 1. The default status is 0 when a user registers if I change their status to 1 I am allowing that user to edit courses and approve/deny user submitted courses. On the pages where a member must be a admin I am using this if statement
    PHP Code:
    $res mysql_query("SELECT * FROM members where username = '$user'");
                while (
    $row=mysql_fetch_array($res)) {
                
    $status $row['status'];};
                if (
    $status == 1) {// code and form that needs approved }
    else { 
                
                echo 
    "You can not make changes to the course. You are being redirected back.";
                
    header("Refresh: 8; url='\condition\index.php'");}; 
    Is that a good method to use? Or would it be better to just start another session like user when they log in?
    Last edited by dxm31; Oct 3, 2011 at 00:53. Reason: Added another Question

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Where is the value for user coming from? If it's coming from the $_SESSION array then possibly you could store the value of status as a session variable then grab the value of status from the session instead, remembering to update the value of status in the session if the user is promoted to an admin or is an admin who is demoted to a member.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •