SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    United Kingdom
    0 Post(s)
    0 Thread(s)

    Trying to populate & preselect a multiple select field

    I'm producing a multiple select field in a basic CMS. This form inputs information into the database about individual offers. On the page where you can edit an offer, the script selects all wholesalers from the database and populates the multiple select field. This field determines all wholesalers who are participating in that specific offer.

    My problem lies in trying to show which wholesalers are currently participating when the offer is edited. I need to preselect those wholesalers that are already participating, which I assume I do something like(???):

    Code HTML4Strict:
    <option value="1"  multiple selected>Ted's Mobiles</option>
    <option value="2"  multiple>Bert's Mobiles</option>
    <option value="3"  multiple selected>Alan's Mobiles</option>
    <option value="4"  multiple>Ken's Mobiles</option>

    I'm getting stuck a bit when I try to echo "selected";

    I initially query the database:
    Code PHP:
    $query = "SELECT companyid
    		, company
    		FROM wholesalers
    		order by company";

    and use a while loop to populate the list, but can anyone then suggest how I preselect the relevant wholesalers?

    I query the database again..

    Code PHP:
    $query2 = "SELECT w.companyid
    		, wo.offerid
    		FROM wholesalers w
    		INNER JOIN wholesaleroffers wo
    		ON wo.companyid = w.companyid
    		WHERE offerid = " . $offerid . "
    		ORDER BY";

    My poor effort (full of rubbish!) ended here....

    Code PHP:
    	echo "<option value='" . $row['companyid'] . "'";
    							echo "SELECTED";						} 
           echo ">" . $row['company'] . "</option>\n\r";

    How do I really do this!?

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    0 Post(s)
    0 Thread(s)
    Take your first query and loop through the result set, store the name of the company in an array use an index such as name, take the record set from the second query and loop through the results of companies. If the name of the company appears in the second record set then set an index such as participates to TRUE or (1). Now loop through the array and display the company names, check the index "participates" with isset() and if true then the option needs to be selected.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


Posting Permissions

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