I have problem with selecting elements from array i have this code which is supposed to take the choosed element and echo it back but instead that this code just takes last element and echo it what is wrong with the code ?

<div class="col-xs-3">
                                <?php
                                    global $m;
                                    $currencies = array("GBP"=>"£", "EUR"=>"€", "USD"=>"$");
                                ?>
                                <form action="" method="POST">
                                    <div class="input-group stylish-input-group">
                                        <p><input type="text" class="form-control" placeholder="Bet" name="bet" style="display: inline;" required/></p>
                                        <span class="input-group-addon">
                                            <button type="button">
                                                <span class="glyphicon glyphicon-menu-down" tabindex="0" title="Choose Currency" role="button" data-toggle="popover" data-trigger="focus" data-placement="bottom" data-content="
                                                <?php
                                                    foreach($currencies as $m => $value) {
                                                        ?>
                                                        <p><a href='#' type='submit' data-toggle='modal' data-target='#currencyModal'><?= $m; ?></a></p>
                                                        <?php
                                                    }
                                                ?>"/><?php echo $m ?></span>
                                            </button>
                                        </span>
                                    </div>
                                </form>
                            </div>
                            <div class="modal fade" id="currencyModal" tabindex="0" data-trigger="focus" role="dialog" aria-labelledby="myModalLabell">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" id="myModalLabel">Set Default Currency</h4>
                                        </div>
                                        <div class="modal-body">
                                            <p>Do you want to make <?php echo $m; ?> your default currency ?</p>
                                            <br>
                                            <p style="font-size: small; color: #90949c;">By setting it by default you will be no longer asked to choose default currency. Also tickets you add automatically will be in the currency you choose as default.</p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-success" >YES</button>
                                            <button type="button" class="btn btn-danger" data-dismiss="modal">NO</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

In this code

    <?php
         foreach($currencies as $m => $value) {
      ?>
       <p><a href='#' type='submit' data-toggle='modal'
                      data-target='#currencyModal'><?= $m; ?></a></p>
               <?php
                       }
                 ?>"    

You iterate through the entire array, so naturally you get the last $m and $value read. If you display all the keys and let the user choose which they want, then you can access the value part directly. I know that's what you think you are doing with the form submit code but I'm not sure it is doing what you want.

In any case, this is a good example of improper use of php on a web page. It is basically impossible to debug and side effects are massive.

Any help about the info you give to me please ?

anyone who can help ?

Member Avatar for diafol

This is a classic problem with mixing HTML and PHP. I suggest you run your PHP functions and simply pass on simple variables to the HTML. Often loops and conditionals can help, but in the main this mixing is horrible and difficult to maintain - and BTW avoid global - yuck.

Not sure why all those options are being placed inside one button tag:

<button type="button">
                                            <span class="glyphicon glyphicon-menu-down" tabindex="0" title="Choose Currency" role="button" data-toggle="popover" data-trigger="focus" data-placement="bottom" data-content="
                                            <?php
                                                foreach($currencies as $m => $value) {
                                                    ?>
                                                    <p><a href='#' type='submit' data-toggle='modal' data-target='#currencyModal'><?= $m; ?></a></p>
                                                    <?php
                                                }
                                            ?>"/><?php echo $m ?></span>
                                        </button>

Its a input text with dropdown menu which you can choose your default currency.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.