【问题标题】:PHP Array and HTML checkboxesPHP 数组和 HTML 复选框
【发布时间】:2011-09-23 21:34:08
【问题描述】:

我有一个字符串保存在MySQL数据库中,字符串如下:

item:19.99/item2:24.99

我已经设法将数组拆分为:

Array(
[0] => Array
    (
        [0] => item
        [1] => 19.99
    )

[1] => Array
    (
        [0] => item2
        [1] => 24.99
    ))

我需要遍历数组,显示名为“categories”的 MySQL 表中的所有项目(其中“item”是一个类别),但棘手的一点是使数组中的复选框被选中,然后是一个包含价格的文本框

我知道这很难解释,唯一能让我更容易理解的方法是提供所需的 HTML 输出:

<input type="checkbox" value="item" checked="checked" />Item <input type="text" value="19.99"/>
<input type="checkbox" value="item1" />Item 1 <input type="text"/>
<input type="checkbox" value="item2" checked="checked" />Item 2 <input type="text" value="24.99"/>
<input type="checkbox" value="item3" />Item 3 <input type="text"/>
<input type="checkbox" value="item4" />Item 4 <input type="text"/>

【问题讨论】:

  • item3 的价格是多少,存储在哪里?其他项目名称存储在哪里?
  • 我们可以从您的数据库中获取一些格式/数据吗?类别的东西?

标签: php arrays loops


【解决方案1】:

首先,您应该重组您的“选定项目”数组,使其更易于使用。看我的例子。然后,当您遍历所有商品时,只需检查所选数组中是否有价格。

示例:

// Updated your array of selected item so that the name is the key and the price the value.
//
// For example:
// 
// Array(
// [item] => 19.99
// [item2] => 24.99
// )
foreach ( $selected_items as $key => $selected_item )
{
  $selected_items[$selected_item[0]] = $selected_item[1];
  unset($selected_items[$key]);
}

// ...

// While looping through all of the items from your SQL query:
while ( $item = mysql_fetch_assoc($result) )
{
  if ( ! empty($selected_items[$item]) )
    echo '<input type="checkbox" value="' . $item['key'] . '" selected="selected" />' . $item['name'] . '<input type="text" value="' . $selected_item[$item['key']] . '" />';
  else
    echo '<input type="checkbox" value="' . $item['key'] . '" />' . $item['name'] . '<input type="text"/>';
}

PS:您可能希望在复选框和输入字段中添加name 属性。

根据您在下面的评论

将您的 foreach 循环更改为:

foreach($b as $c)
{
  list($key, $value) = explode(':', $c);
  $d[$key] = $value;
}

【讨论】:

  • 是的,弗朗索瓦说的(而且打字速度比我快得多)。 +1 :)
  • 我知道这是业余的,但我该如何重组数组,目前我有这个:$b = explode("/", $a['cat']); foreach($b as $c){ $d[] = explode(":",$c);其中 $a['cat'] 是字符串
  • @Lee Price - 我更新的答案有一个新的foreach 循环供您使用,它将执行我原始答案中的foreach 循环所做的事情(即 item_name => 价格)。从那里,当您遍历其余项目时,您可以检查它是否存在于 $d 数组中,如我的示例所示。
  • 你的传奇!我没有完全按照你的建议做,我使用 array_key_exists() 代替,但由于你对数组的重组,我得到了它的工作,所以我要把这个全部归功于你:) 再次感谢
  • @Lee Price - 我很高兴能帮上忙。 :)
【解决方案2】:

如果没有您的 MySQL 组织信息,这是我能得到的最好的信息:

<?php

    $array = array(array('item', '19.99'),
            array('item1', NULL),
            array('item2',  '24.99'),
            array('item3', NULL),
            array('item4', NULL));

    foreach ($array as $item) {

        echo '<input type="checkbox" value="' . $item[0] . '"' . ($item[1] ? ' checked="checked"' : '') . ' />' . $item[0] . '<input type="text"' . ($item[1] ? ' value="' . $item[1] . '"' : '') . "/><br />\n";
    }

?>

输出:

<input type="checkbox" value="item" checked="checked" />item<input type="text" value="19.99"/><br />
<input type="checkbox" value="item1" />item1<input type="text"/><br />
<input type="checkbox" value="item2" checked="checked" />item2<input type="text" value="24.99"/><br />
<input type="checkbox" value="item3" />item3<input type="text"/><br />
<input type="checkbox" value="item4" />item4<input type="text"/><br />

http://codepad.org/fGpGCXwm

希望你能剖析它并让它为你工作。

【讨论】:

  • 谢谢你,但这个方法不适合我,因为这些项目与检查项目位于不同的表中
  • 如果您能提供一些数据库结构,那么我可以进行这些更改。
【解决方案3】:

这样的东西有用吗?

<?php
foreach($array as $values){
    echo '<input type="checkbox" value="item" checked="checked" />' . $values[0] . ' <input type="text" value="' . $values[1] . '"/>'
}
?>

我不确定你如何知道一个值是否被检查,但是一旦你在一个变量中或作为数组的一部分使用它,只需使用 if 语句来输出

checked="checked" 

如果需要的话。

【讨论】:

    【解决方案4】:

    我不确定您使用的是哪种后端,但在数据库中存储数据的更好方法是不要在同一字段中组合多种类型的数据,而是使用数字 ID 而不是基于文本的身份证。您可以创建如下表格:

    id       name          price
    ----------------------------------
    1        Item          19.99
    2        Item 1        0
    3        Item 2        24.99
    4        Item 3        0
    5        Item 4        0
    

    请注意,我的示例显示没有输入价格的“0”。这是因为您可能希望将价格列存储为十进制数(有两位小数),这会使空值默认为“0”。没关系。您的用户界面不必显示“0”。

    然后当你从数据库中检索数据时,你可能会得到一个这样的数组:

    Array(
        [0] => Array
        (
            [id] => 1
            [name] => Item
            [price] => 19.99
        )
        [1] => Array
            [id] => 2
            [name] => Item 1
            [price] => 0
        )
    )
    and so on...
    

    那么你可以在你的 .php 文件中显示如下的值:

    <?php
    foreach ($data as $row) {
        explode($row);
    
        if ($price > 0) {
            $checked = 'checked="checked"';
        } else {
            $checked = '';
            $price = '';
        }
    
        $pattern = '<input type="checkbox" name="categories[%s]" value="1" %s> '
            . '%s <input type="text" name="prices[%s]" value="%s">';
    
        echo sprintf($pattern, $id, $checked, $name, $id, $price);
    }
    ?>
    

    那么在检索表单输入时,你想要的变量是$_POST['categories']$_POST['prices']

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 2014-09-12
      • 2011-09-25
      • 1970-01-01
      • 2012-10-16
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多