I tried inserting as serialize to multiple selected but when i want to retrieve those multi selected in the select box is not working

echo "<td class='tbl1' width='15%'>Format</td><td class='tbl1'><select name='format[]' multiple='multiple' class='textbox'>\n";
        $sel = ($_POST['format'] == $data['format'] ? " selected='selected'" : "");
        echo "<option value='".$data['format']."'$sel>".$data['format']."</option>\n";        
echo "</select></td>\n";
echo "</tr>\n<tr>\n";
Member Avatar

This doesn't make sense to me.
The ternery conditional is wrong and there doesn't seem to be a loop for outputting all the options.

$variable = (...test condition[s]...) ? ...do if true... : ...do if false...;

I want to select those options selected when inserted to the table

$categoryarray = array('Option1', 'Option2', 'Option3');
$data = mysql_fetch_assoc(.....);
$data['format'] has (Option1, Option3)
echo "<select name='format[]' multiple='multiple' class='textbox'>\n";
    foreach($categoryarray as $field){
        $sel = ($field == $data['format'] ? " selected='selected'" : "");
        echo "<option value='".$field."'$sel>$field</option>\n";              
echo "</select>\n";

but its showing all the 3 options without highlighting the 2 options of $data['format']

Member Avatar

As I mentioned:

$sel = ($field == $data['format'] ? " selected='selected'" : "");

is wrong. You could use this...

$sel = ($field == $data['format']) ? " selected='selected'" : "";

But I'm assuming that you want to see whether a field exists in the list ($data['format']) which I assume is an array. Perhaps this...


can you give full code if you don't mind

I tried this one it works but there's duplicate now

$format = explode(", ", $data['format']);
for($i=0; $i<count($format); $i++){
    foreach(explode(",", array('Option1', 'Option2', 'Option3')) as $field){
        $sel = ($field == $format[$i]) ? " selected='selected'" : "";
        echo "<option value='".$format[$i]."'$sel>".$format[$i]."</option>\n"; 

Option1 selected
Option3 selected

Member Avatar

I'm struggling to see where your data is coming from for populating the select field - and in which format this is in.

I shall assume that all options are held as a comma separated string in the $data['format'] array item. So something like...

$data['format'] = "Option1,Option2,Option3,Option4,Option5,Option6";

However, I'm quite confused with the nomenclature, so I shall use a "car make" example to show how I would do it.

$optionItems = array(21=>"Ford",12=>"Morgan",23=>"Fiat",7=>"Mercedes",9=>"Saab",34=>"BMW");
//this could be taken from a DB table  

$countSelected = 0;
    $selectedItems = (array) $_POST['cars'];
    $fltSelectedItems = array_filter($selectedItems, "is_int");
    $countSelected = count($fltSelectedItems);
//the above can work with a multiple select widget or the $_POST['cars'] could be swapped for a $column['cartype_id'] or similar if this data comes from a DB, e.g. cars_owned table with [ user_id | cartype_id ] as fields

//To build the widget, do this:
$options = '';
foreach($optionItems as $index=>$label)
    $sel = ($countSelected && in_array($index,$fltSelectedItems)) ? " selected='selected'" : '';
    $options .= "\n\t<option value='$index'$sel>$label</option>";

//Then forget about it until you need to place it in your form...

<form... >
    <?php echo $options;?>

Thank you so much diafol for your support, it worked

this is my last result

        echo "<td class='tbl1' width='15%'>Format</td><td class='tbl1'><select name='format[]' multiple='multiple' class='textbox'>\n";
            foreach(explode(",", $config['categories']) as $index=>$label){
                $sel = (count(explode(", ", $data['format'])) && in_array($label,explode(", ", $data['format']))) ? " selected='selected'" : '';
                echo "\n\t<option value='$label'$sel>$label</option>";
        echo "</select></td>\n";
        echo "</tr>\n<tr>\n";
Member Avatar

OK, solved? :)

yes, I have just changed a few things and many thanks

Hi diafol again, one more question how can i get this format using $_GET

$optionItems = array(21=>"Ford",12=>"Morgan",23=>"Fiat",7=>"Mercedes",9=>"Saab",34=>"BMW");


if($_GET['format'] == "Fiat"){
    I want to get all models of Fiat..
Member Avatar

As a rule, if you were retrieving data from a DB with related tables thus:

man_id | man_name
21     | Ford
12     | Morgan
23     | Fiat

model_id | model_name | man_id
1        |  Fiesta    | 21
2        |  Escort    | 21
3        |  500L      | 23
4        |  Focus     | 21
5        |  Trekking  | 23

to show models of Fiat:


That could come from a form like...

<form action="showmodels" method="get">
    <label for="man">Manufacturers</label>
    <select name="man" id="man">
        <?php echo $manufacturers;?>
        <!-- get php to build the above -->
    <input type="submit" value="Get models" />

The options for the above would look something like...

<option value="21">Ford</option>
<option value="23">Fiat</option>

So you get the data from the DB with a query...

$man = (int) $_GET['man'];

The prepared statement would then look like...

"SELECT model_id, model_name FROM models WHERE man_id = :man_id"

Alternatively, if you don't have DB tables, just static data, then I'd store the lot as json and then use js to populate the models select field.

I have this in table

id     title,           parent      format               
1      Test Category1   0,          Nasheed         
2      Test Category2   0,          Muxaadaro, Nasheed

In format section there's different

Test Category1 has(Nasheed [format])
Test Category2 has(Muxaadaro, Nasheed [format])

how can i sort by format

Is this ok diaflo

I tried this way

$sub = $_GET['sub'];
$query = dbquery("SELECT * FROM categories WHERE format LIKE '%$sub%'");
while($data = dbarray($query)){
    echo $data['title']." <br>";
Member Avatar

I have no idea heh heh. Not sure what you're trying to do. But if you're trying to retrieve all "titles" where "format" contains "Nasheed" for example, you'd expect to retrieve TestCategory1 and TestCategory2.

It seems that you're using a user-defined functions to run and loop over the query. Please ensure that the guts of these functions are using PDO or mysqli and not mysql, which has been deprecated.

In addition I see no escaping of the raw input ($sub) - this is very dangerous as you are open to SQL injection.

I would suggest using prepared statements in PDO (or mysqli).

Maybe also use FIND_IN_SET() instead of LIKE...

"SELECT id, title FROM categories WHERE FIND_IN_SET(':sub',format)"

For PDO, you could do this...

//create the pdo object ($db) prior to this...
$stmt = $db->prepare("SELECT id, title FROM categories WHERE FIND_IN_SET(':sub',format)");
$stmt->bindParam(':sub', $sub, PDO::PARAM_STR);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

Your data is now held in the $result array