views:

273

answers:

1

I’m working on a multi dimensions array but i have a problem

Imagine I have a company that can order some furniture’s Fx 5 chair of different type’s width different standard prices different discount (%) and a price after discount now what

If a company can choose 3 of the 5 chairs to their design range and add each chair thru as checkbox. Then I need to set a discount to that unit and a “final price” and I ned to do it repeatedly, so I could add more chairs later.

My thoughts is something like this

Note that it has to be able to add an empty field in discount if there is no discount and I have to be able to read the values of the given input on of the given unit

I hope that you can help me and thanks for trying 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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>
A: 

Well I found one answer it’s not as perfect as I wanted it to be but I works so I’m happy

I had to add a hidden field to my form containing the model_id again so I could match it with the model_id that the checkbox sends

<?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"&gt;
<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>
nichico