Re: Multiple select box using mysql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm not sure if anyone answered this for you, and perhaps I'm missing
something. Are you simply trying to populate a multi-select box with
MySQL data?

If so, try this:
echo form tag here...
echo "<SELECT NAME=\"software[]\" MULTIPLE SIZE=5>";
$query="select ID, Name, Version from software";
$result=mysql_query($query) or die("Could not query database");
while ($row=mysql_fetch_array($result)) {
  echo "<OPTION
VALUE=\"{$row["ID"]}\">{$row["Name"]}-{$row["Version"]}</OPTION>"; //
software select row format Name-Version
}
echo "</SELECT>";

echo "<SELECT NAME=\"expert[]\" MULTIPLE SIZE=5>";
$query="select ID, Name from experts";
$result=mysql_query($query) or die("Could not query database");
while ($row=mysql_fetch_array($result)) {
  echo "<OPTION VALUE=\"{$row["ID"]}\">{$row["Name"]}</OPTION>";
}
echo "</SELECT>";
echo "</FORM>";

This will provide you with a form to link the experts and software.

To actually do the linking, you need to insert the data in to the DB.
The action to perform when submitting the form might be something
like this (just steps, not code)

Confirm arrays have data
loop through $software array {
loop through $experts array {
build an insert SQL statement to insert the software table ID and the
expert table ID into a lookup table.
execute the insert statement
}
}

For example,

if (isset($software) && isset($expert)) {
  for ($i=0;$i<count($software);$i++) {
    for ($j=0;$j<count($expert);$j++} {
      $query="insert into <lookuptbl> set
software=\"{$software[$i]}\", expert=\"{$export[$j]\";
      $result=mysql_query($query) or die("Could not insert data");
    }
  }
} else {
error handling routine here
}


You might want to reconsider making the "software" selection box
multiselect. That will allow many to many relationships. If that's
what you want, that's ok. But it allows for a lot of user error.
Eliminating it would eliminate the outer loop, it would make
$software a standard variable rather than an array, and you could
remove the [] from the SELECT form element for the software.

Please check all code, as it's off the top of my head, and not
verified at all.

--- Jason End <bensnephew@yahoo.com> wrote:
> But how do I populate the array with (for e.g.) 2
> fields from a mysql table. Each line of the select box
> needs to hold the Name and Version of the piece of
> software, the results from a:
> select Name, Version from software
> 
> -s-
> 
> --- Corne' Cornelius <cornec@reach.co.za> wrote:
> > You have to give the Mutli select box a name as an
> > array. eg:
> > 
> > <SELECT NAME="experts[]" MULTIPLE SIZE=5>
> > <OPTION VALUE="key1">val1</OPTION>
> > </SELECT>
> > 
> > then in PHP, you can access $experts as an array.
> > 
> > while (list ($key, $val) = each($experts))  {
> >    print "$key = $val<BR>\n";
> > }
> > 
> > 
> > Jason End wrote:
> > 
> > >I writing a software catalog that features
> > "experts",
> > >who a people especially skilled in a piece of
> > >software. My db, has a "software" table and an
> > >"experts" table. 
> > >I need to have a page that generates 2 multiple
> > select
> > >boxes, each on with the data from each table. 
> > >>From there one will be able to choose the software
> > >program and the experts, which will then record the
> > >software-expert relationship in the DB. 
> > >
> > >The problem is that I don't really understand how
> > to
> > >populate multi select boxes with db data, and then
> > >have the selections passed back as inserts. 
> > >
> > >Can someone clue me in?
> > >
> > >thanks, 
> > >
> > >Jay
> > >
> > >__________________________________________________
> > >Do you Yahoo!?
> > >Yahoo! Mail Plus - Powerful. Affordable. Sign up
> > now.
> > >http://mailplus.yahoo.com
> > >
> > >  
> > >
> > 
> > 
> > 
> > 
> > =============Disclaimer and
> > Confidentiality===================
> > This message contains information intended for the
> > perusal, and/or use (if so stated), by the stated
> > addressee(s) only. The information is confidential
> > and privileged. If you are not an intended
> > recipient, do not peruse, use, disseminate,
> > distribute, copy or in any manner rely upon  he
> > information contained in this message (directly or
> > indirectly). The sender and/or the entity
> > represented by the sender shall not be held
> > accountable in the event that this prohibition is
> > disregarded. If you receive this message in error,
> > notify the sender immediately by e-mail, fax or
> > telephone representations contained in this message,
> > whether express or implied, are those of the sender
> > only, unless that sender expressly states them to be
> > the views or representations of an entity or person,
> > who shall be named by the sender and who the sender
> > shall state to represent. No liability shall
> > otherwise attach to any other entity or person.
> >
> ======================================================
> > 
> > 
> > -- 
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> > 
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
> 
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


=====
Mark Weinstock
mark_weinstock@yahoo.com
***************************************
You can't demand something as a "right" unless you are willing to fight to death to defend everyone else's right to the same thing.
***************************************

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux