0

Case :

Hi, Stackoverflow community.

I want to set selected value on SELECT type input based on fetched value from database table column. So if db table column contain "A", SELECT type input will also show "A".

Value in SELECT type input, provide manually.

Experiment :

To show my logic on how to do this, i tried this code

// $query2['via'] will produce fetched value from database
<select selected='".$query2['via']."' name='slPayment'>
<option>--</option>
<option value='Cash'>Cash</option>
<option value='Bank Transfer'>Bank Transfer</option>
<option value='Credit Card'>Credit Card</option>
<option value='Cheque'>Cheque</option>
<option value='Others'>Others</option>
</select>

Experiment Result :

Wont Work

Desired Output :

I actually just want that SELECT type input, show the exactly same value from fetched database value. Any idea how to fix this ?

Thank You

Thank you for Stackoverflow community. I won't stop saying thank you. Btw, i purely asking for learning and knowledge sharing. I'm open to any comment (related info with this question).

But please don't judge me like i am just asking for code. I actually learn how to code based on that code itself, then i adapt to make use of that code properly, am not familiar with library, am still a college student. I wont ask before i try it by myself. Some people did judge me on recent question. And it's not very nice act to this community. I'm learning, we learning.

So thank you once again.

4
  • does your variable $query2['via'] contain cash/Credit Card/Cheque/.. these values?? Commented Jul 29, 2016 at 6:29
  • Instead of hardcoding the options, I would suggest having them in an array and echoing them in a loop. Commented Jul 29, 2016 at 6:34
  • Hey, the "thank you" section is really nice, but on this platform it is considered noise if you put it into the question/answer/comments. Your best way to say thank you is by upvoting and accepting helpful answers and giving constructive feedback and awesome questions. If you really want to explicitly say thank you, your profile page would be the best place for it. Commented Jul 29, 2016 at 6:45
  • @ AsifRahaman yes, that variable contain value like "Cash / Credit Card" etc just like in select box. @ IbrahimLawal its a good suggestion actually, but im not too familiar with array, if me, i would fetch from database instead probably. @ Philipp i wont on next question, but i just want to tell once, because on my recent question, a missconception or something like that happen, i try to explain and communicate it. thank you for your suggestion. Commented Jul 29, 2016 at 7:02

2 Answers 2

2

img_type is my query result and i want img_name in drop_down

 <select id="id_name"  name="photo_type" value="">
                  <option value=""></option>
                   <?php foreach($img_types as $row){ ?>
                    <option value="<?php echo $row->imgtype_id?>"><?php echo $row->imgtype_name?></option>
                    <?php } ?>
                </select>
Sign up to request clarification or add additional context in comments.

Comments

1

You can change your code thus:

<select name='slPayment'>
    <option>--</option>
    <option " . (($query2['via']=='Cash') ? 'selected="selected"': '') .  "value='Cash'>Cash</option>
    <option " . (($query2['via']=='Bank Transfer') ? 'selected="selected"': '') . " value='Bank Transfer'>Bank Transfer</option>
    <option " . (($query2['via']=='Credit Card') ? 'selected="selected"': '') . " value='Credit Card'>Credit Card</option>
    <option " . (($query2['via']=='Cheque') ? 'selected="selected"': '') . " value='Cheque'>Cheque</option>
    <option " . (($query2['via']=='Others') ? 'selected="selected"': '') . " value='Others'>Others</option>
</select>

5 Comments

i got this : syntax error, unexpected '?' , what i do is separate <?php echo with html tag, just like echo ".....<option" <?php echo ?> " .... value = . am not sure how, still figure it out..
I don't get? You mean the code above has a syntax error?
yes, because i try to run code above inside php looping fetch, i must separate html tag with php code above.
like this : while($query2=mysql_fetch_array($query1)) { [your_code] }
@KeshiaAngelina Did you figure this out?

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.