多维数组,获取递归变量输出的问题

时间:2010-03-08 14:51:12

标签: php forms arrays multidimensional-array

我正在研究多维数组,但我遇到了问题

想象一下,我有一家公司可以订购一些家具 FX 5椅子的不同类型的宽度不同标准价格不同的折扣(%)和折扣后的价格现在是什么

如果公司可以选择5把椅子中的3把到他们的设计范围,并通过复选框添加每把椅子。然后我需要设置该单位的折扣和“最终价格”并且我要反复进行,所以我可以在以后添加更多的椅子。

我的想法是这样的

请注意,如果没有折扣,它必须能够在折扣中添加一个空字段,并且我必须能够读取给定单位的给定输入的值

我希望你能帮助我并感谢你的努力 的Mikkel

<?php
mysql_connect('localhost', 'root', '') or 
die("Could not connect: " . mysql_error());
mysql_select_db("hassberg");
?>

<?php

$furniture_id = $_POST['furniture_id'];
$discount = $_POST['discount'];
$netto_price = $_POST['netto_price'];

if (isset($furniture_id)){


$get_info = array($furniture_id, $discount, $netto_price); 

$counter = 0;
$counter2 = 0;
foreach($get_info as $shop){
    echo "id: " . $shop[$counter][$counter02]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
$counter++;
    if ($counter2 > 3){
        $counter2 = 0;
    }
}

//OR

$shop2 = array($furniture_id, $discount, $netto_price); 

foreach ($row as $rows){
    echo "<li><b>The row number $rows</b>";
    echo "<ul>";

    for ($col = 0; $col < 3; $col++){
        echo "<li>".$shop2[$rows][$col]."</li>";
    }

    echo "</ul>";
    echo "</li>";
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>tester</title>
</head>
<body>

<form name="create01" method="post" action="<?php echo $PHP_SELF; ?> ">
<?php 
$result = @mysql_query("SELECT id
                       FROM furniture_model;");
if (mysql_num_rows($result) == 0) // Exits if your db table is empty{
  echo 'I have no rows to display';
  exit;
}
else{
while($row_model = mysql_fetch_assoc($result)){
    echo $row_model['id'].
        '<input type="checkbox" class="customer_rel_check" name="chosen_model_id[]" value="'.$row_model['id'].'" /> 
         <input type="text" name="discount[]" />% 
         <input type="text" name="netto_price[]" />€ <br />'; //netto_price will be cumulated of the standard price and the discount
}
}
?>

<input type="submit" name="send" value="create" />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

嗯,我找到了一个答案,它并不像我想要的那样完美,但我的工作让我很开心

我必须在包含model_id的表单中添加一个隐藏字段,以便我可以将它与复选框发送的model_id匹配

<?php
mysql_connect('localhost', 'root', '') or 
die("Could not connect: " . mysql_error());
mysql_select_db("hassberg");

$customer_id = 4;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>tester</title>
</head>
<body>
<?php
$sql = 'INSERT INTO tabelname (customer_id, model_id, discount, netto) VALUES ';

if(isset($_POST['chosen_model_id']) && count($_POST['chosen_model_id']) > 0){
$count = count($_POST['chosen_model_id']);
$i = 1;
foreach($_POST['chosen_model_id'] as $value){
    $key = array_search($value, $_POST['hidden_furniture_id']);

    $sql .= '(' . $customer_id . ',' . 
              $_POST['hidden_furniture_id'][$key] ."," . 
              $_POST['netto_price'][$key] ."," . 
              $_POST['discount'][$key] . ")"; 
    $sql .= ($i<$count) ? ", " : "";
    $i++;
}
    echo '<p>' . $sql . '</p>';     
}
?>

<form name="create01" method="post" action="<?php echo $PHP_SELF; ?> ">
<?php 
$result = mysql_query("SELECT id
           FROM furniture_model;");
if (mysql_num_rows($result) == 0){ // Exits if your db table is empty
  echo 'I have no rows to display';
  exit;
}else{
while($row_model = mysql_fetch_assoc($result)){
    echo $row_model['id'].
        '<input type="checkbox" class="customer_rel_check" name="chosen_model_id[]" value="'.$row_model['id'].'" /> '.
        '<input type="text" name="discount[]" />% ' .
        '<input type="text" name="netto_price[]" />€ ' .
        '<input type="hidden" name="hidden_furniture_id[]" value="' . 
                    $row_model['id'] . '" /> <br />'; 
                    //netto_price will be cumulated of the standard price and the discount 
}
}
?>
<input type="submit" name="send" value="create" />
</form>
</body>
</html>