Jump to content

pass variable


searls03

Recommended Posts

so now it is always inserting two, one with proper record, the other blank....like picture shows. 

 

here is where sessions are set:

<?php
include_once("connect.php");
session_start();








foreach($_POST["product"] AS $key => $val) {
$_SESSION['product'] = $val;
$_SESSION['month'] = $_POST['month'][$key];
$_SESSION['day'] = $_POST['day'][$key];
$_SESSION['year'] = $_POST['year'][$key];
$_SESSION['date'] = $_POST['date'][$key];
$_SESSION['price'] = $_POST['price'][$key];
$_SESSION['qty'] = $_POST['qty'][$key];
$_SESSION['id'] = $_POST['id'][$key];
$_SESSION['total'] = $_POST['total'][$key];
$_SESSION['academy'] = $_POST['academy'][$key];
$_SESSION['priceunit'] = $price * $qty;


}

?>

 

and then the other is exactly what you gave me :(

post-112513-13482403228963_thumb.png

Link to comment
Share on other sites

oh, here is the code that the form that submits the arrays come from:

<form name="pay" class="pay" method="POST" action="collect.php"> 
   <p>
     <?php if ($total == ""){$total = "0";} ?>
     <?php echo "$".$total.""; ?>
     <?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id']." && product123 !=''");
while($row = mysql_fetch_array($sql)){
$product = $row["product123"];
$price1 = $row["price"];
$id = $row["product_id"];
$qty = $row["quantity"];



$month = date("F Y"); 
$day = date("d"); 
$year = date("Y"); 
$date = date("Y-m-d");
?>
     <input name="product[]" type="hidden" value="<?php echo $product; ?>" /><input name="academy[]" type="hidden" value="<?php echo $academy; ?>" /><input name="month[]" type="hidden" value="<?php echo $month; ?>" /><input name="day[]" type="hidden" value="<?php echo $day; ?>" /><input name="year[]" type="hidden" value="<?php echo $year; ?>" /><input name="date[]" type="hidden" value="<?php echo $date; ?>" /><input name="price[]" type="hidden" value="<?php echo $price1; ?>" /><input name="id[]" type="hidden" value="<?php echo $id; ?>" />
  <input name="qty[]" type="hidden" value="<?php echo $qty; ?>" /><input name="total" type="hidden" value="<?php echo $total; ?>" />
     
     
     
  <?php
}
?> 
  <?php if ($total == " "){$total = "0";} ?><input type="submit" name="pay" id="pay" class="pay1" value="" />
   </p>
</form>

Link to comment
Share on other sites

           

<form name="pay" class="pay" method="POST" action="collect.php">
    <p>
        <?php if ($total == ""){$total = "0";} ?>
        <?php echo "$".$total.""; ?>
        <?php
// Query member data from the database and ready it for display
        $sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id']." && product123 !=''");
        while($row = mysql_fetch_array($sql)){
            $product = $row["product123"];
            $price1 = $row["price"];
            $id = $row["product_id"];
            $qty = $row["quantity"];



            $month = date("F Y");
            $day = date("d");
            $year = date("Y");
            $date = date("Y-m-d");
            ?>
<input name="product[]" type="hidden" value="<?php echo $product; ?>" />
<input name="academy[]" type="hidden" value="<?php echo $academy; ?>" />
<input name="month[]" type="hidden" value="<?php echo $month; ?>" />
<input name="day[]" type="hidden" value="<?php echo $day; ?>" />
<input name="year[]" type="hidden" value="<?php echo $year; ?>" />
<input name="date[]" type="hidden" value="<?php echo $date; ?>" />
<input name="price[]" type="hidden" value="<?php echo $price1; ?>" />
<input name="id[]" type="hidden" value="<?php echo $id; ?>" />
<input name="qty[]" type="hidden" value="<?php echo $qty; ?>" />
<input name="total" type="hidden" value="<?php echo $total; ?>" />

<?php
        }
        ?>
        <?php if ($total == " "){$total = "0";} ?><input type="submit" name="pay" id="pay" class="pay1" value="" />
    </p>
</form>

 

simply setting your name to academy[], wont add it to an array. you are still in html code.

 

something like:

 

<form name="pay" class="pay" method="POST" action="collect.php">
    <p>
        <?php if ($total == ""){$total = "0";} ?>
        <?php echo "$".$total.""; ?>
        <?php
// Query member data from the database and ready it for display
        $sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id']." && product123 !=''");
        $productnumber = 1; // set a counter for the products
        while($row = mysql_fetch_array($sql)){
            $product = $row["product123"];
            $price1 = $row["price"];
            $id = $row["product_id"];
            $qty = $row["quantity"];



            $month = date("F Y");
            $day = date("d");
            $year = date("Y");
            $date = date("Y-m-d");
            ?>

            <input name="product<?php echo $productnumber; ?>" type="hidden" value="<?php echo $product; ?>" />
            <input name="academy<?php echo $productnumber; ?>" type="hidden" value="<?php echo $academy; ?>" />
            <input name="month<?php echo $productnumber; ?>" type="hidden" value="<?php echo $month; ?>" />
            <input name="day<?php echo $productnumber; ?>" type="hidden" value="<?php echo $day; ?>" />
            <input name="year<?php echo $productnumber; ?>" type="hidden" value="<?php echo $year; ?>" />
            <input name="date<?php echo $productnumber; ?>" type="hidden" value="<?php echo $date; ?>" />
            <input name="price1<?php echo $productnumber; ?>" type="hidden" value="<?php echo $price1; ?>" />
            <input name="id<?php echo $productnumber; ?>" type="hidden" value="<?php echo $id; ?>" />
            <input name="qty<?php echo $productnumber; ?>" type="hidden" value="<?php echo $qty; ?>" />
            <input name="total" type="hidden" value="<?php echo $total; ?>" />

            <?php
            $productnumber ++; //increase the product number counter
        }
        ?>
        <?php if ($total == " "){$total = "0";} ?><input type="submit" name="pay" id="pay" class="pay1" value="" />
    </p>
</form>

 

that way you'll have multiple hidden boxes for EACH product, whereas no you only have one set, which gets overwritten with additional products.

 

but then you'll  receive a bunch of names like product1, product2, product3 etc . that can be dealth with but you want it dynamic so it will scale. in one of my more recent posts i walked someone through something similar. it involved counting all the session elements, dividing that by the number in each set (ie 9 for you, or 10 if you count total for each product - otherwise subtract 1 for total)

 

and using that number of sets you now have, you can insert multiple values into mysql at once

 

like:

INSERT into table (product,academy, month) VALUES ('product1', 'academy1' 'month1'), ('product2', 'academy2' 'month2').

 

and using a counter you can have that done automatically. as i said it's in one of my recent posts.

 

it's late so im done for the nite, ill check back on it tomorrow though. good luck.

Link to comment
Share on other sites

because the session is only setting one products info

 

that will only pass one products info to the next page

<?php
foreach($_POST["product"] AS $key => $val) {
$_SESSION['product'] = $val;
$_SESSION['month'] = $_POST['month'][$key];
$_SESSION['day'] = $_POST['day'][$key];
$_SESSION['year'] = $_POST['year'][$key];
$_SESSION['date'] = $_POST['date'][$key];
$_SESSION['price'] = $_POST['price'][$key];
$_SESSION['qty'] = $_POST['qty'][$key];
$_SESSION['id'] = $_POST['id'][$key];
$_SESSION['total'] = $_POST['total'][$key];
$_SESSION['academy'] = $_POST['academy'][$key];
$_SESSION['priceunit'] = $price * $qty;
}
?>

 

you'd need a counter ans something like

 

<?php
$x = 1;
foreach($_POST["product"] AS $key => $val) {
$_SESSION[$x]['product'] = $val;
$_SESSION[$x]['month'] = $_POST['month'][$key];
$_SESSION[$x]['day'] = $_POST['day'][$key];
$_SESSION[$x]['year'] = $_POST['year'][$key];
$_SESSION[$x]['date'] = $_POST['date'][$key];
$_SESSION[$x]['price'] = $_POST['price'][$key];
$_SESSION[$x]['qty'] = $_POST['qty'][$key];
$_SESSION[$x]['id'] = $_POST['id'][$key];
$_SESSION[$x]['total'] = $_POST['total'][$key];
$_SESSION[$x]['academy'] = $_POST['academy'][$key];
$_SESSION[$x]['priceunit'] = $price * $qty;
$x++;
}
?>

 

then session will save a multidimensional array, which will hold all the relevant info for each product counted in the foreach loop. right now they are being overwritten because they are all being saved to same session variable. ie when it processes product 3 it overwrites what it had for 2, as there is only one 'academy" variable in that session array. adding $x and a counter adds another dimension to the array, so it can save those individually

Link to comment
Share on other sites

If I were to put the arrays directly on the page after the form, and then use that page to submit, how would I make the page store the arrays for after a form on that page is submitted?  so like store the arrays so they don't get submitted right away to make sure the sale is confirmed before submitting?

Link to comment
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.