Jump to content


Photo

Help with arrays


  • Please log in to reply
45 replies to this topic

#1 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 25 October 2006 - 02:35 PM

Hi Chaps,

I posted about arrays last week regarding an online order form and got some useful responses pointing me in the direction of simple/free e-commerce packages. I've played around but nothing seems to be what i'm after.

Can anyone advise me on the use of arrays because I've not worked with them much in the past?

What I think I need to do is to store the item number of products in an array in a hidden page and then when people click 'View Order Form' it calls in the item numbers (and product names) of the items they selected from the array/hidden page.

The form doesn't need to be any more sophisticated than that. It needs to send the order details by email but I think i've got that to work. There's no payment options or anything like that, I just need people to be able to click the item number and for it to remember what they've selected and eventually display them in the order form.

Can anyone give me any advice on how best to do this?

Thanks

#2 HuggieBear

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

Posted 25 October 2006 - 02:39 PM

OK, for starters then, if it's going to be used across multiple pages, it needs to be a assigned to a session variable...

How are the products displayed on a page, are they static or dynamically generated from a database.

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

#3 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 25 October 2006 - 02:54 PM

Hi,

I'm calling them in from a database and then just displaying them one below the other. There are a few sections so people will be browsing between these and selecting products from them so sessions variable sounds about right, although i've never dealt with those before either!

I just need a hyperlink from the item number to then add it to a list if you like. Then at the end the user views the list and submits it.

#4 HuggieBear

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

Posted 25 October 2006 - 03:06 PM

ok, what about quantity, will there be a separate quantity for each individual item, or will they only be allowed to order one of each item?

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

#5 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 25 October 2006 - 03:08 PM

Yes there will be a quantity field on the order form. I was thinking maybe of automatically setting this to 0 for each item they order by default. The user should change the quantities themselves and then when they click submit I can use a javascript prompt to make sure there are no values left set at 0.

#6 HuggieBear

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

Posted 25 October 2006 - 03:12 PM

ok, my advice is an array of product id's as the key and quantities as the value.

I'll knock up an example in the next hour.

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

#7 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 25 October 2006 - 03:26 PM

Excellent, cheers.

I also want the form to select the product name from the database using a select from query like this:

"select product_name from DB where item_number = $item_number"

and then display the product name beside the item number.

I tried taking the product name as part of the hyperlink but because the names contain spaces it caused problems, so just the item number goes as part of the 'add to order' link.

#8 HuggieBear

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

Posted 25 October 2006 - 03:35 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
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

#9 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 25 October 2006 - 03:42 PM

That's great. I think that's exactly what I need.

I'll give that a try this evening and let you know how I get on.

Thanks for this.

#10 HuggieBear

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

Posted 25 October 2006 - 05:38 PM

OK, here's an idea on how to get data from the database for your order page...

<?php
// start a 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('connect.php');

// run the query to get the product name from the database
$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());

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

It should work with the previous two pages.

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

#11 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 09:03 AM

Hello,

I'm getting a couple of errors. Do you know what it means?...

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at URL:6) in URL on line 182

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at URL:6) in URL on line 182

#12 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 26 October 2006 - 09:27 AM

session_start() must be called before you render anything to the page.
The easiest way is to put it right at the top of your main page.
SA PHP Archives - www.phparchives.za.org

#13 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 09:45 AM

Ok I put the code at the very top of the page and it seems to have got rid of one of the errors, but i'm still getting this one in a really small font at the top of the page.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at URL:2) in URL on line 4

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 October 2006 - 09:48 AM

Make sure there no white spaces ok

<?php session_start();

>>>code here<<<<
?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 10:00 AM

I've got a bit lost with this code. I think i'm being a bit dumb.

I tested the code in a page called products.php and it works fine.

However, when I try it in my site I can't seem to get it right. It's a bit complicated too so let me try to explain...

What i'm doing is using a page called index_client.php and then using .inc files to display the content of the site when each link is clicked. So, for example, the contact us link opens contact_us.inc, which contains the page content.

So, the products listing is in a page called products_view.inc. When it displays the products I need a hyperlink from the item number to add the item numbers selected to the order form, which is a separate form (order_form.php).

I'm getting a few errors and it's not quite working properly. I think this is because i've not put the code in the correct places to be honest. Does all the code above need to go in the order form page or does some go in product_view.inc?

Sorry if that's confusing or sounds dumb but i'm a newbie so bear with me!

#16 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 26 October 2006 - 10:05 AM

The session_start(); call must be in the main file (index_client.php) right at the top.
SA PHP Archives - www.phparchives.za.org

#17 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 October 2006 - 10:14 AM

every page needs <?php session_start();?>
ok.

also resave all your pages to a .php and not a .inc for sec reasons ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#18 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 10:25 AM

Ok thanks. I've put <?php session_start();?> at the top of every page but it still isn't working. I think now it's more a case of the rest of the code isn't in the right place (possibly but i'm not certain).

This is the code i'm having difficulty with. It's in the file order_form.php. Can you spot anything obvious that i've done wrong?

<?php
// 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['id']}\" value=\"{$_SESSION['items'][$row['$item_number']]}\"></td></tr>\n";
}
?>

By the way, my .inc files don't contain anything that might compromise security. All the database connection stuff is in a db.php file which I call in.

#19 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 October 2006 - 10:39 AM

once agin no session start here ok.


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

// 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['id']}\" value=\"{$_SESSION['items'][$row['$item_number']]}\"></td></tr>\n";
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#20 stualk

stualk
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 October 2006 - 10:46 AM

Ok I've put the session start in that section and I'm still getting the same errors. These are the errors. I don't understand PHP errors at all, as you can probably tell:

Warning: array_keys() [function.array-keys]: The first argument should be an array in URL on line 210

Warning: implode() [function.implode]: Bad arguments. in URL on line 210
Unable to run : SELECT * FROM products WHERE item_number IN ()

Because: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users