Skor Posted May 8, 2007 Share Posted May 8, 2007 I'm trying to grab all rows for an order email after I've "exploded" the data from the shopping cart transfer. Here's the explosion code which works perfectly. How do I use the same data to populate the ordered items table I've created. It works fine for one item. $i = 0; // breaks each product string into its own array foreach($products as $var => $val){ $product[$i] = explode(":", $val); $desc = $product[$i][0]; $qty = $product[$i][1]; $money = $product[$i][2]; $pid = $product[$i][4]; $update = "UPDATE table..."; mysql_query($update) or die(mysql_error()); $i++; } Here's the order block which is a bit further down in the code, do I need to iterate over it? <div> <table cellspacing=\"2\" cellpadding=\"2\" width=\"350\" bgcolor=\"#ffffff\" border=\"1\"> <tr valign=\"top\"> <td><b>Product</b></td> <td><b>Quantity</b></td> <td><b>Price</b></td> </tr> <tr valign=\"top\"> <td>$desc</td> <td>$qty</td> <td>$money</td> </tr> </table> thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/ Share on other sites More sharing options...
benjaminbeazy Posted May 8, 2007 Share Posted May 8, 2007 what does the code do right now? errors? Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-247868 Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 if u want to grab the items from db then i think try this: use while loop. $update = "UPDATE table..."; $result=mysql_query($update) or die(mysql_error()); while($row= mysql_fetch_array($result)){do ur html here} Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-247869 Share on other sites More sharing options...
Skor Posted May 8, 2007 Author Share Posted May 8, 2007 No errors, it only includes one item in the table. If there are more than 1 -- I tested with 2 -- only one row of data appears. Can you do a while loop within an HTML email message under the $message variable?? Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-247870 Share on other sites More sharing options...
benjaminbeazy Posted May 8, 2007 Share Posted May 8, 2007 No errors, it only includes one item in the table. If there are more than 1 -- I tested with 2 -- only one row of data appears. Can you do a while loop within an HTML email message under the $message variable?? nope, if you check the other topic where you posted the question. i suggested a couple solutions. how is the info transferred from the shopping cart, is it already in a database or is it a posted string or what. if its already in the db, try what mmarifu4u suggested. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-247885 Share on other sites More sharing options...
Skor Posted May 8, 2007 Author Share Posted May 8, 2007 Where should I include this code to include it in the $message variable. Once it's inside the variable it just seems to interpret the variables and not the logic or commands. I'll do some tinkering tonite to see what works best. Thanks for all of your input. Steve Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248538 Share on other sites More sharing options...
Skor Posted May 9, 2007 Author Share Posted May 9, 2007 Where should I include this code to include it in the $message variable. Once it's inside the variable it just seems to interpret the variables and not the logic or commands. I'll do some tinkering tonite to see what works best. Thanks for all of your input. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248649 Share on other sites More sharing options...
Skor Posted May 9, 2007 Author Share Posted May 9, 2007 The good news is that I was able to get empty field logic to work. The bad news is that I'm still stuck on this one. The good news is that I was able able to figure out how to jump between my $message variable declaration versus needing PHP functionality. So here's what I did. remember that I've got the explode code in towards the top of the script. This time I received the following error: PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Here's my revised code: <table cellspacing=\"2\" cellpadding=\"2\" width=\"350\" bgcolor=\"#ffffff\" border=\"1\"> <tr valign=\"top\"> <td><b>Product</b></td> <td><b>Quantity</b></td> <td><b>Price</b></td> </tr>"; $cart = str_replace("~","\n",$cart); while($row= mysql_fetch_array($result)){ $message .= "<tr valign=\"top\"> <td>$desc</td> <td>$qty</td> <td>$money</td> </tr>"; } $message .= "</table> Do I need to do something to the cart variable to be able to use it again? ~thanks Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248748 Share on other sites More sharing options...
mmarif4u Posted May 9, 2007 Share Posted May 9, 2007 From where the $result variable is coming. while($row= mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248766 Share on other sites More sharing options...
Skor Posted May 9, 2007 Author Share Posted May 9, 2007 I'm not sure, that was a suggestion made yesterday? Do I need to explode the $cart value all over again? Or can I capture those values once exploded as an array. See original post which leads to database update. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248767 Share on other sites More sharing options...
mmarif4u Posted May 9, 2007 Share Posted May 9, 2007 Please post ur update code. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-248773 Share on other sites More sharing options...
Skor Posted May 9, 2007 Author Share Posted May 9, 2007 I will post the code this evening. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-249153 Share on other sites More sharing options...
Skor Posted May 10, 2007 Author Share Posted May 10, 2007 OK, here we go. The code is broken down into 5 main sections, and I've got 4 of them working. Kinda like Meatloaf said, "4 outta 5 ain't bad." Arghh. Where here we go. Part 1: Declare variables (which are being posted from the shopping cart process) $cart = ($_POST['cart']); $voucher = ($_POST['voucher']); $vval = ($_POST['vval']); There's lots of them, and they're all setup like this. Part 2: Insert data into database (This is tested and works fine) mysql_connect ($host,$user, $password) or die ("Unable to connect to server"); mysql_select_db($db_name) or die ("Couldn't retrieve database information from $db_name"); // Build the query $query = "INSERT INTO orders (id, username, ip...) VALUES ('$id', '$username', '$ip'...)"; // Run the query. mysql_query ($query); Part 3: Explode the Cart (Extract the usable data elements) // breaks the string into an array of products $products = explode("~", $cart); $i = 0; // breaks each product string into its own array foreach($products as $var => $val){ $product[$i] = explode(":", $val); $id = $product[$i][0]; $qty = $product[$i][1];... Part 4: Update product database // if you want to update the status of codes, you can do it here // say you have a table named codes with code, status fields $update = "UPDATE dbtable SET status ='S', sold_date = NOW() WHERE id = '$id'"; mysql_query($update) or die(mysql_error()); $i++; } Part 5: Create Email Message (Use posted variables containing order info in the outbound message) $message = "<html> <body bgcolor=\"#FFFFFF\"> <font size=\"3\" face=\"Arial\"> <b>Thank you for your order.</b><br><br>... Included in the message are a couple of static table containing labels and variables. Here's where the issue is. Should I run through the explode routine again? <div> <table cellspacing=\"2\" cellpadding=\"2\" width=\"350\" bgcolor=\"#ffffff\" border=\"1\"> <tr valign=\"top\"> <td><b>Product</b></td> <td><b>Quantity</b></td> <td><b>Price</b></td> </tr>"; $cart = str_replace("~","\n",$cart); while($row= mysql_fetch_array($result)){ $message .= "<tr valign=\"top\"> <td>$desc</td> <td>$qty</td> <td>$money</td> </tr>"; } $message .= "</table> <br><br> <div> Part 6: Send Message (Send order confirmation to customer) mail($to, $subject, $message, $headers); Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-249444 Share on other sites More sharing options...
Skor Posted May 10, 2007 Author Share Posted May 10, 2007 This one's really got me, can someone offer me some guidance on step 5. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-249760 Share on other sites More sharing options...
Skor Posted May 11, 2007 Author Share Posted May 11, 2007 "bump" Do I need to loop through an exploded array. Not sure quite how to do that. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-250256 Share on other sites More sharing options...
StormTheGates Posted May 11, 2007 Share Posted May 11, 2007 Ok I cant really be bothered to read it all. So I looked at the title and here is some code that I use for my game that you can use for populating tables with all entries: <?php echo " <table width=300 cellspacing=1 cellpadding=1 border=0 bordercolor=black class=black2> <tr> <td colspan=4 class=header><div align=center>Last 10 Attempts On Your Life </div></td> </tr>"; $result = mysql_query("SELECT * FROM attempts WHERE defender='$username' ORDER BY `id` DESC limit 0,10"); while ($info = mysql_fetch_row($result)) { echo " <tr class=text> <th width=1></th> <td width=135>$info[1]</td> <td>$info[2] shot at you, and you $info[4]</td> <th width=1></th> </tr>"; } echo "</table>" ?> If you have all your things in an array like $array = "Toys-5.00-Bob" Then just do this for the code: $result = mysql_query("SELECT array FROM attempts WHERE defender='$username' ORDER BY `id` DESC limit 0,10"); while ($info = mysql_fetch_row($result)) { $stuff = explode("-", $info[0]); echo " <tr class=text> <th width=1></th> <td width=135>$stuff[0]</td> <td>$stuff[1] shot at you, and you $stuff[2]</td> <th width=1></th> </tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-250276 Share on other sites More sharing options...
Skor Posted May 14, 2007 Author Share Posted May 14, 2007 This was my attempt to replicate the spirit of your script to mine, but it didn't sent an email, rather it returned an error message: [10-May-2007 22:20:04] PHP Parse error: syntax error, unexpected '~' in /home/carolynk/public_html/the_lab/monet/mals/cartworks6.php on line 154 <table cellspacing=\"2\" cellpadding=\"2\" width=\"350\" bgcolor=\"#ffffff\" border=\"1\"> <tr valign=\"top\"> <td><b>Product</b></td> <td><b>Quantity</b></td> <td><b>Price</b></td> </tr>"; $result = mysql_query("SELECT cart FROM orders WHERE id = '$id'); mysql_query($result) or die(mysql_error()); $products = explode("~", $result); $i = 0; // breaks each product string into its own array foreach($products as $var => $val){ $product[$i] = explode(":", $val); while ($info = mysql_fetch_row($result)) { $message .= "<tr valign=\"top\"> <td>$product[$i][0]</td> <td>$product[$i][1]</td> <td>$product[$i][2]</td> </tr>"; } } $message .= "</table> The exploded value has two delimitters -- a ~ between records and a : between fields. I'm just missing something. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50447-need-to-grab-all-rows-to-populate-table-only-grabbing-1/#findComment-252468 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.