vmicchia Posted October 4, 2010 Share Posted October 4, 2010 Ok, I have a couple multidimensional arrays that I am having problems with but it's all the same problem so I'll just use one. Basically it just takes input from a form and either creates or appends the array and then makes it a session variable. My problem seems to the the appending part. When I go to show the array (print_r() atm) it has only made the array with the newest record rather than adding the newest record to the existing set. Anyway here's the code. <?php session_start(); ?> $cushion = $_POST["Order_a_cushion_group_Cushion"]; $fabric = $_POST["Order_a_cushion_group_Fabric"]; $fill = $_POST["Order_a_cushion_group_Fill"]; $button = $_POST["Order_a_cushion_group_Button"]; $contWelt = $_POST["Order_a_cushion_group_Contrasting_Welt"]; $contWeltFab = $_POST["Order_a_cushion_group_Contrasting_Welt_Fabric"]; $zip = $_POST["Order_a_cushion_group_Zipper"]; $quantity = $_POST["Order_a_cushion_group_Quantity"]; if($contWelt == "N"){ $contWeltFab = "N/A"; } if($_POST["Default_submit"] == "Checkout"){ if(!isset($_SESSION["cushArray"])){ $cushArray = array(array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity) ); $_SESSION["cushArray"] = $cushArray; }else{ $sub = array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity )); $_SESSION["cushArray"] = $sub; } //header("Location:orderUserInfo.php"); print_r($_SESSION["cushArray"]); }elseif($_POST["Default_submit"] == "Add more"){ //echo "WTF"; if(!isset($_SESSION["cushArray"])){ $cushArray = array(array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity) ); $_SESSION["cushArray"] = $cushArray; }else{ $sub = array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity ); $_SESSION["cushArray"] = $sub; } //header("Location:orderPage.php"); print_r($_SESSION["cushArray"]); } ?> any help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 4, 2010 Share Posted October 4, 2010 to append an element onto the end of an array, use []: $_SESSION["cushArray"][] = $cushArray; Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 4, 2010 Author Share Posted October 4, 2010 Thanks so much. That was so simple. I'd been looking around but there's not much on using sessions with arrays and such. I didn't know if maybe the session unset the array or something. Anyway, Thanks so much. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 4, 2010 Share Posted October 4, 2010 As noted above, your problem is here: $sub = array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity ); $_SESSION["cushArray"] = $sub; The last lines tells PHP to REPLACE $_SESSION["crushArray"] with $sub. If you want to add $sub to $_SESSION["crushArray"] as a new index, use the code provided above or: $_SESSION['crushArray'][] = $sub; Just as an aside, you should use single quotes when you don't want PHP to parse a string: $test = 'test'; echo 'This is a $test'; // This is a $test echo "This is a $test"; // This is a test echo 'This is a '.$test; // This is a test Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 4, 2010 Author Share Posted October 4, 2010 Just as an aside, you should use single quotes when you don't want PHP to parse a string: $test = 'test'; echo 'This is a $test'; // This is a $test echo "This is a $test"; // This is a test echo 'This is a '.$test; // This is a test Thanks I'll do that in the future. Also my apologies for leaving that in the code. I had had problems with the if statements and as testing it. Left that in on accident. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 4, 2010 Author Share Posted October 4, 2010 just ran into another problem. It seems that the array records don't start getting a identifier until the second entry in the array. so when I print the array it looks like this: Array ( [Cushion] => Test 1 [Fabric] => Test [Fill] => Foam [button] => red [ContWelt] => [ContWeltFabric] => Test [Zipper] => black [Quantity] => 68 [0] => Array ( [Cushion] => Test 1 [Fabric] => Test [Fill] => Reticulated Foam [button] => red [ContWelt] => Y [ContWeltFabric] => Test [Zipper] => black [Quantity] => 25 ) [1] => Array ( [Cushion] => Test 2 [Fabric] => Test 2 [Fill] => Reticulated Foam [button] => blue [ContWelt] => N [ContWeltFabric] => N/A [Zipper] => black [Quantity] => 4 ) ) so am I not creating or appending the array correctly? and also as A side note how would I delete a record from this array? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 4, 2010 Share Posted October 4, 2010 I'd make the following changes. Firstly, in order to remove stuff from the array you're going to need to give cart items some kind of identifier. My suggestion would be to create an incrementing variable you store in your session: $_SESSION['identifier'] = 1; Then when you make your $cushArray, use this identifier as the index (and then make sure to increment the identifier so that you don't reuse it!). That brings me to my next point, whenever you're checking if the $_SESSION['cushArray'] exists, don't declare an array(array(, just use that opportunity to initialize the $_SESSION variable, this will break your need to do the assignment stuff in both the if and the else, here's an example: if(!isset($_SESSION['cushArray'])){ $_SESSION['cushArray'] = array(); }//Actually, the brackets aren't necessary here, when an "if" statement has no brackets // it will automatically only do the following line when the if statement is true, and will skip // the 1st line after the if when the if is false. $stuffToAddArray = array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity) ); $_SESSION['cushArray'][$_SESSION['identifier']] = $stuffToAddArray; $_SESSION['identifier']++; // Don't forget this step!! Notice that the else statement is entirely eliminated, and now you've shortened your code by only having to write the creation of the new array in 1 place? To delete items from the cart, you now merely have to write a script that unsets $_SESSION['cushArray'][$identifier_to_be_deleted]. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 4, 2010 Author Share Posted October 4, 2010 Ok that makes sense. The way I was planning on deleting something was to display all the records in the array in a table with a remove button. So when I do this in a for loop will it automatically start with the record at 0 how will I be able to retrieve the identifier for this? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 4, 2010 Share Posted October 4, 2010 Ok that makes sense. The way I was planning on deleting something was to display all the records in the array in a table with a remove button. So when I do this in a for loop will it automatically start with the record at 0 how will I be able to retrieve the identifier for this? Depends exactly on how you wind up looping, this would be my suggestion: foreach ($_SESSION['cushArray'] as $key => $value) { // Write/prepare your HTML that will let the user choose what they want to delete // $value is the variable that's the array with all your data in it, like: $value['Cushion'] or $value['Fabric'] // $key will be the numeric identifier for that particular cart item. } Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 4, 2010 Author Share Posted October 4, 2010 This is how I had it set up: if(isset($_SESSION["pillowArray"])){ $num = count($_SESSION["pillowArray"]); for($n = 0; $n < $num; $n++){ echo "<tr>"; echo "<td>".$_SESSION["pillowArray"][$n]["Cushion"]."</td>"; echo "<td>".$_SESSION["pillowArray"][$n]["Fabric"]."</td>"; echo "<td>".$_SESSION["pillowArray"][$n]["ContWelt"]."</td>"; echo "<td>".$_SESSION["pillowArray"][$n]["ContWellFabric"]."</td>"; echo "<td>".$_SESSION["pillowArray"][$n]["Quantity"]."</td>"; echo "<td><form name='delete' id='delete' enctype='multipart/formdata' method='post' action='removePillowOrder.php'>\n <input name='cushion' type='hidden' value='".$_SESSION["pillowArray"][$n]["Cushion"]."'>\n <input name='cushion' type='hidden' value='".$_SESSION["pillowArray"][$n]["Fabric"]."'>\n <input name='cushion' type='hidden' value='".$_SESSION["pillowArray"][$n]["ContWelt"]."'>\n <input name='cushion' type='hidden' value='".$_SESSION["pillowArray"][$n]["ContWeltFabric"]."'>\n <input name='cushion' type='hidden' value='".$_SESSION["pillowArray"][$n]["Quantity"]."'>\n <input name=\"remove\" type=\"submit\" value=\"Remove\">\n </form></td>"; echo "</tr>"; } } but that was because I was uncertain on how to do it well the form part anyway. also do I set the $_SESSION['identifier'] = 1; in the if(!isset($_SESSION['cushArray'])){ $_SESSION['cushArray'] = array(); } because if I did it elsewhere it would always reset the identifier to one correct? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 4, 2010 Share Posted October 4, 2010 As to the identifier, check if it is set where ever you handle your session creation: session_start(); if (!isset($_SESSION['identifier']) $_SESSION['identifier'] = 1; /* Note the lack of brackets? It's okay, the 1st line after the if will only be executed when the if is true, everything after that will be executed normally. */ Before I get into the loop, I'm a bit confused. You're using $_SESSION['cushArray'] up until now, but suddenly I see $_SESSION['pillowArray'] - are these the same thing? If so, why have two names? If not, what exactly is the difference? That being said, I'm going to use 'cushArray' since that's what you've been using up until now. Also, you should break the habit now of writing strings with double quotes, and then using a period, it's redundant and makes PHP waste time (although a really small amount, never-the-less it adds up, and it's more work for the processor). $test = 'test'; echo "This is a $test"; // This is a test echo "This is a ".$test; // This is a test - but this one takes longer for PHP to parse! echo 'This is a '.$test; Now, on to the loop: foreach ($_SESSION['cushArray'] as $key => $value) { echo '<tr>'; echo '<td>'.$value['Cushion'].'</td>'; /* etc. down to where you want to have the delete form */ echo '<td><form name="delete" id="delete" enctype="multipart/formdata" method="post" action="removePillowOrder.php">\n <input name="cushionID" type="hidden" value="'.$key.'" /><input name="remove" type="submit" value="Remove" /></form></td></tr>'; // Note that I had to change all your single quotes in your HTML to double quotes // because I'm using single quotes for the PHP echo command. } In your script that processes removals, you're looking for $_POST['cushionID'] and the value of that variable will be the index you need to remove from $_SESSION['cushArray']. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 the $_SESSION["pillowArray"] is another array like I said in my first post there are more than one array but they have different amounts of values going into them but it's the same idea behind them I just copy pasted the wrong part of my code. The cusharray looks the same just a bit longer my apologies. Anyhow the the values in the cusharray can be the same for different entries. so my problem was I don't want to delete more than one entry by only identifying one key. I'd also Like to thank you for your help so far it is much appreciated. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 Ok I've been working on this a bit and With my current removal function it just clears the entire array. here's my removal code: session_start(); $val = $_POST['cushionID']; remove_element($_SESSION['cushArray'], $val); $_SESSION['cushArray'] = $arr; function remove_element($arr, $val){ foreach ($arr as $key => $value){ if ($arr[$key] == $val){ unset($arr[$key]); } } return $arr = array_values($arr); } header("Location:finishOrder.php"); Also an odd thing that I noticed is that when entries are added to the array they are doubled. Here is the code for adding. I think it's the same as what ialsoagree gave me but perhaps I made a mistake: if (!isset($_SESSION['identifier'])) $_SESSION['identifier'] = 1; if(!isset($_SESSION['cushArray'])){ $_SESSION['cushArray'] = array(); } $addToCushArray = array(Cushion=> $cushion, Fabric=> $fabric, Fill=> $fill, Button=> $button, ContWelt=> $contWelt, ContWeltFabric=> $contWeltFab, Zipper=> $zip, Quantity=> $quantity ); $_SESSION['cushArray'][$_SESSION['identifier']] = $addToCushArray; $_SESSION['identifier']++; // print_r($_SESSION['cushArray']); if($_POST['Default_submit'] == 'Checkout'){ header("Location:orderUserInfo.php"); }else{ header("Location:orderPage.php"); } Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 OK I fixed it so it is not double inserting records into the array but still having problems with removing the records. Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 Ok I fleshed out that problem now with the delteing.. Code is now: $val = $_POST['cushionID'];unset($_SESSION['cushArray'][$val]); ok now I have a mail function that sends this mail but I am uncertain how to put all this information into the body of this email. Here's my current code if anyone has any better Idea's please let me know. session_start();include("includes/openDbConn.php");$order = "INSERT INTO Orders(OrderRecipient, Address, Address2, City, State, Zip, DateOrdered, Status, Company) VALUES('".$_SESSION["recipient"]."', '".$_SESSION["add"]."', '".$_SESSION["add2"]."', '".$_SESSION["city"]."', '".$_SESSION["state"]."', '".$_SESSION["zip"]."', NOW(), 'Pending', 'example')";$resultOrder = mysql_query($order);$getOrderID = "SELECT OrderID FROM Orders ORDER BY OrderID DESC LIMIT 1";$resultGetOrderID = mysql_query($getOrderID);$getOrderIDNum = mysql_fetch_assoc($resultGetOrderID);if(isset($_SESSION["cushArray"])){ $countCush = count($_SESSION["cushArray"]); for($i = 0; $i < $countCush; $i++){ $getCushSKU = "SELECT SKU FROM Cushion WHERE Name = '".$_SESSION["cushArray"][$i]["Cushion"]."'"; $resultGetCushSKU = mysql_query($getCushSKU); $getResultGetCushSKU = mysql_fetch_assoc($resultGetCushSKU); $getFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$_SESSION["cushArray"][$i]["Fabric"]."'"; $resultGetFabricSKU = mysql_query($getFabricSKU); $getResultGetFabricSKU = mysql_fetch_assoc($resultGetFabricSKU); if($_SESSION["cushArray"][$i]["ContWellFabric"] != "N/A"){ $getWeltFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$_SESSION["cushArray"][$i]["ContWellFabric"]."'"; $resultGetWeltFabricSKU = mysql_query($getWeltFabricSKU); $getResultGetWeltFabricSKU = mysql_fetch_assoc($resultGetWeltFabricSKU); }else{ $getResultGetWeltFabricSKU = "N/A"; } $sql = "INSERT INTO (OrderID, ProductSKU, FabricSKU, WeltFabricSKU, Quantity, ButtonColor, ZipperColor, Fill) VALUES('".$getOrderIDNum."', '".$getResultGetCushSKU.", '".$getResultGetFabricSKU.", '".$getResultGetWeltFabricSKU.", '".$_SESSION["cushArray"][$i]["Quantity"].", '".$_SESSION["cushArray"][$i]["Button"].", '".$_SESSION["cushArray"][$i]["Zipper"].", '".$_SESSION["cushArray"][$i]["Fill"].") "; $result = mysql_query($sql); } }if(isset($_SESSION["pillowArray"])){ $countPillow - count($_SESSION["pillowArray"]); for($n = 0; $n < $countPillow; $i++){ $getPillowSKU = "SELECT SKU FROM Pillow WHERE Name = '".$_SESSION["pillowArray"][$n]["Pillow"]."'"; $resultGetPillowSKU = mysql_query($getPillowSKU); $getResultGetPillowSKU = mysql_fetch_assoc($resultGetPillowSKU); $getFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$_SESSION["pillowArray"][$n]["Fabric"]."'"; $resultGetFabricSKU = mysql_query($getFabricSKU); $getResultGetFabricSKU = mysql_fetch_assoc($resultGetFabricSKU); if($_SESSION["pillowArray"][$n]["ContWellFabric"] != "N/A"){ $getWeltFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$_SESSION["pillowArray"][$i]["ContWellFabric"]."'"; $resultGetWeltFabricSKU = mysql_query($getWeltFabricSKU); $getResultGetWeltFabricSKU = mysql_fetch_assoc($resultGetWeltFabricSKU); }else{ $getResultGetWeltFabricSKU = "N/A"; } $pillowSQL = "INSERT INTO OrdersPillow(OrderID, PillowSKU, FabricSKU, WeltFabricSKU, Quantity) VALUES('".$getOrderIDNum."', '".$getResultGetPillowSKU."', '".$getResultGetFabricSKU."', '".$getResultGetWeltFabricSKU."', '".$_SESSION["pillowArray"][$n]["Quantity"]."')"; $PillowResult = mysql_query($pillowSQL); } $to = "example@example.com"; $from = "example@example.com"; $subject = "New Order #'".$getOrderIDNum."'"; $body = print_r($_SESSION["cushArray"])." ".print_r($_SESSION["pillowArray"]); $body = wordwrap($body, 70); if (mail($to, $subject, $body, $from)) { echo "<span style='color:lime;'>E-Mail sent successfully to " . $to . ".</span>"; $_SESSION["recipient"] = ""; $_SESSION["add"] = ""; $_SESSION["add2"] = ""; $_SESSION["city"] = ""; $_SESSION["state"] = ""; $_SESSION["zip"] = "";} header("Location:orderPage.php");} Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 ok I workedthat submit code down. I am now having a problem where it is inserting one less than the actual number of items in the array. I am somewhat lost for this but here is my code: session_start(); include('includes/openDbConn.php'); $order = "INSERT INTO Orders(OrderRecipient, Address, Address2, City, State, Zip, DateOrdered, Status, Company) VALUES('".$_SESSION["recipient"]."', '\n ".$_SESSION["add"]."', '".$_SESSION["add2"]."', '".$_SESSION["city"]."', '".$_SESSION["state"]."', '".$_SESSION["zip"]."', NOW(), 'Pending', 'company')"; $resultOrder = mysql_query($order); $getOrderID = "SELECT OrderID FROM Orders ORDER BY OrderID DESC LIMIT 1"; $resultGetOrderID = mysql_query($getOrderID); $getOrderIDNum = mysql_fetch_array($resultGetOrderID); //echo $getOrderIDNum["OrderID"]; if(isset($_SESSION['cushArray'])){ foreach($_SESSION['cushArray'] as $key => $value){ $getCushSKU = "SELECT SKU FROM Cushion WHERE Name = '".$value['Cushion']."'"; //echo $getCushSKU; $resultGetCushSKU = mysql_query($getCushSKU); $getResultGetCushSKU = mysql_fetch_assoc($resultGetCushSKU); //echo $getResultGetCushSKU['SKU']; $getFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$value['Fabric']."'"; $resultGetFabricSKU = mysql_query($getFabricSKU); $getResultGetFabricSKU = mysql_fetch_assoc($resultGetFabricSKU); if($_SESSION['cushArray'][$i]["ContWeltFabric"] != "N/A"){ $getWeltFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$value['ContWeltFabric']."'"; $resultGetWeltFabricSKU = mysql_query($getWeltFabricSKU); $getResultGetWeltFabricSKU = mysql_fetch_assoc($resultGetWeltFabricSKU); }else{ $getResultGetWeltFabricSKU = "N/A"; } $sqlCush = "INSERT INTO OrdersCushion(OrderID, ProductSKU, FabricSKU, WeltFabricSKU, Quantity, ButtonColor, ZipperColor, Fill) /n VALUES('".$getOrderIDNum['OrderID']."', '".$getResultGetCushSKU['SKU']."', '".$getResultGetFabricSKU['SKU']."', '".$getResultGetWeltFabricSKU['SKU']."', /n '".$value['Quantity']."', '".$value['Button']."', '".$value['Zipper']."', '".$value['Fill']."') "; //echo $sqlCush; $resultCush = mysql_query($sqlCush); } } if(isset($_SESSION['pillowArray'])){ foreach($_SESSION['pillowArray'] as $key => $value){ $getPillowSKU = "SELECT SKU FROM Pillow WHERE Name = '".$value['Pillow']."'"; $resultGetPillowSKU = mysql_query($getPillowSKU); $getResultGetPillowSKU = mysql_fetch_assoc($resultGetPillowSKU); $getFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$value['Fabric']."'"; $resultGetFabricSKU = mysql_query($getFabricSKU); $getResultGetFabricSKU = mysql_fetch_assoc($resultGetFabricSKU); if($value['ContWeltFabric'] != "N/A"){ $getWeltFabricSKU = "SELECT SKU FROM Fabric WHERE Name = '".$value['ContWeltFabric']."'"; $resultGetWeltFabricSKU = mysql_query($getWeltFabricSKU); $getResultGetWeltFabricSKU = mysql_fetch_assoc($resultGetWeltFabricSKU); $getResultGetWeltFabricSKU = $getResultGetWeltFabricSKU['SKU']; }else{ $getResultGetWeltFabricSKU = "N/A"; } /* echo $getResultGetWeltFabricSKU; echo "<br />"; echo "WTF"; echo "<br />"; */ $pillowSQL = "INSERT INTO OrdersPillow(OrderID, PillowSKU, FabricSKU, WeltFabricSKU, Quantity) VALUES('".$getOrderIDNum['OrderID']."',/n '".$getResultGetPillowSKU['SKU']."', '".$getResultGetFabricSKU['SKU']."', '".$getResultGetWeltFabricSKU."', '".$value['Quantity']."')"; /* echo $pillowSQL; echo "<br />"; */ $PillowResult = mysql_query($pillowSQL); } } $to = "example@example.com"; $from = "example@example.php"; $subject = "New Gensun Order #'".$getOrderIDNum."'"; //$body = print_r($_SESSION['cushArray'])." ".print_r($_SESSION['pillowArray']); $body = wordwrap($body, 70); if (mail($to, $subject, $body, $from)) { //echo "<span style='color:lime;'>E-Mail sent successfully to " . $to . ".</span>"; $_SESSION["recipient"] = ""; $_SESSION["add"] = ""; $_SESSION["add2"] = ""; $_SESSION["city"] = ""; $_SESSION["state"] = ""; $_SESSION["zip"] = ""; unset($_SESSION['cushArray']); unset($_SESSION['pillowArray']); } header("Location:orderPage.php"); Quote Link to comment Share on other sites More sharing options...
vmicchia Posted October 5, 2010 Author Share Posted October 5, 2010 Also I'm not really sure how to make the email display the order information. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.