Aventurine Posted November 22, 2010 Share Posted November 22, 2010 Hi there, Need a little help here if anyone has the time. In short, I have a page that is dynamic, that sends POST data to a handler, pretty standard stuff. Normally, for me anyway, I know what the field names will be in advance and handle then as usual. In this case the form content is created dynamically from a GUI I created and the names of the fields are suffixed with the product ID. Within the handler I have stripped the POST data down to the main two vars, namely the pid- product ID and qty- quantity ordered and within a loop created a string to explode() like; 9_33_21_66 $oStrArray = explode('_', $ostring); echo "ostring[0] =".$oStrArray[0]; echo "ostring[1] =".$oStrArray[1]; echo "ostring[2] =".$oStrArray[2]; echo "ostring[3] =".$oStrArray[3]; Result => ostring[0] =9 ostring[1] =33 ostring[2] =21 ostring[3] =66 Now, the 1st, 3rd, 5th etc will be the pid and the 2nd, 4th, 6th etc. will be the qty. Only two sets are used in this example, normally 50+ pairs of values will be present. If it helps with the loop that I need to create - if a foreach will not suffice - I can deduce how may pairs will be present and send this as a var via GET from the form setting a value for i, for instance. I need to be able to, within a loop, * pull the first value as a pid * run a subroutine to pull the data from the DB table to get all the product info to display on a confirmation page [this is not ecom - just emailed orders for existing clients] I can do all the MySQL Query stuff it is just rationalising the data extracted from the array Well I am sure you can see from that what I am trying to do, I will need to do the multiplications etc. I do not know how to handle the data in the array created by the explode() In logic I would say this if x in $ostring[x] is even then $ostring[x] is a product id [mysql query to get details from table] next item in array is corresponding quantity - print details of product in a table, do the multiplication to give line total and add to $message ready for email - NEXT pair of vars I really hope that makes sense and someone can point me in the right direction - thanks in advance for any assistance. Regards A Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/ Share on other sites More sharing options...
litebearer Posted November 22, 2010 Share Posted November 22, 2010 Is this approx what you want... $x = 0; // will be the element of the array $count = count($array); //number of elements; say 50 for example $pairs = $count /2; // would be 25 $i = 1; while($i<=$pairs) { $pair1 = $array[$x]; $x = $x +1; $pair2 = $array[$x]; // do whatever with those two values here $i ++; } Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/#findComment-1137947 Share on other sites More sharing options...
liamoco Posted November 22, 2010 Share Posted November 22, 2010 $count = count($oStrArray); $pid = ""; $quantity = ""; for($x = 0;$x<$count;$x++) { if($x & 1) $pid .= $oStrArray[$x].","; //number is odd (pid) else $quantity .= $oStrArray[$x].","; //number is even (quantity) } I think this is what you ment, this hopefully (not tested) will put the pid's and quantities into two different strings that you will be able to explode. Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/#findComment-1137949 Share on other sites More sharing options...
Aventurine Posted November 22, 2010 Author Share Posted November 22, 2010 Hi liamoco, thanks for your response, I see your logic. I will have two strings one containing all the qtys and the other containing all the pids. So, ideally, I would like to say if [even] this is a pid run my sql query and pull the next value to do the multiplication. Do you see? I have been going through many resources about arrays etc and I am finding it difficult to apply the basics. I can call each var [0], [1] etc, in the script you propose would it be simple to call pid[0] and qty[0] then pid[1] and qty[1] etc for a given number of times so I can do my queries and calculations? Thanks again A Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/#findComment-1137962 Share on other sites More sharing options...
Aventurine Posted November 22, 2010 Author Share Posted November 22, 2010 I am using the quick reply here so I hope the php comes out OK - I may have been beating around the bush, here is the order.php page - thanks for any assistance <?php session_start(); require_once ('../mysql_connect.php'); // Connect to the db. $cid = $_GET['cid']; $surname = $_GET['surname']; $order = $_POST['order']; /* Retrieve the cid and surname for that combination. */ $query = "SELECT * FROM customer WHERE cid='$cid' AND surname='$surname'"; $result = @mysql_query ($query); // Run the query. $row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable. /* ============ ALL OK WITH LOGON START IF ================ */ if ($row) { // A record was pulled from the database. // Set the session data & redirect. session_name ('YourVisitID'); session_start(); $_SESSION['cid'] = $row[0]; $_SESSION['surname'] = $row[3]; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); /* =================== REGISTER VARS ======================= */ $ostring = ""; foreach($_POST as $key => $value) { if (strripos($key,'y')) { //check for y $key must be qty $nv = explode('_',$key); //if it contains underscore $thispid = $nv[1]; $ostring .= $thispid ."_" .$value . "_"; } } // end foreach $len=strlen($ostring); $ostring2=substr($ostring,0,($len-1)); // remove underscore from the end of the string echo $ostring2; $ostring = $ostring2; $oStrArray = explode('_', $ostring); echo "ostring[0] =".$oStrArray[0]; echo "ostring[1] =".$oStrArray[1]; echo "ostring[2] =".$oStrArray[2]; echo "ostring[3] =".$oStrArray[3]; /////////////// CODE TO BE ADDED ///////////////////////// // for every even value of x in $oStrArray[x] // this is a pid // SELECT * FROM from products where pid = '$oStrArray[x]' // result will include description/price/unit/unitprice // with unitprice do the multiplication $oStrArray[x] * $oStrArray[x+1] // print in table $row['description'] $row['unit'] $row['unitprice'] $oStrArray[x] * $oStrArray[x+1] // add data printed to table to var $message .= ready to be emailed to supplier // NEXT even value... //////////////////////////////////////////////////////////////// /* ================= ELSE LOGON FAILED ===================== */ } else { echo "Sorry your details do not match those on our records.<br />Click <a href='http://www.slipstream-organics.co.uk/makeyourownbox/index.php'>here</a> to go back."; } mysql_close(); // Close the database connection. ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/#findComment-1137978 Share on other sites More sharing options...
Aventurine Posted November 23, 2010 Author Share Posted November 23, 2010 To litebearer - I'm an idiot! Yes, looking at your code in the lite [haha] of day that is exactly what I needed, I could not see the wood for the trees. I am marking this as solved - thank you so much for your assistance. Repect A Link to comment https://forums.phpfreaks.com/topic/219468-handling-vars-from-array-after-explode/#findComment-1138294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.