1

I would like to start out saying I don't want to use Javascript....

I have an array which is returning correctly I believe, however I am only having the first entry in the array display in the drop down list.

Is this a problem with the array? or with this function?

foreach($array as $key=>$value){
    $html = "<option value='$key'>$value</key>";
}

echo "<select name="process">$html</select>";

3 Answers 3

2

You have to use the concatenation operator (.):

$html = '';
foreach($array as $key => $value)
{
    $html.= "<option value='$key'>$value</option>";
}

echo "<select name=\"process\">$html</select>";

However, looking at the function you posted earlier, mysql_fetch_assoc only returns a single row at a time. You need to loop over that, instead. The following should suffice:

function tasks_list($p_id) { 
    $project_tasks = array(); 
    $p_id = (int)
    $p_id; 
    $func_num_args = func_num_args(); 
    $func_get_args = func_get_args(); 

    $result = mysql_query("SELECT task_name FROM tasks WHERE project_id = $p_id");
    while($project_tasks = mysql_fetch_assoc($result))
    {
        $html.= "<option value='".$project_tasks['task_name']."'>".$project_tasks['task_name']."</option>";
    }

    echo "<select name=\"process\">$html</select>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi ben this is perfect and working great! Although it displays the select box twice? I assume I am referencing the function correctly? tasks_list($p_id)
Yes, that's the correct way to call the function, but you must be calling it twice.
1

You need to concat the strings. Start by initializing $html to an empty string before starting your for loop.

$html = '';
foreach($array as $key => $value)
{
    $html .= "<option value='$key'>$value</key>";
    // same as $html = $html . "<option value='$key'>$value</key>";
}

Note: There is also a typo in your code:

echo "<select name="process">$html</select>";

should either be:

echo "<select name=\"process\">$html</select>";

OR

echo "<select name='process'>$html</select>";

if that's what you meant.

Comments

1
$html = "";
foreach ($array as $key => $value){
    $html .= "<option value='$key'>$value</option>";
}
echo "<select name='process'>$html</select>";

= reassigns; .= appends

If you use = it reassigns $html in every iteration, so that $html will contain the result of the last iteration - one single option.

10 Comments

Okay, I am still only recieving one value, does this mean that there is a problem with the array?
Are you sure that you haven't done any error while copying my code?
yes 100%, would you be able to take a look at my array please?
@user2286553 That will only return a single result.
Yes the array only contains the first result. $result = mysql_query("SELECT task_name FROM tasks WHERE project_id = $p_id"); while ($task = mysql_fetch_assoc($result)) $project_tasks[] = $task;
|

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.