SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 variables into "foreach"?!

    I'm creating this an array like this:
    PHP Code:
    $response = array();

    $sql mysql_query("SELECT * FROM users");
    $names = array();
    $currUser = array();
    while(
    $row mysql_fetch_array($sql)){
        
        
    $names[$row['userid']] = $row['fname'].' '.$row['lname']; 
    }

    and are putting this into a foreach:
    PHP Code:
    foreach ($names as $i => $name)
    {
        
    $response[] = array($i$namenull'<img src="profileimages/'.$row['userid'].'.jpg" width="25px" height="25px"/> ' $name);

    The problem is that I don't get the "$row['userid']" into the foreach... How do I get that? Actually it would be great if I could change the "$i" to the userid...

    Thanks in advance :-)

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The $i IS the userid. Did you check its values?
    Just substitute $row['userid'] in the foreach with $i and it should work.

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you do it in just one loop?
    PHP Code:
    $response = array;
    $sql mysql_query("SELECT * FROM users");
    while(
    $row mysql_fetch_array($sql)){
      
    $response[] = array($row['userid'], $namenull'<img src="profileimages/'.$row['userid'].'.jpg" width="25px" height="25px"/> ' $row['fname'].' '.$row['lname']);

    or whatever?

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    The $i IS the userid. Did you check its values?
    Just substitute $row['userid'] in the foreach with $i and it should work.
    The $i is not the userid... It is a id from 1 -> ??? and the userid does not match it... hmmm What to do?

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    why don't you do it in just one loop?
    PHP Code:
    $response = array;
    $sql mysql_query("SELECT * FROM users");
    while(
    $row mysql_fetch_array($sql)){
      
    $response[] = array($row['userid'], $namenull'<img src="profileimages/'.$row['userid'].'.jpg" width="25px" height="25px"/> ' $row['fname'].' '.$row['lname']);

    or whatever?
    This part won't work due some other part of the script... (javascript)

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    This
    PHP Code:
    $names[$row['userid']] = $row['fname'].' '.$row['lname']; 
    uses the userid as key for the array.
    So this
    PHP Code:
    foreach ($names as $i => $name
    should give the key in $i, and the value in $name. Since you set the key to be the userid, $i should contain the userid. Or do you do some other things with the $names array between the little pieces of code you posted?

  7. #7
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This part won't work due some other part of the script... (javascript)
    that's same mistake as with $i

  8. #8
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Or do you do some other things with the $names array between the little pieces of code you posted?
    Ahhh... Missed a little bit...

    sort($names);

    Removed this and everything works fine... Thanks :-)

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    New Jersey, USA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use asort to sort your array and maintain the index.
    http://us.php.net/manual/en/function.asort.php


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
  •