0

I've got a list of records with edit links on them. When i click on the link it takes me to an edit page with the results from the database.

I can retrieve the data successfully for all the text boxes.

I'm trying to implement this via an array which fails to work.

Here is my implementation in code fragments:

        # $data is from $data=mysqli_fetch_array($result)


                        $product=$data['product'];
                        echo $product.'<br />';

        #initializing array to empty
           $product_list=array("Remote"=>" ","TV"=>" ","Box"=>" ");

        if (array_key_exists($product,$product_list)){


            $product_list["'$product'"] =  'selected="selected" ';


            }

                            print_r($product_list);


        #combo box 

              <select name="products">
                        <option value="select">Select</option>
                        <option value="Remote"<?php echo @$product_list["'$product'"] ?>>Remote</option>
                        <option value="TV" <?php echo @$product_list["'$product'"] ?>> TV</option>
                        <option value="Box" <?php echo @$product_list["'$product'"] ?>> Box</option>
                    </select>

In the page that displays all the records if i hit edit on a record that has product 'Remote' i get the following output(as per echo statements above):

Remote
Array ( [Remote] => [TV] => [Box] => ['Remote'] => selected="selected" ) 

The HTML form displays:

 <select name="products">                   
    <option value="select">Select</option>
        <option selected="selected" value="Remote">Remote</option>
        <option selected="selected" value="TV"> TV</option>
        <option selected="selected" value="Box"> Box</option>
</select>

In the edit page if i select a record that has product 'TV' i get the following output:

 TV
 Array ( [Remote] => [TV] => [Box] => ['TV'] => selected="selected" ) 

HTML output is same as above. And it always sets the option to the last product which is 'Box'.

Can somebody please advise on how i can fix this? thanks!

2
  • You explicitly specifying that all the options in the <select> tag are to be selected, that is why your getting the complete list of options in the edit page. Also if it is a drop-down list, specify size=1 attribute in select tag. Commented May 13, 2012 at 8:05
  • Thanks, i meant to say drop down box : ] Commented May 13, 2012 at 8:45

1 Answer 1

1

This:

$product_list["'$product'"] =  'selected="selected" ';

should be

$product_list[$product] =  'selected="selected" ';

And in other places that have $product_list["'$product'"] you should also remove quotes.

Edit:

Here

<select name="products">
    <option value="select">Select</option>
    <option value="Remote"<?php echo @$product_list["'$product'"] ?>>Remote</option>
    <option value="TV" <?php echo @$product_list["'$product'"] ?>> TV</option>
    <option value="Box" <?php echo @$product_list["'$product'"] ?>> Box</option>
</select>

you are not using any loop and trying to get the value of $product_list[$product] in every item - $product will always be the same in every line and equal to the last one fetched from mysql.

You have either to use correct index in each line (ie. <?php echo $product_list['TV']; ?>) or proper way - use the loop:

<select name="products">
    <option value="select">Select</option>
    <?php foreach($product_list as $product_name => $selected): ?>
    <option value="<?= $product_name ?>" <?= $selected ?>><?= $product_name ?></option>
    <?php endforeach; ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.