【问题标题】:Multidimensional array, problem with getting recursive variable output多维数组,获取递归变量输出的问题
【发布时间】:2010-03-08 14:51:11
【问题描述】:

我正在处理一个多维数组,但我遇到了问题

假设我有一家可以订购一些家具的公司 外汇 5把椅子不同宽度不同标准价格不同折扣(%)和折扣后的价格现在什么

如果一家公司可以在其设计范围内选择 5 把椅子中的 3 把,并将每把椅子添加为复选框。然后我需要为那个单元设置一个折扣和一个“最终价格”,我需要反复做,这样我以后可以添加更多的椅子。

我的想法是这样的

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

我希望你能帮助我,感谢你的尝试 米克尔

<?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>

【问题讨论】:

  • 听起来像是一项任务。您可能需要添加“家庭作业”标签(如果是这样的话)。
  • 嗯,这是我正在尝试制作的系统,但它并不是真正的功课
  • 我会将您的数据库连接信息排除在示例之外,您看起来在本地主机上,所以真的没问题,但如果这是一个生产框。哎呀!
  • 只是因为它是本地主机,所以我发布了它,所以它不会真正被黑客入侵,密码也已被删除,但感谢您的关注。

标签: php forms arrays multidimensional-array


【解决方案1】:

好吧,我找到了一个答案,它并不像我想要的那样完美,但我工作,所以我很高兴

我必须再次向包含 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>

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 2020-04-28
    • 2017-10-11
    • 1970-01-01
    • 2012-08-13
    • 2021-12-11
    • 2017-12-28
    • 2017-11-28
    • 2012-08-31
    相关资源
    最近更新 更多