Jump to content

[SOLVED] Programatically Select Listbox Items from Imploded Array


FishWagon

Recommended Posts

Hello!

 

I have a registration form with a listbox where people can select multiple items. I Implode the array of selected items and store them into a MYSQL DB.

 

So, for example, the field in that database will have a value of: SelectedValue1, SelectedValue4, SelectedValue7, SelectedValue10, SelectedValue11

 

When the users login, they can change info about their record. So on this page, I have the listbox with the same entries as the listbox on the registration form. I pre-populate all the other fields on this "user record form" with my recordset and allow them to change things.

 

What I can't figure out, is to how to "pre-select" the items for them so I can show them what they originally selected when registering.

 

So I guess my question is, how can I programatically select an item in an already constructed listbox, using a recordset where the value is an imploded array?

 

Thanks for the help, pretty new to PHP programming.....

 

Have a great day everyone,

 

Rich

Hi

 

Store the "array" as seperate rows on a 2nd table.

 

This way when you generate a drop down list you can do an outer join between your full list of options and the list for that particular person.

 

All the best

 

Keith

Understood.

 

However, what I am having problems trying to do is to highlight their previous selections.

 

So once I populate the listbox (which I can do no problem), I want to highlight for them their previous selections so they can see what they already selected when they registered.

 

Is there a way to programatically select multiple items in a listbox?

Hi

 

Say you have 3 tables

 

Users

UserId

Name

 

Items

ItemId

ItemName

 

UsersItems

UserId

ItemId

 

This rough code would do it for you (please excuse any typos):-

 

<?php

$dbms = 'mysql';
$dbhost = 'db.MyIsp.com';
$dbname = 'name';
$dbuser = 'userid';
$dbpasswd = 'password';

// Make the database connection.
$conn = mysql_connect($dbhost,$dbuser,$dbpasswd) or die(mysql_error());
mysql_select_db($dbname,$conn);

$sql = "SELECT c.Name AS Name, a.ItemId AS ItemId, a.ItemName AS ItemName FROM Items a LEFT OUTER JOIN UsersItems b ON a.ItemId = b.ItemId LEFT OUTER JOIN Users c ON b.UserId = c.UserId WHERE c.UserId = '".$GET['UserId']."'";

if ( ($result = mysql_query($sql,$conn)) )
{
echo 'Welcome back '.$GET['UserId'].'<br />';
echo '<select name="ItemSelect" mulitple>';
while ($row = mysql_fetch_array($result))
{
	echo '<option value="'.$row['ItemId'].'" '.(($row['Name'] != '') ? "select='selected'" : '' ).' >'.$row['ItemName'].'</option>';
}
echo '</select>';
}
?>

 

You would have to clean this up a bit (ie, sanitise the entered userid before using it in the SQL), but hopefully this will give you the basic idea.

 

All the best

 

Keith

  • 3 weeks later...

Hello, I have a very similar issue , my script is only able to set the very first value as selected, if a listbox has multiple values that were previously inserted, it is only able to assign the first value as selected value.

Below the outer while retrieves the listbox citizenship values from MS SQL database that were previously inserted and then,the inner while

is for oracle (that is why you see oci call) that hits oracle table and retrieves all citizenship values and compares with that values that were inserted

into the MS SQL database and tried to set them as selected but is sets only the first record as selected.

 

Any help would be appreciated??

 

while ($msrow = mssql_fetch_array($result,MSSQL_BOTH)) {

  $CITZ = $msrow['CITZ'];

 

  while ($row = oci_fetch_assoc($stmt)) {

 

$sel =((trim($msrow['CITZ']) == trim($row['CODE'])) ? 'selected' : NULL );

echo  "<option value='". $row['CODE'] ."' '. $sel .'>". $row['DESC'] ."</option>";

 

}

 

 

    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.