SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach - array not working

    For some reason, I cannot find out why this is not working...I have done similar things before, maybe my eye sight is going bad. Nothing is displaying:

    PHP Code:
    // Categories
        
    $catarray = array(
                        
    "1" => "Dogs",
                        
    "2"=>"Cats",
                        
    "3"=>"Fish",
                        
    "4"=>"Fish->Tropical",
                        
    "5"=>"Fish->Salt Water",
                        
    "6"=>"Fish->Fresh Water",
                        
    "7"=>"Birds",
                        
    "8"=>"Reptiles",
                        
    "9"=>"Small Animals",
                        
    "10"=>"Pet Insurance",
                        
    "11"=>"Pet Insurance->Pet Jewelry",
                        
    "12"=>"Pet Horoscopes",
                        
    "13"=>"Pet Names",
                        
    "14"=>"Pet Medications",
                        
    "15"=>"Exotic Pets",
                        
    "16"=>"Pet Adoption"
                        
    );

    //Populate categories and sub-categories
                
    foreach($catarray AS $key => $value) {
                    echo
                        
    '
                    <option value="'
    .$key.'">'.$value.'</option>
                        '
    ;
                } 
    Can you see what is wrong?
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    Syria
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you getting any errors, warnings or notices? I just tried the code and it worked fine for me!! What's the version of PHP you're running?

    Just a tiny piece of a advice. I think you should use arrays rather than associative arrays (hashes) for indexed values like your case. You can could add 1 to the index.
    I blog here here, and I'm happy to help.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright... works for me, maybe its because you left out "<select>"

    PHP Code:
    <?php
    // Categories
        
    $catarray = array(
                        
    "1" => "Dogs",
                        
    "2"=>"Cats",
                        
    "3"=>"Fish",
                        
    "4"=>"Fish->Tropical",
                        
    "5"=>"Fish->Salt Water",
                        
    "6"=>"Fish->Fresh Water",
                        
    "7"=>"Birds",
                        
    "8"=>"Reptiles",
                        
    "9"=>"Small Animals",
                        
    "10"=>"Pet Insurance",
                        
    "11"=>"Pet Insurance->Pet Jewelry",
                        
    "12"=>"Pet Horoscopes",
                        
    "13"=>"Pet Names",
                        
    "14"=>"Pet Medications",
                        
    "15"=>"Exotic Pets",
                        
    "16"=>"Pet Adoption"
                        
    );

    echo 
    "<select name='pets'>";
    echo 
    "<option name=''>Select category below</option>";
    //Populate categories and sub-categories
    foreach($catarray AS $key => $value) {
        echo 
    "<option value=\"$key\">$value</option>";
    }
    echo 
    "</select>";
    ?>

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    Syria
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure that the "<select>" as anything to do with it, but maybe you were seeing the output in a browser window rather than plain text output, so that's why you didn't see the <select>.

    anyway, glad it worked
    I blog here here, and I'm happy to help.

  5. #5
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had pasted just the two segments (array and foreach loop) of the page, I do indeed have the <select> tag and stuff

    I'm on localhost, I know it should work as when that was not working I tried just a simple array - foreach and it worked, not sure why this one is not working :\ I will post the full code:

    PHP Code:
    <?
        
    // /CONTROL/FUNCTIONS.PHP

    // Categories
        
    $catarray = array(
                        
    "1" => "Dogs",
                        
    "2"=>"Cats",
                        
    "3"=>"Fish",
                        
    "4"=>"Fish->Tropical",
                        
    "5"=>"Fish->Salt Water",
                        
    "6"=>"Fish->Fresh Water",
                        
    "7"=>"Birds",
                        
    "8"=>"Reptiles",
                        
    "9"=>"Small Animals",
                        
    "10"=>"Pet Insurance",
                        
    "11"=>"Pet Insurance->Pet Jewelry",
                        
    "12"=>"Pet Horoscopes",
                        
    "13"=>"Pet Names",
                        
    "14"=>"Pet Medications",
                        
    "15"=>"Exotic Pets",
                        
    "16"=>"Pet Adoption"
                        
    );

    function 
    PageContent($do) {
        if(
    $do=="front") {
            echo
                
    '
            Welcome to your Endless Pets administration panel.  Here you can add, edit, or delete items, and manage categories that those items are placed in.  Please select an option from the left to perform.  If you have any trouble, please see the <a href="index.php?do=help">help</a> section.
                '
    ;
        } elseif(
    $do=="add") {
            echo
                
    '
            <form action="index.php?do=process_new" method="post">
                Product Name: <br />
                <input type="text" name="pname" size="30"> <br />
                Product Category: <br />
                <select name="pcat">
                '
    ;
            
    //Populate categories and sub-categories
                
    foreach($catarray AS $key => $value) {
                    echo
                        
    '
                    <option value="'
    .$key.'">'.$value.'</option>
                        '
    ;
                }
            echo
                
    '
                </select> <br />
                Product Code: <br />
                <textarea name="pcode" rows="15" cols="90"></textarea> <br />
                    <br /> &nbsp; &nbsp; &nbsp; 
                    <input type="submit" value="Add This Item">
            </form>
                '
    ;
        }
    }
    edit: ahhh I bet it is because $catarray is not declared in the function, and thus it is treating it like $catarray does not exist...

    edit2: yeah, it works now knew it would be something obvious
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches


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
  •