Jump to content

Help with arrays


stualk

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
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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!
Link to comment
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.
Link to comment
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]
Link to comment
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
Link to comment
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.
Link to comment
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!
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.