0

have little problem with my citylisting in html <option>.

Want to list all cities in <select><option>.....citylisting.....</option></select>

my list is like this:

$citylist = array('Adana', 'Adıyaman', 'Afyonkarahisar', 'Ağrı', 'Amasya', 'Ankara');

if i use print_r; giving me list of arrays,

Array ( [0] => Adana [1] => Adıyaman [2] => Afyonkarahisar [3] => Ağrı [4] => Amasya [5] => Ankara)

i'm newbee in php, can i use this values for use in citylisting? Thanks already,

1
  • You can use foreach to put the values in option element. Commented Dec 7, 2016 at 8:59

7 Answers 7

4
<select name="city" id="cities" class="selectbox"> 
<?php   
    foreach ($citylist  as $city) {
        echo "<option value='$city'>$city</option>";
    }
?>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

yeap, works like a charm.. I was trying to do it with while loop. Thank you so much
3

You need to use foreach loop to iterate the array and need to pass that array value to <select><option>.

Try below code :

<?php
$citylist = array('Adana', 'Adıyaman', 'Afyonkarahisar', 'Ağrı', 'Amasya', 'Ankara');

echo "<select name='city' id='cities' class='city'>>";
foreach ($citylist as $city) {
    echo "<option value='$city'>$city</option>";
}
echo "</select>";

2 Comments

But where is the value= attribute of the option tag and the name= attribute of the select tag.
@RiggsFolly you are tooo quick updating my answer anyways updated the answer
2

you may want to use foreach in PHP as so:

<?php

$arr = ['Adana', 'Adıyaman', 'Afyonkarahisar', 'Ağrı', 'Amasya', 'Ankara'];

$options = '';
foreach ($arr as $k => $v) {
    $options .= '<option value="'.$k.'">'.$v.'</option>';
}
$options = '<select name="foobar">'.$options.'</select>';
echo $options;

where $arr is your array ; you may prefer to use echo() to print $options and not print_r(): the first one does not format your options, as the second one does...


or my prefered, inline style:

<?php
    $arr = ['Adana', 'Adıyaman', 'Afyonkarahisar', 'Ağrı', 'Amasya', 'Ankara'];
?>
<select name="foobar">
    <?php foreach ($arr as $k => $v): ?>
        <option value="<?= $k; ?>"><?= $v; ?></option>
    <?php endforeach; ?>
</select>

Comments

1

Mean this? :)

<select><?php foreach ($citylist as $city) { echo "<option>$city</option>"; } ?></select>

1 Comment

yeap, its working. I wasn't know about foreach command in php. Thnx
0

try this,

<select>
<?php
foreach($citylist as $citylist){
echo "<option>".$citylist."</option>";
}
?>
</select>

Comments

0

Try to something like this.

  <?php
        $citylist = array('Adana', 'Adıyaman', 'Afyonkarahisar', 'Ağrı', 'Amasya', 'Ankara');
        echo "<select name='city'>";?><?php
        foreach ($citylist as $key => $value) {
         echo "<option value=".$value.">".$value."</option>";
        }
        echo "</select>";
  ?>

Comments

0
(PHP 4, PHP 5, PHP 7)

for loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is:

for (expr1; expr2; expr3) {
    statement
}

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 is evaluated (executed).

Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part. expr2 being empty means the loop should be run indefinitely (PHP implicitly considers it as TRUE, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional break statement instead of using the for truth expression. check doc

FOREACH LOOP

(PHP 4, PHP 5, PHP 7) The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (array_expression as $value) {
    statement
}
foreach (array_expression as $key => $value) {
    statement
}

The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).

The second form will additionally assign the current element's key to the $key variable on each iteration. DOC

AND Yes your solution is here

<select>
    <?php
        $citylist = array(
            'Adana',
            'Adıyaman',
            'Afyonkarahisar',
            'Ağrı',
            'Amasya',
            'Ankara'
        );
        echo populate_option($citylist, "");
    ?>
</select>

Your function goes here

<?php

function populate_option($citylist, $selected_option = "")
{
    foreach ($citylist as $city) {
        if ($selected_option !== "" && $city === $selected_option) {
            echo "<option value='".$city."' selected>$city</option>";
        } else {
            echo "<option value='".$city."'>$city</option>";
        }

    }
}

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.