SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Arrrgh - I need help! In a mysql_query, I'm stripping certain items from an array, and now the ID:s of the array looks like this:

    ID | Item
    --------------
    1 | Apple
    3 | Orange
    7 | Cheese
    9 | Bulldozer

    However, this is no good. Due to a javascript that interprets the output, the items must be numbered one number after another i.e. 0,1,2,3,4,5,6,7,8,9. I guess this can be done in two ways:

    1. Changing the ID to be 0-9:
    ID | Item
    --------------
    0 | Apple
    1 | Orange
    2 | Cheese
    3 | Bulldozer

    OR

    2. Adding another column with 0,1,2 numbering:

    ID2| ID | Item
    ---|--------------
    0 | 1 | Apple
    1 | 3 | Orange
    2 | 7 | Cheese
    3 | 9 | Bulldozer

    Both ways are okay as long as i can somehow number the output 0,1,2,3.... BUT HOW CAN IT BE DONE? I do nat have a clue!

    Please help, I'm totally stuck.

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $num 0;

      while (
    $row mysql_fetch_array($result)) {
        
    $id $row["id"];

        if (
    $id != $num) {
          
    $id $num;
          
    // Code to update the database with the new $id value.
        
    }
      
    $num++;
      } 
    I may be WAY off - I may not understand what it is you're trying to accomplish, or how often - this would require a query two queries to update each record - if this is a one-time deal then it might be okay.

    Could you explain to us why it needs to be the way you mentioned, and what goes wrong if it's not? Thanks!

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I agree -- it sounds like it would be less trouble to adjust your JavaScript to handle gaps in the ID list than it would to constantly update your database to keep the ID's continuous.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are trying to do what I think you are trying to do, then this should serve as a basis to give you wha tyou need.

    <?
    $dude = array("a", "b", "c", "a", "b", "d", "b", "f", "g", "a", "b");
    unset($dude[3]);
    unset($dude[9]);
    unset($dude[1]);
    unset($dude[5]);

    $i = 0;
    foreach($dude as $key => $val) {
    $newdude[$i] = $val;
    $i++;
    }

    foreach($newdude as $key => $val) {
    print $key ." = ". $val ."<br>";
    }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the responses! I'm not sure if they can accomplish what I'm looking for (I'll analyze them later)
    I'll try to clarify what I want to do.

    The JavaScript I'm talking about is a menu, with muliple subcategories. I agree that it would be easier to make the javascript do this, but I know very, very little javascript, and the menu script is not mine.

    This is how the script looks (very simplified):

    while ($row = mysql_fetch_array ($result)) {
    echo "oMenu.makeSub($row[0])";
    }

    This will generate a portion of the menu, and it has to be done about 10-15 times to generate the whole menu. Also, the database MUST NOT BE ALTERED - it must remain intact. I must keep the change to the array. If I'm unclear, I'd be happy to clarify.
    Last edited by M. Johansson; Feb 20, 2001 at 14:38.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi truelight, I am not sure of the exact context of what you are trying to do. However, if I understand the logic of what you want to do, here are my thoughts.

    You have an ordered list:

    Item
    -----------
    Apple
    Orange
    Cheese
    Bulldozer

    Just completely ignore the ID column as it doesn't contain the data you need.

    Now you want to index the list starting from 0 incrementing by 1. Well that's simple. Start at the begining of the list. Initialise a counter to 0 ( $x = 0 ). Loop through the list and increment the counter each time.

    If I understand your intention,
    PHP Code:
    while ($row mysql_fetch_array ($result)) { 
    echo 
    "oMenu.makeSub($row[0])"

    will result in the following output:

    oMenu.makeSub(0)
    oMenu.makeSub(1)
    oMenu.makeSub(2)
    etc

    If this is the case then you can achieve it with this logic
    PHP Code:
    $x 0;
    while (
    $row mysql_fetch_Array($result) {
       echo 
    "oMenu.makeSub($x++)";

    $x++ post increments the value of $x. That is, the present value of $x will be used and *then* the value of $x will be incremented by 1.


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
  •