www.webdeveloper.com
Results 1 to 11 of 11

Thread: Selecting position in the array

  1. #1
    Join Date
    Jan 2005
    Posts
    354

    Question Selecting position in the array

    Hi all
    I'm trying to figure out how can I get my next position in the array
    Lets say I have array

    red
    green
    blue
    grey
    orange

    nowsay we have variable value "blue"

    Next I want to do something to see that after blue we have grey and returan value of grey (basically get the value that comes after the variable)

    Any ideas how to do that?

    Tnx
    ------
    What am I doing?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    PHP Code:
    $color 'blue'
    $ix = (array_search($color$array))
    {
       if(
    $ix !== FALSE)
       {
          if(
    array_key_exists($ix 1$array))
          {
             echo 
    "Next after $color is: " $array[$ix 1];
          }
          else 
          {
             echo 
    "There is no color after $color.";
          }
       }
       else
       {
          echo 
    "There is no color $color in the array";
       }

    Note: this assumes an array indexed with sequential numbers. If it is not, then you could probably do it with a foreach loop and the next() function.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2005
    Posts
    354
    ok thanx, let me see how it works!
    ------
    What am I doing?

  4. #4
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    Allows for both arrays indexed with sequential numbers and with defined keys:
    PHP Code:
    $color 'blue';
    if (
    in_array($color$array))
    {
        while (
    current($array) != $color)
        {
            
    next($array);
        }
    }
    else 

      echo 
    "There is no color $color in the array"


    echo 
    "Next after $color is: " next($array); 

  5. #5
    Join Date
    Jan 2005
    Posts
    354
    what does it mean indexed array?
    my array is what i get from MySQL.
    ------
    What am I doing?

  6. #6
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    What function (mysql_fetch_assoc, mysql_fetch_array, etc.) are you using to fetch your array from your MySQL Database?

  7. #7
    Join Date
    Jan 2005
    Posts
    354
    mysql_fetch_array
    ------
    What am I doing?

  8. #8
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    and are you passing a second argument like (MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH)?

  9. #9
    Join Date
    Jan 2005
    Posts
    354
    here is how i do get info for the array, im think im lost now

    PHP Code:
    $sql_next "select id, page_name from tp_pages WHERE group_='GeneralPlanning'";
    $result_next mysql_query($sql_next); 
    $row_next mysql_fetch_array($result_next
    ------
    What am I doing?

  10. #10
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    It's probably going to get really confusing if I try to explain what your current code is doing, I'd suggest using replacing your current code with the following:
    PHP Code:
    $sql_next "select id, page_name from tp_pages WHERE group_='GeneralPlanning'"
    $result_next mysql_query($sql_next); 
    $row_next mysql_fetch_array($result_nextMYSQL_NUM); 
    And now you may use either mine or NogDog's code to show the next element in the array result.

  11. #11
    Join Date
    Jan 2005
    Posts
    354
    well it seams that I got real confused now one way or another.
    I'm getting something wrong here. the script gives me the same info as the curent row, and not the next row. also if my row has id, page_name im getting page name outputed insted of id, plus its is the page name of curent row id.

    So say actuall data look like this:
    45, Fin Data
    46, Address

    when I set curent row as 45, the next row outut says Fin data, instead of 46

    PHP Code:
    $sql_next "select id, page_name from tp_pages WHERE group_='GeneralPlanning'"
    $result_next mysql_query($sql_next); 
    $row_next mysql_fetch_array($result_nextMYSQL_NUM); 
    ///     
    $color '45'
    if (
    in_array($color$row_next)){ 
        while (
    current($row_next) != $color){ 
            
    next($row_next); 
        } 
    }else{ 
        echo 
    "<br>There is no color $color in the array"


    echo 
    "<br>Next after $color is: <b>" next($row_next) ."</b>"
    ------
    What am I doing?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles