SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $row1["$row2["UserID"]"] or something to that effect

    I am obtaining a user ID from one table and displaying the user name that is associated with it from another table. I atempted this below, but the problem is $row1["$row2["UserID"]"]. How do I do this properly?
    PHP Code:
    $result1 = @mysql_query("SELECT * FROM Sessions WHERE Page=$ID);

    $result2 = @mysql_query("SELECT * FROM Users WHERE UserID=$UserID);

    // Determine if another user is editing this page
    while ( $row1 mysql_fetch_array($result1) AND $row2 mysql_fetch_array($result2) ) {
    if (
    $row["UserID"]==$UserID) {
    }
    // If one or more session logs are found, display the User Name
    else {
    echo(
    "<p /><center><table bgcolor=\"#FFFFFF\" border=\"2\" cellspacing=\"0\" cellpadding=\"5\" width=\"90%\" bordercolor=\"red\"><tr><td><font size=\"2\"><b><font color=\"red\" size=\"3\">NOTICE:</font> The User " $row1["$row2["UserID"]"] . " is currently editing this page</b></tr></td></table></center><p />" );
    }

    Thanks in advance,
    David

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off the top of my head, this seems like the best solution, a transition variable:
    PHP Code:
    $var $row2['UserID'];
    echo 
    $row1[$var]; 
    Note the use of quotes by the way. Single quotes around the string (['UserID']), no quotes around the variable ([$var]).

    I made a couple attempts at getting $this[$that['theOther']] syntax to work, but to no avail.
    Perhaps someone else knows how to, but to be honest, it is so convoluted-looking that I'm not really interested in looking it up.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just delete your first set of doubles so it looks like this:

    PHP Code:
    row1[$row2["UserID"]] 

  4. #4
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $row1[$row2]['UserID']. The way pasted above is incorrect. Samsm's workaround will do in a pinch if it gets too unwieldy.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  5. #5
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what incorrect about it? besides the missing $ for $row1?

  6. #6
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    You can use { or } to use arrays inside strings. Eg
    PHP Code:
    <?php
    $row1
    ["{$row['UserID']}"];
    ?>

  7. #7
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pretty sure that Jgreens is right (with the extra $ )
    PHP Code:
    <?
    $row1
    [$row2['UserID']];
    ?>
    with $variable='blah';
    theres no point quoting $array[$variable];
    as you can't represent the constant 'blah' via $variable

    $array[variable]; && $array['variable'];
    are 2 seperate variables

    wheras AFIK
    $array[$variable]; && $array["$variable"];
    can only ever represent the same thing ?

  8. #8
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well I know that syntactically its ok, cause I do it all the time, not sure if it will work logically for dcbraund though, I don't have enough of their code....

  9. #9
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgreen
    what incorrect about it? besides the missing $ for $row1?
    It's not documented anywhere, as far as I know. See all code listings here.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  10. #10
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From your link:
    Example 7-6. Collection

    <?php
    foreach ($colors as $key => $color) {
    // won't work:
    //$color = strtoupper($color);

    // works:
    $colors[$key] = strtoupper($color);
    }
    print_r($colors);

    /* output:
    Array
    (
    [0] => RED
    [1] => BLUE
    [2] => GREEN
    [3] => YELLOW
    )
    */
    ?>
    Notice the $colors[$keys] ...

    Suggesting one can use a variable as a key. The only difference is I have a special variable, an array.

    Then in the use comments below:

    wmoranATpotentialtechDOTcom
    29-Nov-2002 04:10
    Dereferencing arrays takes some time, but is not terribly expensive.
    I wrote two dummy loops to test performance:
    for ($i =0; $i < count($a); $i++) {
    $x = $a[$b[$i]];
    $y = $a[$b[$i]];
    $z = $a[$b[$i]];
    }
    for ($i =0; $i < count($a); $i++) {
    $q = $b[$i];
    $x = $a[$q];
    $y = $a[$q];
    $z = $a[$q];
    }

    The first loop is 6.5% slower than the second. Meaning that dereferencing arrays is not terribly expensive, unless you do it a whole lot. I would expect that each extra reference costs about 3% in speed. The lesson is that if you're going to be using a specific value in an array for a number of operations, you can gain a little speed by assigning it to a temp variable (or creating a reference with $q = &$b[$i]) but it's not worth getting crazy over.
    I tested this with iterations of 10,000 and 100,000 on php 4.2 and the results were consistent.
    So you see, it is indeed documented. But that's ok, now we both know for sure that its correct.

  11. #11
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never contested you could use variables as keys.

    As per multidimensional array syntax, user comments != documentation.

    I will concede that it seems like it works
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  12. #12
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    agreed

    FYI, I was glad you contested this, I knew it worked, but had never actually researched it.

    good show

  13. #13
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help, it's working great now, but I have another problem. I need to echo a response from a table even if it is empty. This has to be the easiest thing in the world - but I can't figure it out!

    Here's my code:
    PHP Code:
     
      $result 
    = @mysql_query("SELECT * FROM Sessions WHERE Page=$ID AND UserID=$UserID");
        if (!
    $result) {
        echo(
    "Unable to retrieve Session Log: " mysql_error() . "");
        exit();
      }
      
    // Determine if a session already exists
      
    while ( $row mysql_fetch_array($result) ) {
      if (
    $row["UserID"]=$UserID) {
      echo (
    "A session log exists");
      } 
      
    // Determine if a session does not exist
      
    else {
      echo (
    "A session log does not exist");
      } 
    It works fine when the table is occupied (echo's "A session log exists"), but displays nothing on an empty table.

    HELP!

    Thanks again,
    David

  14. #14
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your code:
    PHP Code:
      $result = @mysql_query("SELECT * FROM Sessions WHERE Page=$ID AND UserID=$UserID");
        if (!
    $result) {
        echo(
    "Unable to retrieve Session Log: " mysql_error() . "");
        exit();
      }
      
    // Determine if a session already exists
      
    while ( $row mysql_fetch_array($result) ) {
      if (
    $row["UserID"]=$UserID) {
      echo (
    "A session log exists");
      } 
      
    // Determine if a session does not exist
      
    else {
      echo (
    "A session log does not exist");
      } 
    my code:
    PHP Code:
      $result = @mysql_query("SELECT * FROM Sessions WHERE Page=$ID AND UserID=$UserID");


        if (
    mysql_num_rows($result)<1) {
        echo(
    "Unable to retrieve Session Log: " mysql_error() . "");
        exit();
      }



      
    // Determine if a session already exists
      
    while ( $row mysql_fetch_array($result) ) {
      if (
    $row["UserID"]=$UserID) {
      echo (
    "A session log exists");
      } 
      
    // Determine if a session does not exist
      
    else {
      echo (
    "A session log does not exist");
      } 
    mysql_query always returns something, so $result always gets set. If there was an error is gets false. This doesn't tell you anything about the number of rows. If you get nothing back (0 rows) you have an empty table.

    Also on an empty return, your fetch_array won't work and the while will escape immediately. So you don't get to the if else.

  15. #15
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually this is probably better:

    PHP Code:
      $result = @mysql_query("SELECT * FROM Sessions WHERE Page=$ID AND UserID=$UserID");
        if (!
    $result) {
        echo(
    "Unable to retrieve Session Log: " mysql_error() . "");
        exit();
      }
      
      
    // Determine if a session already exists
      
    if (mysql_num_rows($result)>0){
        while ( 
    $row mysql_fetch_array($result) ) {
          if (
    $row["UserID"]=$UserID) {
            echo (
    "A session log exists");
          } 
        }
      }
      
    // Determine if a session does not exist
      
    else {
      echo (
    "A session log does not exist");
      } 
    Even an empty result set return a true for if (!$result) since the sql did run. But there are no rows in the return, this means its a valid query with no results.

  16. #16
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jgreen...your fast
    That code is working great - but while we're at it, I have another one for you all. I have a collumn named ID in all of my MySQL tables set as primary key & auto increment. How do I make MySQL re-use deleted numbers rather than increasing by 1 each time. With all my testing I'm getting some rather large ID numbers that, if re-used, should be a lot lower. I have already tried disabling the auto increment with no luck.

    You guys are great,
    David

  17. #17
    SitePoint Zealot jgreen's Avatar
    Join Date
    Apr 2003
    Location
    everywhere and nowhere
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's kind of tricky, especially if you use that ID as a refence in any other tables. ie. tbl_permissions contains id from tbl_user.

    You can reset or even set auto_increment with mysql, but I'm not sure if it will just advance to the next un used id or not...


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
  •