Jump to content

Archived

This topic is now archived and is closed to further replies.

stualk

Help with arrays

Recommended Posts

[quote author=HuggieBear link=topic=112678.msg457438#msg457438 date=1161790534]
ok, here you go, this is the code for page 1.  If you look at the bottom of this page, there's an example of page one and page 2.

[code]
<?php
// start a session
session_start();

// clear the cart if the action is clear
if ($_GET['action'] == "clear"){
   unset($_SESSION['items']);
}

// if a link has been clicked
if (isset($_GET['product'])){
   if (!isset($_SESSION['items'][$_GET['product']])){
      // if it the first time it's selected, quantity is 1
      $_SESSION['items'][$_GET['product']] = 1;
   }
   else {
      // if it's been selected before, increase it by 1
      $_SESSION['items'][$_GET['product']]++;
   }
}
?>

<a href="products.php?product=1">Product 1</a><br>
<a href="products.php?product=2">Product 2</a><br>
<a href="products.php?product=3">Product 3</a><br>
<a href="products.php?product=4">Product 4</a><br>
<a href="products.php?product=5">Product 5</a><br>
<br>
<a href="products.php?action=clear">Clear Cart</a>
<br><br>

<?php
// This is just a var dump so you can see things incrementing
echo "<pre>";
var_dump($_SESSION['items']);
echo "</pre>";
?>
[/code]

You can try page 1 [url=http://dizzie.co.uk/php/products.php]here...[/url] and page 2 [url=http://dizzie.co.uk/php/products2.php]here...[/url]

Once you have all of these id's in an array, you can use that array to get the descriptions from the database for on the order form page.

Regards
Huggie
[/quote]

Although if you want the users "CART" to be stored on their computer for a certain time
or never expires then i would suggest using cookies.
www.php.net/cookies

Share this post


Link to post
Share on other sites
Huggie,

Yes, Yes, Yes! Works perfectly now, thank you, thank you, thank you!!! Let me buy you a drink...!!!

Now for the last bit - the contents of the form then get submitted to a specified email account. Before I was using arrays I just had a long list of input boxes, each numbered like below.

DESCRIPTION CODE QTY
$desc1 $code1 $qty1
$desc2 $code2 $qty2
$desc3 $code3 $qty3
etc

Now that i'm using an array how do I do this? Do I need to use the items field along with the id?

Share this post


Link to post
Share on other sites
There's no need for input boxes or a form at all.

Your order page is dynamically generated from the database, based on what existed in the array, so generate the email dynamically too.

Once you have your order ready, hit 'purchase' or whatever you want on there and process it like follows (Most of the code is similar to the page we've already seen):

[code]
<?php
// start the session
session_start();

// put the array of id's into a string for use in the sql query
$sqlitems = implode(",", array_keys($_SESSION['items']));

// connect to the database
include_once('database.php');

// get the results from the database (the same query as earlier)
$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";
$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());

// start the email
$to = "me@mydomain.com"; // may need to escape the @
$subject = "Order from your domain";

// generate the main message body of the email
$body = "Item\t\tQuantity\n";

// put a row for each order line into the body of the email
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";
}

// send the mail
mail($to, $subject, $body);
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
Problem is my order form already contains the client address and contact details. The hyperlink that takes the product details to the order form also takes the client details, so I need these details to be part of the email at the top really. Here's the content of the email I was using before, do I just put this beside the $body tag?:

An order has been received. The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments

Share this post


Link to post
Share on other sites
Ok, previously I was using the code below. My order form was submitting the details to a file called 'order_form_submit.php' which is here...

[code]
<?php

include "mime_mail.inc";

# create object instance
$mail = new mime_mail;

# set all data slots
$mail->from = "$email";
$mail->to = "MY EMAIL ADDRESS HERE";
$mail->subject = "Order from the website";
$mail->body = "The following is an order from the website.

The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments

DESCRIPTION CODE QTY
$brand_name $product_name $item_number $items

";
# send email
$mail->send();
print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">
");
?>
[/code]

Share this post


Link to post
Share on other sites
ok, I'm not 100% certain how that's going to work, as I've not used classes before, but you could try adding more than one item to the body of the email and see how it goes, for example:

[code]
<?php
// start the session
session_start();

// put the array of id's into a string for use in the sql query
$sqlitems = implode(",", array_keys($_SESSION['items']));

// connect to the database
include_once('database.php');

// get the results from the database (the same query as earlier)
$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";
$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());

include "mime_mail.inc";

# create object instance
$mail = new mime_mail;

# set all data slots
$mail->from = "$email";
$mail->to = "MY EMAIL ADDRESS HERE";
$mail->subject = "Order from the website";
$mail->body = "The following is an order from the website.

The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments";

$mail->body = . "\n\nItem\t\tQuantity\n";

// put a row for each order line into the body of the email
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $mail->body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";
}

# send email
$mail->send();
print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">
");
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
It's not liking that. It is only emailing through the last reference to $mail->body

When I took out the order details and just left in the client contact details they came through ok.

Seems it's just a problem displaying all the info in one email.

Could we not declare everything as variables before we get to the mail->body bit and then when we get to mail->body just call in the variables?:

$mail->body =
"$contact_details
$order_details
";

I guess that's complicating matters a little. There must be another way of doing it.

Share this post


Link to post
Share on other sites
Better idea, based on what you've said...

[code]
<?php
// start the session
session_start();

// put the array of id's into a string for use in the sql query
$sqlitems = implode(",", array_keys($_SESSION['items']));

// connect to the database
include_once('database.php');

// get the results from the database (the same query as earlier)
$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";
$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());

include "mime_mail.inc";

# create object instance
$mail = new mime_mail;

# set all data slots
$mail->from = "$email";
$mail->to = "MY EMAIL ADDRESS HERE";
$mail->subject = "Order from the website";
$body = "The following is an order from the website.

The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments";

$body =. "\n\nItem\t\tQuantity\n";

// put a row for each order line into the body of the email
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";
}

$mail->body = $body;

# send email
$mail->send();
print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">
");
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
No that does the same, it just returns the last reference to $body instead of all three.

I've just reverted to my original code (which is below) and it works other than where the product details should appear it displays the word "Array"

Maybe we can put the array details in here and that will fix the problem? It's definitely carrying all the values through it's just not displaying them.

[code]<?php

include "mime_mail.inc";

# create object instance
$mail = new mime_mail;

# set all data slots
$mail->from = "$email";
$mail->to = "MY EMAIL ADDRESS HERE";
$mail->subject = "Order from the website";
$mail->body = "The following is an order from the website.

The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments

DESCRIPTION CODE QTY
$brand_name $product_name $item_number $items

";
# send email
$mail->send();
print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">
");
?>[/code]

Share this post


Link to post
Share on other sites
My bad, made a major typo! I had [code=php:0]$body =. "value";[/code] I should have had [code=php:0]$body .= "value";[/code] I had the period and the equals the wrong waya around, I wasn't actually concatenating anything... Idiot  :-[

Try again with this code:

[code]<?php
// start the session
session_start();

// put the array of id's into a string for use in the sql query
$sqlitems = implode(",", array_keys($_SESSION['items']));

// connect to the database
include_once('database.php');

// get the results from the database (the same query as earlier)
$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";
$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());

include "mime_mail.inc";

# create object instance
$mail = new mime_mail;

# set all data slots
$mail->from = "$email";
$mail->to = "MY EMAIL ADDRESS HERE";
$mail->subject = "Order from the website";
$body = "The following is an order from the website.

The order details are as follows:

NAME: $contact_name
COMPANY: $company_name
ADDRESS: $address
TOWN: $town
COUNTY: $county
POST CODE: $post_code
TELEPHONE: $telephone
MOBILE: $mobile
COMMENTS: $comments";

$body .= "\n\nItem\t\tQuantity\n";

// put a row for each order line into the body of the email
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $body .= "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";
}

$mail->body = $body;

# send email
$mail->send();
print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">
");
?>[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
That works perfectly, you are the man!! A crate of Budweiser is on it's way to you in the post! (Is that what you're drinking in your picture? :-D

Thanks mate, I owe you one.

Share this post


Link to post
Share on other sites
It is indeed a Bud, and you're more than welcome.

Glad to have been able to help.

Regards
Rich  8)

Share this post


Link to post
Share on other sites
Morning!

Just spotted a couple of minor problems with this. One is that if you add a product to the order form and then change the quantity, then go back and add another product, it puts all the quantities you changed back to 1. Is there any way we can make it so that it does nothing with the quantity at all and we'll let the user specify their quantity?

The other thing is that if you add a few products to the form and add your quantities just before you submit the form it uses the quantities specified when the product was added to the form (i.e. 1) rather than the quantity you changed it to.

Are these two things fairly straight forward to sort out?

Share this post


Link to post
Share on other sites
Yes, they aren't problems, I just hadn't written those bits yet.  If you think you understand some of the code, then why not have a go at those bits yourself and see how you get on.

Regards
Huggie

Share this post


Link to post
Share on other sites
I already have but failed miserably!

I tried taking the if statements out regarding the quantity (code below) but that didn't work. I also tried changing the quantity in this code from 1 to 0 and even tried putting nothing at all but none of this works.

Can't think what else to try. I think i've almost done the right thing, but not quite!

[code]
// if a link has been clicked
if (isset($_GET['id'])){
  if (!isset($_SESSION['items'][$_GET['id']])){
      // if it the first time it's selected, quantity is 1
      $_SESSION['items'][$_GET['id']] = 1;
  }
  else {
      // if it's been selected before, increase it by 1
      $_SESSION['items'][$_GET['id']]++;
  }
}
[/code]

Share this post


Link to post
Share on other sites
OK, it's always good to start with a plan, so how about this.  On all your product pages you allow people to add the product to the order form by selecting a link.  We have this in place already.  They can't decrease the quantity from these pages, they have to go to the order form for that, is that a fair assumption?

I think despite this being an order form, it should be treated like a shopping cart, with the same principles.

Then from the order form page, they can remove specific items, remove all items, or change the quantity for individual items?

Regards
Huggie

Share this post


Link to post
Share on other sites
Yes that's a fair assumption. The quantity is dealt with on the order page.

It would be good if we could have an option to remove individual items from the order form but it's not crucial. The quantity issue is the main thing but if there's a way we can have a 'remove' option in a column beside each product as well that could be useful.

I have already put in a 'clear form' option and this works fine.

Share this post


Link to post
Share on other sites
Can you possibly provide me the complete code that you're using for the order page now.

I can work with that.

Regards
Huggie

Share this post


Link to post
Share on other sites
Well you did ask...! :-D

[code]
<?php
if ($action =="" || $action=="default") {

print("
<form name=\"form1\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"addscript\" size=\"20\">
");

if ($action =="" || $action=="default") {

$NumRows=mysql_query("SELECT * FROM customers") or die("Sorry Couldn't Complete Query_1");
$NumRows=mysql_num_rows($NumRows);
if ($NumRows == "0") {
echo("<br><br><br><b>SORRY, PLEASE GO BACK AND TRY AGAIN.</b>");
}
else {
$Result = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_2");
  $id = mysql_result($Result,0,0);
  $company_name = mysql_result($Result,0,3);
  $contact_name = mysql_result($Result,0,4);
  $address = mysql_result($Result,0,5);
  $town = mysql_result($Result,0,6);
  $county = mysql_result($Result,0,7);
  $post_code = mysql_result($Result,0,8);
  $email = mysql_result($Result,0,9);
  $telephone = mysql_result($Result,0,10);
  $mobile = mysql_result($Result,0,11);
}

if ($NumRows == "0") {
  echo ("<b>Sorry, please go back and try again.</b><br><br>");
}
else  {
$NumRows = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_3");
$NumRows = mysql_num_rows($NumRows);
$Result = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_4");
for ($i=1; $i<=$NumRows; $i++) {
  if ($row = mysql_fetch_array($Result)) {
$id = $row["id"];
$company_name = $row["company_name"];
$contact_name = $row["contact_name"];
$address = $row["address"];
$town = $row["town"];
$county = $row["county"];
$post_code = $row["post_code"];
$email = $row["email"];
$telephone = $row["telephone"];
$mobile = $row["mobile"];

}}}

print("
<table width='580' border='0' cellspacing='0' cellpadding='0'>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Name: </font></b></td>
                  <td colspan='3'><input type='text' name='contact_name' value='$contact_name' size='50'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Company Name: </font></b></td>
                  <td colspan='3'><input type='text' name='company_name' value='$company_name' size='50'></td>
                </tr>
                <tr valign='top'>
                  <td width='25%'><b><font face='Verdana' size='2'>Address: </font></b></td>
                  <td colspan='3'><textarea name=\"address\" cols=\"40\" rows=\"4\" wrap=\"VIRTUAL\">$address</textarea></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Town: </font></b></td>
                  <td colspan='3'><input type='text' name='town' value='$town' size='25'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>County: </font></b></td>
                  <td colspan='3'><input type='text' name='county' value='$county' size='25'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Post Code: </font></b></td>
                  <td colspan='3'><input type='text' name='post_code' value='$post_code' size='10'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Email: </font></b></td>
                  <td colspan='3'><input type='text' name='email' value='$email' size='35'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Daytime Telephone: </font></b></td>
                  <td colspan='3'><input type='text' name='telephone' value='$telephone' size='20'></td>
                </tr>
                <tr>
                  <td width='25%'><b><font face='Verdana' size='2'>Mobile: </font></b></td>
                  <td colspan='3'><input type='text' name='mobile' value='$mobile' size='20'></td>
                </tr>
<tr valign='top'>
                  <td width='25%'><b><font face='Verdana' size='2'>Comments: </font></b></td>
                  <td colspan='3'><textarea name=\"comments\" cols=\"40\" rows=\"4\" wrap=\"VIRTUAL\"></textarea></td>
                </tr>
              </table>

<font face='Verdana' size='2'><br>Please enter your order details below and click the <b>Submit Order</b> button at the bottom to automatically send your order through to us.<br></font>

<font face='Verdana' size='2'><br>Alternatively, please click the <b>Print Order Form</b> button at the bottom to print and fax the order form to us.<br><br></font>

<table width='580' border='1' cellpadding='2' cellspacing='0' bordercolor='#000000'>
  <tr valign='top'>
    <td width='70%'><div align='center'><font size='2'><strong>Product Description</strong></font></div>
    </td>
    <td width='15%'><div align='center'><font size='2'><strong>Product Code</strong></font></div></td>
    <td width='15%'><div align='center'><font size='2'><strong>Quantity</strong></font></div></td>
  </tr>
");
?>


<?php session_start();

// clear the cart if the action is clear
if ($_GET['action'] == "clear"){
  unset($_SESSION['items']);
}

// if a link has been clicked
if (isset($_GET['id'])){
  if (!isset($_SESSION['items'][$_GET['id']])){
      // if it the first time it's selected, quantity is 1
      $_SESSION['items'][$_GET['id']] = 1;
  }
  else {
      // if it's been selected before, increase it by 1
      $_SESSION['items'][$_GET['id']]++;
  }
}

// put the array of id's into a string for use in the sql query
$sqlitems = implode(",", array_keys($_SESSION['items']));

// connect to the database
include_once('database.php');

// run the query to get the product name from the database
$sql = "SELECT id, brand_name, product_name, item_number FROM products WHERE id IN ($sqlitems)";
$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());

// echo a row for each of our product lines
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  echo "<tr><td><font face='Verdana' size='2'>{$row['brand_name']} {$row['product_name']}</font></td><td><font face='Verdana' size='2'>{$row['item_number']}</font></td><td><input size=\"5\" type=\"text\" name=\"{$row['id']}\" value=\"{$_SESSION['items'][$row['id']]}\"></td></tr>\n";
}
?>

<?php
print ("
  <tr>
    <td>&nbsp;</td>
    <td><div align='center'>
      <input type='submit' name='Submit' value='Submit Order'>
    </div></td>
    <td><font face='Verdana' size='2'><b><div class='navlinks'><a href='order_form.php?action=clear'>CLEAR FORM</a></b></div></font></td>
  </tr>
</table>
</form>
");
}}

if ($action =="addscript" ) {

require ('mail.php');

if ($multiple == "yes"){print("<meta http-equiv=\"refresh\" content=\"1;URL=?action=submitted\"><font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><br><br><b>Submitting order...</b><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"); }
else {print("<meta http-equiv=\"refresh\" content=\"1;URL=?action=submitted\"><font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><br><br><b>Submitting order...</b><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"); }

// ------------------------------------------------------------------------------- action
if ($action =="submitted" ) {

print("<font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><b>Thank you.</b><br><br>Your order has been submitted and will be processed and dispatched within 5 working days.<br><br><br><br><br><br><br><br></font>");

// ------------------------------------------------------------------------------- action
}}

?>
[/code]

Share this post


Link to post
Share on other sites
Hi Huggie,

I've been thinking about it over the weekend and I don't think we really need a 'remove item' button on the form. If people want to remove anything they can clear the form and start again, lazy so and sos!

The main thing I need to do is sort the small problem with the quantity keep resetting itself and not submitting properly to the email script. Do you know what could be causing this?

Once i've sorted that i'm a happy bunny!

Share this post


Link to post
Share on other sites

×

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.