Jump to content

Need to grab all rows to populate table, only grabbing 1


Skor

Recommended Posts

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...

Link to comment
Share on other sites

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}

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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>";
	}	

 

Link to comment
Share on other sites

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.

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.