处理数组中的起始索引 - PHP

时间:2022-10-28 22:36:56

I've got an array of users from a database (I'm working in PHP, using CodeIgniter in Sublime)

我从数据库中获得了一组用户(我在PHP中工作,在Sublime中使用CodeIgniter)

I've got a view, that has a select that show display all the users, so in my view, at the top, I have this code (suppose that the arrays has 3 items):

我有一个视图,有一个选择显示所有用户,所以在我看来,在顶部,我有这个代码(假设数组有3项):

<?php
$optionsUsers = array();

$qtyUsers = count($users);  -->

if($qtyUsers > 0){
    $optionsUsers[0]['name'] = 'Choose an option';
    for($i=0; $i < $qtyUsers; $i++){
        $optionsUsers[$i]['id'] = $users[$i]["userId"];
        $optionsUsers[$i]['name'] = $users[$i]["username"];
    }
}
?>

Then, in the select part, I have this:

然后,在选择部分,我有这个:

 <select id="cursadaUsuario">
        <?php
        $qtyOptionsUsers = count($optionsUsers);
        if($qtyOptionsUsers>0){
          for($i=0; $i<$qtyOptionsUsers; $i++){
             if($i == 0){
                echo '<option value="0" disabled selected>Please select an option</option>';
             }else{
                echo '<option value="'.$optionsUsers[$i]['id'].'">'.$optionsUsers[$i]['name'].'</option>';
             }
           }
         }else{
             echo '<option value="">There are no options available</option>';   
         }
         ?>
 </select>

I've assigned to the $optionsUsers array in [0] the string "Choose an option" because when iterating THAT ARRAY in the select, I wanted to display it as disabled and just iterate the rest of the elements as usual

我已经在[0]字符串“选择一个选项”中分配了$ optionsUsers数组,因为当在select中迭代THAT ARRAY时,我想将它显示为禁用,并像往常一样迭代其余的元素

The problem is that array $users starts in 0 --> I've checked it with a foreach and all the users display are actually there:

问题是数组$ users从0开始 - >我用foreach检查了它,所有用户显示实际上都在那里:

foreach ($users as $key => $value){
      print_r($users);
   });

But I wanted [0] to be the text to display, "Choose your option", so if I assign the text to [0], the first item in array $users is never shown :( (in this example, it would iterate the text and 2 of the items, not the 3)

但我希望[0]成为要显示的文本,“选择你的选项”,所以如果我将文本分配给[0],则数组$ users中的第一项永远不会显示:(在此示例中,它将迭代文本和2个项目,而不是3)

If I remove the text to [O], and just iterate the array, all the users are shown correctly, no one is missing, but that means... no text to tell "Choose an option" :/

如果我将文本删除到[O],并且只是迭代数组,所有用户都会正确显示,没有人丢失,但这意味着......没有文字告诉“选择一个选项”:/

I would like it to look like this --> http://www.hkvstore.com/phpreportmaker/doc/images/dropdownselect.png, so I want the text "Choose you option" to be shown AND disabled, and then the elements of the array, in my case, a list of users.

我希望它看起来像这样 - > http://www.hkvstore.com/phpreportmaker/doc/images/dropdownselect.png,所以我希望显示和禁用“选择你选项”文本,然后在我的例子中,数组的元素是用户列表。

Note: I would like to keep using a 'for' loop. Note2: This (Add option selected disabled in PHP) would be kind of similar to what I want to achieve, but still, I don't think I could just change the keys and assign my own to a list of users, the could be changed, added, deleted, etc.. :/

注意:我想继续使用'for'循环。注2:这个(在PHP中选择禁用的添加选项)与我想要实现的类似,但是,我认为我不能只更改密钥并将自己的密钥分配给用户列表,可能是更改,添加,删除等..:/

Ideas? Thanks in advance! :)

想法?提前致谢! :)

2 个解决方案

#1


1  

Unless I'm not understanding you, it's the same as the "No options available" option – it belongs outside the loop:

除非我不理解你,否则它与“No options available”选项相同 - 它属于循环之外:

if ($qtyOptionsUsers > 0) {
  echo '<option value="0" disabled selected>Please select an option</option>';
  for ($i = 0; $i < $qtyOptionsUsers; $i++) {
    echo '<option value="'.$optionsUsers[$i]['id'].'">'.$optionsUsers[$i]['name'].'</option>';
  }
} else {
  echo '<option value="">There are no options available</option>';   
}

#2


1  

you need to push the users data from index 1 rather than from 0 index.Try below code to fix the choose option

你需要从索引1而不是从0索引推送用户数据。尝试下面的代码来修复选择选项

for($i=0; $i <= $qtyUsers; $i++){
   $optionsUsers[$i+1]['id'] = $users[$i]["userId"];
   $optionsUsers[$i+1]['name'] = $users[$i]["username"];
}

#1


1  

Unless I'm not understanding you, it's the same as the "No options available" option – it belongs outside the loop:

除非我不理解你,否则它与“No options available”选项相同 - 它属于循环之外:

if ($qtyOptionsUsers > 0) {
  echo '<option value="0" disabled selected>Please select an option</option>';
  for ($i = 0; $i < $qtyOptionsUsers; $i++) {
    echo '<option value="'.$optionsUsers[$i]['id'].'">'.$optionsUsers[$i]['name'].'</option>';
  }
} else {
  echo '<option value="">There are no options available</option>';   
}

#2


1  

you need to push the users data from index 1 rather than from 0 index.Try below code to fix the choose option

你需要从索引1而不是从0索引推送用户数据。尝试下面的代码来修复选择选项

for($i=0; $i <= $qtyUsers; $i++){
   $optionsUsers[$i+1]['id'] = $users[$i]["userId"];
   $optionsUsers[$i+1]['name'] = $users[$i]["username"];
}