SitePoint Sponsor

User Tag List

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

    array displaying array

    Hi guys

    I have a problem, im calling a array into a dropdown. Its showing the correct number of rows but they all display 'array' and not the values from the database

    Can anyone help me?


    PHP Code:
    $sql "SELECT category 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[] = array($tmp);

     }

    PHP Code:

    <? echo "<select name='category'>";
    foreach(
    $allcategory as $z) {
       
    $selected = ($z == $category) ? 'selected="selected"' '';
       echo 
    "<option value='$z$selected>$z</option>\n";
    }
    ?>

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use the results returned by the database more than once, this is a good approach, else you can just echo the things you need inside the while-loop.
    $allCategories = array();
    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
        
    $allCategories[] = $row;
    }

    foreach (
    $allCategories as $dbCategory) {
        
    $selected $category == $dbCategory['id'] ? ' selected="selected"' '';
        echo 
    '<option value="'.$dbCategory['id'].'"'.$selected.'>'.$dbCategory['Name'].'<option>';

    Last edited by hexburner; Jun 29, 2007 at 00:10.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it now displays all the categories but doesn't highlight the one it should

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what fields you have in the table with categories, so it would be nice to be a little more specific on that part.
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while($row mysql_fetch_assoc($result)) {
        
    $allCategories[] = $row;
    }

    foreach (
    $allCategories as $dbCategory) {
        if ( 
    $selected == $dbCategory["Name"]) {
            echo 
    "<option value='" .$dbCategory["Name"]. "' selected='selected'>" .$dbCategory["Name"]. "</option>\n";
        } else {
            echo 
    "<option value='" .$dbCategory["Name"]. "'>" .$dbCategory["Name"]. "</option>\n";
        } 

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $selected $category == $dbCategory['id'] ? 'selected="selected"' ''
    PHP Code:
        if ( $selected == $dbCategory["Name"]) { 
    Aren't you using $selected in two totally different contexts here?
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it appears there is only one column in category. Go back and look @ the first post in this thread. Follow the post in this thread.

    Quote Originally Posted by dnbidder2005 View Post
    it now displays all the categories but doesn't highlight the one it should
    So I can presume the only column would be "Name".

    Yes I see your point the code should read.
    PHP Code:

    if ( $category $dbCategory["Name"] ) { 
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  8. #8
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry byron3, for some reason I thought your post was made by the OP. It's been a long day
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!


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
  •