Jump to content

Archived

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

stualk

Help with arrays

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


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

[code]<?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";
}
?>[/code]

It should work with the previous two pages.

Regards
Huggie

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Make sure there no white spaces ok

<?php session_start();

>>>code here<<<<
?>

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
The session_start(); call must be in the main file (index_client.php) right at the top.

Share this post


Link to post
Share on other sites
every page needs <?php session_start();?>
ok.

also resave all your pages to a .php and not a .inc for sec reasons ok.

Share this post


Link to post
Share on other sites
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?

[code]
<?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";
}
?>
[/code]

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.

Share this post


Link to post
Share on other sites
once agin no session start here ok.


[code]
<?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";
}
?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

[code]
<?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";
}
?>
[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

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.