0

Is it possible to loop a function variable from this? I am trying to loop the $item[$a-1]

echo load_func($hid, $item[$a-1]);

And make it something like this but I know this is wrong (just an idea):

echo load_func($hid, for($a=1;$a<=$addctr;$a++){$item[$a-1]});

This is the actual but fail because it loops the whole function.

echo "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
for($a=1;$a<=$addctr;$a++){
     echo load_func($id, $item[$a-1]);
}
echo "</select>";

The purpose of the function is to automatically select an option based from the record saved on a table.

3
  • So why shouldn't your code work? Commented Feb 24, 2015 at 15:05
  • What load_func function does? can you post that code so it can be understood better? Commented Feb 24, 2015 at 15:05
  • there is no question here :-) you just want to change sintax of php? :-) there are many others, try python, prolog perl :-) but if you use php - you already know the answer - here is your loop. you can pass you array or maxnumber of elements as second parameter to your function and loop it inside the function. these 2 options is absolutely enough :-) Commented Feb 24, 2015 at 15:09

2 Answers 2

1

Try to pass the whole item to load_function();

echo load_func($hid, $item);

And deal with every item in the function itself.

function load_func($hid, $item) {
    $return = "<select id='drpopitem-' name='drpopitem[]' size='10' multiple>";
    foreach ($item as $option) $return .= $option;
    $return .= "</select>";
    return $return;
}
Sign up to request clarification or add additional context in comments.

Comments

0

From what I am understanding from your question, you should pass the array $addctr to the function. And inside the function you should put your for loop and do the calculations.

something like:

function load_func($id, $items) {
   $strOptions = "";
   for($a=1;$a<=$items;$a++){
       if($items[$a-1] != $id)
          $strOptions .= "<option>Your value</option>";
       else
          $strOptions .= "<option selected>Your value</option>";
   }
   return $strOptions;
}

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.