Jump to content

Recommended Posts

Hello,

 

I have the following code which echo's out results from my database along with a checkbox (To select an Item) and textbox (To enter the quantity desired) after each output. Here is a screenshot of my page layout:

 

40266340du7.jpg

 

The code which displays the above is this:

 

echo  "	
<p class=main2>
<strong>{$ProductName}</strong> - 
({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 
<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity\" type=\"text\" size=\"3\" maxlength=\"3\" /></span>
</p>
";	

 

The idea is that if a user decides to select an item using the checkbox, they must state the quantity in the textbox. Once they have done this they can hit submit and the order details get placed into my database. At the moment i can get the selected items into the database but cannot get the quantities to enter correctly and have no idea what to do. Im finding it hard to get a relationship between the quantities and those items selected.

 

The code to format the selected item numbers is this: (e.g #14, #21, #45)

 

$orderid = $_POST['orderid'];
foreach($orderid as $value){
echo "<span class=\"main\">$value, </span>";
$value = implode(",",$_POST['orderid']);
}

 

I now need some code to say, "if item number #14 is selected, get the corresponding quanitity".

 

Any help? or is my description of what i want too vague?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/
Share on other sites

echo  "	
<p class=main2>
<strong>{$ProductName}</strong> - 
({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 
<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span>
</p>
";

 

foreach($_POST['orderid'] as $id){
  echo $id.": ".$_POST['quantity'][$id]."<br>";
}

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453459
Share on other sites

Thanks so much,

 

nearly there!

 

if using your:

 

foreach($_POST['orderid'] as $id){
  echo $id.": ".$_POST['quantity'][$id]."<br>";
}

 

How would i get the results into my SQL query?

 

$sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$notes','$id')";

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453481
Share on other sites

Its the table which holds order information. I appreciate i am not doing this in the most appropriate fashion but it has to be done this way.

 

The Order numbers columb in the image below is how the order numbers are stored when a user selects them. I need the quantities to be collected and stored the same way.

 

48741538ex7.jpg

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453506
Share on other sites

<?php
  $orderid = array();
  $quantity = array();
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  $orderid = implode(',',$orderid);
  $quantity = implode(',',$quantity);
  $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')";
?>

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453528
Share on other sites

hmm..only one quantity is going into the database.

 

If i check item number 12 and 20, and type a quantity of 3 x #12's and 2 x #20's

 

i get:

 

|quantity|ordernotes|

| 3        | 12, 20    |

 

The quantity 2 never makes it to the database. I'll show you the whole php file. Beware though, it's quite messy.

 

<form name="the_form" id="the_form" method="post" action="<?php $_SERVER[php_SELF]; ?>">
<?php

// connect to access database and find the table 'Product1'
$sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg());


// define variables
$prevCat='';
while($row = odbc_fetch_array($sql2))
{
$Category = $row["CategoryName"];
$ProductId = $row["ProductId"];
$ProductName = $row["ProductName"];
$Price = $row["Price"];
$Type = $row["Type"];
$Habit = $row["Habit"];
$Colour = $row["Colour"];
$Price = $row["Price"];
$Status = $row["Status"];

// find out if category has changed
// if so, print it, and it's contents
if ($Category != $prevCat)  {
	echo "<p class=title><u>$Category</u></p>";
}
echo  "	
<p class=main2>
<strong>{$ProductName}</strong> - 
({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 
<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span>
</p>
";	

$prevCat = $Category;
}
?>
<hr />

<div align="center">
  <p align="left"><span class="main"><Br />
    </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br />
    <br />
    <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea>
     <img src="images/eg.jpg" /><br />
     <br />
     <input name="submit" type="Submit" value="Submit" />
    </p>
  </p>
</div>
</form>

<?php
if ($_POST[submit] == "Submit")
{
  $orderid = array();
  $quantity = array();
  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];
  }
  $orderid = implode(',',$orderid);
  $quantity = implode(',',$quantity);
  }
  
?>

<?php
$conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!');

$date = date('d m y');
$notes = htmlspecialchars($_POST['ProductNotes']);



// execute SQL Statement
$sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')";


$rs = @odbc_exec($conn,$sql);

if (!$rs)
{

echo "";

}

else

{

echo "<span class=\"main\">The record was successfully inserted.</span>";

}
odbc_close($conn);
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453562
Share on other sites

try this..

 

<form name="the_form" id="the_form" method="post">
<?php
  //Get products
  $sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg());

  // define variables
  $prevCat='';
  while($row = odbc_fetch_array($sql2)){
    $Category = $row["CategoryName"];
    $ProductId = $row["ProductId"];
    $ProductName = $row["ProductName"];
    $Price = $row["Price"];
    $Type = $row["Type"];
    $Habit = $row["Habit"];
    $Colour = $row["Colour"];
    $Price = $row["Price"];
    $Status = $row["Status"];
  
    // find out if category has changed
    // if so, print it, and it's contents
    if($Category != $prevCat){
      echo "<p class=title><u>{$Category}</u></p>";
    }
    echo  " 
<p class=main2>
<strong>{$ProductName}</strong> - 
({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) 
<span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span>
</p>
";  
  
    $prevCat = $Category;
  }
?>
<hr />

<div align="center">
  <p align="left"><span class="main"><Br />
    </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br />
    <br />
    <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea>
     <img src="images/eg.jpg" /><br />
     <br />
     <input name="submit" type="Submit" value="Submit" />
    </p>
  </p>
</div>
</form>

<?php
  if($_POST['submit'] == "Submit"){
    //Connect to database
    $conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!');

    $orderid = array();
    $quantity = array();
    foreach($_POST['orderid'] as $id){
      $orderid[] = $id;
      $quantity[] = $_POST['quantity'][$id];
    }
    $orderid = implode(',',$orderid);
    $quantity = implode(',',$quantity);

    $date = date('d m y');
    $notes = htmlspecialchars($_POST['ProductNotes']);

    //execute SQL Statement
    $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$notes','$cust_name','$date','$orderid','$quantity')";
    $rs = @odbc_exec($conn,$sql);

    if(!$rs)
      echo "Error: ".odbc_errormsg();
    else
      echo "<span class=\"main\">The record was successfully inserted.</span>";

    odbc_close($conn);
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/88572-quantity/#findComment-454379
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.