SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array in a variable

    Hi guys

    I want to store the categories in my database as a array


    $allcategory = array('');


    the variable is $allcategory, what code would i use to call the categories from the database and store them in the array

    this is my code to call them from the database

    $sql = " select * from category";


    im using it to fill my dropdown box
    PHP Code:
    <? echo "<select name='category'>";
    foreach(
    $allcategory as $z) {
       
    $selected = ($z == $category) ? 'selected="selected"' '';
       echo 
    "<option value='$z$selected>$z</option>\n";
    }
    echo 
    "</select>";?>




    Many thanks in advance

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "SELECT * FROM category";
    $result mysql_query($sql) or die("SQL error: ".mysql_error());

    if(
    mysql_num_rows($result) != 0)
    {
     while(
    $tmp mysql_fetch_assoc($result))
     {
       
    $allcategory[] = $tmp;
     }

    -- Jelena --

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $allcategory = array();
    while(
    $rows mysql_fetch_assoc($sql)) {
        
    $allcategory[] = $rows['categoryName'];

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jelena

    I used your code

    but every field in the dropdown is showing as array?


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
  •