SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: what structure

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what structure

    hi there

    i have the following array
    $my_array = array($first_product_id, $second_product_id...)

    and

    PHP Code:
    echo '<select name="product_id">';
    foreach(
    $my_array as $id_product)
    echo 
    '<option value="'.$id_product.'">'.$id_product.'</option>';
    echo 
    '</select>'
    But I need to display the product name in the select option menu, not the id.
    How to do that? What structure to use?

  2. #2
    SitePoint Addict zero_digit's Avatar
    Join Date
    Jul 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of this

    echo '<option value="'.$id_product.'">'.$id_product.'</option>';

    try this

    echo "<option= {$my_array[0]}>"'.$id_product.'</option>';

    hope this help....

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zero_digit View Post
    echo "<option= {$my_array[0]}>"'.$id_product.'</option>';
    sorry, but this didn't work for me, or at least I don't know how to use it right.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $array = array(
                    
    '10001'=>'Product Name1',
                    
    '10002'=>'Product Name2',
                    
    '10003'=>'Product Name3',
                    
    '10004'=>'Product Name4',
                    
    '10005'=>'Product Name5',
                    
    '10006'=>'Product Name6'
                
    );
    echo 
    '<select name="products">';
    $selected_prd 10004;
    foreach(
    $array as $product_id=>$product_name){
        
    $sele = ($selected_prd == $product_id) ? ' selected="selected"' '';
        echo 
    '<option value="' $product_id '"' $sele '>' $product_name '</option>' "\n";
    }
    echo 
    '</select>'
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    PHP Code:
    $array = array(
                    
    '10001'=>'Product Name1',
                    
    '10002'=>'Product Name2',
                    
    '10003'=>'Product Name3',
                    
    '10004'=>'Product Name4',
                    
    '10005'=>'Product Name5',
                    
    '10006'=>'Product Name6'
                
    );
    echo 
    '<select name="products">';
    $selected_prd 10004;
    foreach(
    $array as $product_id=>$product_name){
        
    $sele = ($selected_prd == $product_id) ? ' selected="selected"' '';
        echo 
    '<option value="' $product_id '"' $sele '>' $product_name '</option>' "\n";
    }
    echo 
    '</select>'
    many thx rajug..this is a very clean/neat solution.


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
  •