www.webdeveloper.com
Results 1 to 4 of 4

Thread: question about sql and php select option

  1. #1

    question about sql and php select option

    I was wondering if they were a way to have a select option in side a table.

    in the table i have a field called for_sale and put this in it Dealer&&Private Party

    so I want to pull this filed out and make select option. can the sql be split on the&& or some other type of value in the middle?



    just a normal select option you would do something like this

    Code:
    <?php
    echo "<select name='for_sale '> 
    <option value='.$row[for_sale ].'selected='selected'>$row[for_sale ]</option>";
                       
    $query = mysql_query("SELECT for_sale_values FROM for_sale ");
    while($temp = mysql_fetch_assoc($query) )
    {
    ?>
    <option value="<?php echo $temp['for_sale ']; ?>"><?php echo $temp['for_sale ']; ?></option> 
    <?php
    }
    echo "</select>";?>
    Winracer
    Jack of all trades master of none
    http://www.brownlows.net

    “Only a life lived for others is a life worth while” Albert Einstein
    “The golden rule for every business man is this: 'Put yourself in your customer’s place'” Orison Swett Marden
    “No person was ever honored for what he received; honor has been the reward for what he gave” Calvin Coolidge

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    Well, you'd probably be better off in the long run if you didn't try to store two values in one field (search on "data normalization"), either breaking that into two separate columns in the table if that makes sense for what that table is modeling, or else put them into a separate relational table in separate rows, with another field that is a foreign key into the main table.

    If that is not possible/practical for some reason, I'd probably do the splitting in the PHP code, using either explode() or preg_split() within the loop on each DB result row. You *could* probably do it within the SQL by fiddling around with the various text manipulation functions MySQL has, but that would be my last choice, I think (letting the database server just do database stuff while the application code on the web server can handle text manipulation and output -- but normalizing your data design should be the first choice.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Quote Originally Posted by NogDog View Post
    Well, you'd probably be better off in the long run if you didn't try to store two values in one field (search on "data normalization"), either breaking that into two separate columns in the table if that makes sense for what that table is modeling, or else put them into a separate relational table in separate rows, with another field that is a foreign key into the main table.

    If that is not possible/practical for some reason, I'd probably do the splitting in the PHP code, using either explode() or preg_split() within the loop on each DB result row. You *could* probably do it within the SQL by fiddling around with the various text manipulation functions MySQL has, but that would be my last choice, I think (letting the database server just do database stuff while the application code on the web server can handle text manipulation and output -- but normalizing your data design should be the first choice.

    thanks for the reply!! I think I will use a different table to pull the select option in to the form and use the other table to post what was selected. I did not know if it would make scene to do the way I was trying or not. I thought it might be easier. if i create a table called select_option and put in different columns 'Dealer' and 'Private Party' how would i pull it into a forum with select option? just think i could put all the different select option in the same table.
    Winracer
    Jack of all trades master of none
    http://www.brownlows.net

    “Only a life lived for others is a life worth while” Albert Einstein
    “The golden rule for every business man is this: 'Put yourself in your customer’s place'” Orison Swett Marden
    “No person was ever honored for what he received; honor has been the reward for what he gave” Calvin Coolidge

  4. #4
    I did get this to work on pulling from a column with the && between the values to select, just thought I would share, not sure about it will post back to sql yet.

    Code:
    echo "<select name='for_sale_by'> 
    <option value='for_sale_by'selected='selected'>for_sale_by</option>";
                       
    $query = mysql_query("SELECT for_sale_by FROM ads where adid = '$adid'");
    
    
    while($temp = mysql_fetch_assoc($query) )
    {
    for($i = 0; $i < 2; $i++) {
     
    $arr[] = explode("&&", $temp["for_sale_by"]);
    
    ?>
    <option value="<?php echo $arr[$i]; ?>"><?php echo $arr[$i];?><br> 
    
    </option> 
    <?php
    
    
    }
    }
    echo "</select>";?>                  	
    
    
    <input type="hidden" name="for_sale-by">
    
    <?php
    Winracer
    Jack of all trades master of none
    http://www.brownlows.net

    “Only a life lived for others is a life worth while” Albert Einstein
    “The golden rule for every business man is this: 'Put yourself in your customer’s place'” Orison Swett Marden
    “No person was ever honored for what he received; honor has been the reward for what he gave” Calvin Coolidge

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles