SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Users edit multiple cats...

    Ok, I store all the catids the user is allowed to edit, in the user table, under there username in the column 'catid'

    they are listed like:

    1,3,6

    so I wrote this:

    PHP Code:
                    if($_SESSION['user_level'] == 2) {
                        echo(
    "<p><span><a href='".$this->getUrl()."admin/edit.php?id=".$row['id']."'>Edit</a> | <a href='".$this->getUrl()."admin/deletenews.php?id=".$row['id']."'>Delete</a></span></p>");
                    } elseif(
    $_SESSION['user_level'] == 1) {
                        
    $username $_SESSION['username'];
                        
    $getuserinfo mysql_query("SELECT * FROM users WHERE username='$username'");
                        
    $inforow mysql_fetch_array($getuserinfo);
                        
    $catid_array explode(','$inforow['catid']);
                        foreach(
    $catid_array as $allowed_cats) {
                            
    $getcat mysql_query("SELECT * FROM cats WHERE id='$catid_array'");
                            
    $getcat_array mysql_fetch_array($getcat);
                            
    $catname $getcat_array['id'];
                            
    $edit_array=explode(",",$_SESSION['catids']);
                            if (
    in_array$catname$edit_array))
                            { 
                                echo(
    "<p><span><a href='".$this->getUrl()."user/editnews.php?id=".$row['id']."'>Edit</a></span></p>"); 
                            }
                        } 
    The user_level == 1 is the part we're interested in... the level == 2 works great...

    for some reason, it never shows the edit button there... I'm thinking its the:
    if(in_array($catname, $edit_array))

    line, because $catname isn't an array... but its checking it...

    but then again, I'm not perfectly sure on how in_array works...

    EDIT: I just found out about in_array... and theoretically it should work...

    any help?
    thanks,
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  2. #2
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I got it to show the edit button... but it shows 3 times... In my foreach statement... the very first query in there... I was using $catid_array instead of $allowed_cats ($allowed_cats is the as part...)

    So my new elseif part is:

    PHP Code:
                    } elseif($_SESSION['user_level'] == 1) {
                        
    $username $_SESSION['username'];
                        
    $getuserinfo mysql_query("SELECT * FROM users WHERE username='$username'");
                        
    $inforow mysql_fetch_array($getuserinfo);
                        
    $catid_array explode(",",$inforow['catid']);
                        foreach(
    $catid_array as $allowed_cats) {
                            
    $getcat mysql_query("SELECT * FROM cats WHERE id='$allowed_cats'");
                            
    $getcat_array mysql_fetch_array($getcat);
                            
    $catname $getcat_array['id'];
                            
    $edit_array=explode(",",$_SESSION['catids']);
                            if (
    in_array$catname$edit_array))
                            { 
                                echo(
    "<p><span><a href='".$this->getUrl()."user/editnews.php?id=".$row['id']."'>Edit</a></span></p>"); 
                            }
                        }
                    } 
    But it echos edit 3 times... how do I stop that?
    it also shows edit on EVERY page... not just the ones they're allowed to edit...

    ok, I added an echo to the catname... so now I get:

    About
    Edit

    Portfolio
    Edit

    School
    Edit

    On every page...

    I just took the if statement out of the foreach statement... and now it only displays edit once... yet it still shows on every page =\
    Last edited by jabird; Sep 6, 2005 at 14:47.
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  3. #3
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you should really be using a lookup table to determine which catid's each user has access to.

    Consider a table below

    lookup_t
    userid
    catid

    now as you go through printing out each cat, all you need do is check to make sure there is an entry in lookup_t theat contians userid = $_SESSION['username'] AND catid = cat being printed, if so you can enable the edit link to be printed, if not you don't print the link.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  4. #4
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but that doesn't make since to me...

    I create another table...

    this table holds the userid, and the catid's they can edit?

    why do this, when I already have the catids in the user table...

    OHHH nevermind... you're saying:

    For each cat they can edit, put a new row in that table, with the catid... and the userid is there id... I understand now
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  5. #5
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I made it this:

    PHP Code:
                        $author_username $authorrow['username'];
                        
    $getauthor_cats mysql_query("SELECT * FROM lookup_t WHERE userid='$author_username'");
                        
    $getauthorcats_row mysql_fetch_array($getauthor_cats);
                        
    $authorcats_row[] = $getauthorcats_row;
                        foreach(
    $getauthorcats_row as $cats_row) {
                            
    $category $cats_row['catid'];
                            echo(
    $category);
                        } 
    just as a test... that echos:

    tt11....

    t is the first letter in the persons username...

    he can edit cats 1 and 2... yet it is only echoing 1, but echoing twice...

    how in the world can I fix this stuff? its about to drive me mad!!!!

    if I have it echo just $cats_row... it says:
    testtest11
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jabird
    For each cat they can edit, put a new row in that table, with the catid... and the userid is there id... I understand now
    exactly

    Then you can ditch all the array coding, and (depending your version of SQL) get results from one SQL query
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the post just above your new one... That is for the manage users page, so I can see the users name/what cats they can edit and other stuff like that...
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So you want to print out all users and a list of their cats thier allowwed to change ?

    Edit:



    Please refresh me as to what you want shown, sorry its late
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes...

    I haven't written the part that checks if they can see the edit button or not yet... but I have a creeping feeling I'll need help there too...

    In the script I posted above, that should only show the id of the cat they can edit... when I get that perfected, I can easily throw in the part that gets the name...
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Do you have a layout you want to use ?

    ie if a person can change 50 cats, how do you want to handle this ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the layout all set up... all I gotta do is get it to echo the cats...

    When I get the names and all... I'll implode that array separated by commas, for a list of the names...

    EDIT: All I want is raw data... I can handle the rest...
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  12. #12
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, this is just a starter, we will need to refine it but try this on your database see what it gives you

    Code:
    SELECT * FROM users 
    INNER JOIN lookup_t ON lookup_t.user_id = users.id
    INNER JOIN cats ON lookup_t.cat_id = cats.id
    WHERE users.username = '$author_username'
    GROUP BY users.username
    See how you get on with that, if you don't get any joy, email me your database (only these 3 tables) and I'll sort something for you tomorrow, off to bed soon.

    Edit:

    forgot to say, don't plug this into your code above, we just want to get the right bits out of the database at this point, use mysqladmin or straight command line to your database if avail
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  13. #13
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with a few minor changes, that worked.... but I can't figure out how to echo the catnames...

    I did fetch array of course... and echo $getcatname_array['catid'];

    I also tried
    echo $getcatname_array['name'];


    no luck
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  14. #14
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I haven't seen the result set so can't help you, I'll set up a database and have a look
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  15. #15
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I opened PuTTy and used:

    Code:
    SELECT * FROM users 
    						INNER JOIN lookup_t ON lookup_t.userid = users.id
    						INNER JOIN cats ON lookup_t.catid = cats.id
    						WHERE users.username = 'test'
    						GROUP BY users.username
    I got an empty set...
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  16. #16
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can you send me the latest query, as you said you changed it
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  17. #17
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nearly got it leave it with me a while
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  18. #18
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Jabird

    Thanks for being so patient, try this

    PHP Code:
    <?php
    require 'dbconnect.inc.php';
    $result mysql_query("SELECT users.username, cats.id
    FROM users 
    INNER JOIN lookup_t ON lookup_t.user_id = users.id
    INNER JOIN cats ON lookup_t.cat_id = cats.id
    ORDER BY users.username;
    "
    );
     
    while (
    $row mysql_fetch_array($result)){
      
    $username $row['username'];
      
    $array[$username][] = $row['id'];
    }
    print_r ($array);
    exit;
    ?>
    What that will do is produce for you a multidim variable called $array. The first dimension will hold a users name, the second will hold the cats they have.

    Of course you don't have to use my script and put your data into the array, that was just an example to prove the data was accessable, you could use the output of the SQL to formulate your layup and send straight to the browser.

    Let me know how it goes.
    Last edited by Mandes; Sep 8, 2005 at 10:38.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


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
  •