Jump to content


Photo

Help with arrays


  • Please log in to reply
45 replies to this topic

#21 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 11:11 AM

OK, did you call your $_SESSION variable 'items' like I did in my example of not?  If not, then you need to change the references to it.

The errors you're getting are because $_SESSION['items'] does not appear to be a array.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#22 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 11:33 AM

Yes I did call my session 'items'. I have just re-added some code that I took out because I thought I didn't need it and I'm getting less errors now!The only error i'm getting in my order form now is this...

Unable to run : SELECT * FROM products WHERE item_number IN (BO7407) < this is the item number I clicked on in my products page.

Because: Unknown column 'BO7407' in 'where clause'

Here's the code i'm using in my file order_form.php...

<?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['item_number'])){
   if (!isset($_SESSION['items'][$_GET['item_number']])){
      // if it the first time it's selected, quantity is 1
      $_SESSION['items'][$_GET['item_number']] = 1;
   }
   else {
      // if it's been selected before, increase it by 1
      $_SESSION['items'][$_GET['item_number']]++;
   }
}

// 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 * FROM products WHERE item_number 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>{$row['$item_number']}</td><td><input type=\"text\" name=\"{$row['$item_number']}\" value=\"{$_SESSION['items'][$row['$item_number']]}\"></td></tr>\n";
}
?>

Can you see anything obvious there that i've done wrong? It appears to be a problem with the Select from statement.

#23 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 01:18 PM

ok, I assumed that the products all had a unique id that was an integer.  In that case we need to re-think.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#24 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 01:24 PM

Sorry, they do have a unique id. There is an id field which basically increments by 1 every time they add a new product to the database. I don't use this for anything though, I always call the item number.

Shall I try using the id then?

We can't be far away with this now because the error I mentioned above is showing all the item numbers every time I click a new item number, so it's clearly 'amost' working!

#25 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 01:27 PM

Yes, you should use the unique, auto-incrementing id for the link.

That would solve all the issues.  You can still display the product name on the page, but just have the link use the unique id to add to the array.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#26 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 26 October 2006 - 01:37 PM

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.

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

You can try page 1 here... and page 2 here...

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


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

#27 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 01:59 PM

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?

#28 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 02:21 PM

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

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#29 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 02:39 PM

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

#30 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 03:07 PM

Can I see the actual code.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#31 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 03:15 PM

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

<?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\">
");
?>


#32 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 03:23 PM

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:

<?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\">
");
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#33 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 03:36 PM

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.

#34 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 03:44 PM

Better idea, based on what you've said...

<?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\">
");
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#35 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 03:58 PM

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.

<?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\">
");
?>


#36 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 04:06 PM

My bad, made a major typo! I had
$body =. "value";
I should have had
$body .= "value";
I had the period and the equals the wrong waya around, I wasn't actually concatenating anything... Idiot  :-[

Try again with this 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\">
");
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#37 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 04:13 PM

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.

#38 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 11:04 PM

It is indeed a Bud, and you're more than welcome.

Glad to have been able to help.

Regards
Rich  8)
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#39 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 27 October 2006 - 08:46 AM

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?

#40 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 October 2006 - 10:08 AM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users