prlutes Posted April 20, 2010 Share Posted April 20, 2010 ok, so basically i'm new to the forum and i'm really stuck, so here goes nothing... I have to create a simple shopping cart, where you just select 1 or more items out of 5. The items are stored in a separate .txt file and the php file reads them and explodes them into an array, the array then must be displayed as what you are purchasing, once i figure out how to get that to display properly, i need to figure out how to add up a total amount of money but i'll worry about getting the basic part working first haha... i'll go into more detail below I'm not sure where to go from here as it stands right now, here is my .txt file Canon EOS Rebel T2i, N82E16830120437, 899.99 Canon EOS Speedlite 580EX II, N82E16830998075, 499.99 SanDisk Extreme 16GB Compact Flash, N82E16820171417, 131.00 Canon LP-E4 Li-Ion Battery Pack, N82E16830998242, 109.99 Canon EF-S 17-85mm f/4-5.6, N82E16830998034, $499.00 This is my code to run the php file itself if (file_exists("items.txt")) { $data = file("items.txt"); print "<h2>So you want to buy a new camera???</h2> \n"; print "<form> \n"; foreach($data as $name) { $selection = explode("," , $name); print '<p> <input type="checkbox" name="item" value="' . $selection[0] . '"> Item : ' . $selection[0] . " Item Number is " . $selection[1] . " for $" . $selection[2] . "</p>\n"; } print "<input type=" . '"submit"' . "> \n"; print "</form> \n"; print "So, You want to buy: " . $item .". I don't blame you it's a great product!"; } Now, if i mark more than one box, only the latter thing in the form is being displayed so for example if i were to check off the Item : Canon EOS Rebel T2i Item Number is N82E16830120437 for $ 899.99 and the Item : Canon EOS Speedlite 580EX II Item Number is N82E16830998075 for $ 499.99 then only the Item : "Canon EOS Speedlite 580EX II" would be displayed, which is correct, but i also would like the first part displayed. Any help would be amazing. I'm fairly new to this language so bare with me, but i will do my best to understand. Thanks in advance for any help. Quote Link to comment Share on other sites More sharing options...
prlutes Posted April 20, 2010 Author Share Posted April 20, 2010 i mean i know i'm not doing it properly, i guess my question is how do i do this properly? because i know why mine doesn't work, but i don't know what i should do to make it work the way i want it to. Quote Link to comment Share on other sites More sharing options...
ignace Posted April 20, 2010 Share Posted April 20, 2010 Why not use a database instead of .txt files, a database is much faster not to mention cleaner. Quote Link to comment Share on other sites More sharing options...
prlutes Posted April 20, 2010 Author Share Posted April 20, 2010 well it's homework for my php class, and the teacher specifically wants us to call it from a .txt file... Quote Link to comment Share on other sites More sharing options...
ignace Posted April 20, 2010 Share Posted April 20, 2010 function readCsvFile($csvFile) { if (!file_exists($csvFile)) return array(); return array_map('csv2array', file($csvFile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); } function storeCsvLine($line, $csvFile, $mode = FILE_APPEND) {/* @see php.net/file_put_contents */ if (!file_exists($csvFile) || (!is_string($line) && !is_array($line))) return 0; if (is_array($line)) $line = implode(',', $line); return file_put_contents($csvFile, $line . PHP_EOL, $mode); } function csv2array($csv) { return array_map('trim', explode(',', $csv)); } function readProducts($file) { return readCsvFile($file); } function readCart($userId) { return readCsvFile($userId); } function storeProductInCart($product, $userId) { return storeCsvLine($product, "$userId.txt"); } function getCartSubTotal($userId, $cartContents = array()) { if (empty($cartContents)) $cartContents = readCart($userId); return array_reduce($cartContents, 'getCartSubTotalCallback'); } function getCartSubTotalCallback($item1, $item2) { return $item1[2] + $item2[2]; } function get($offset, $default = null) { return isset($_GET[$offset]) ? $_GET[$offset] : $default; } session_set_cookie_params(86400);//1-day session_start(); if (get('action') === 'show-products') { include('show-products.php'); /* $products = readProducts('path/to/products.txt'); */ } else if (get('action') === 'show-cart') { include('show-cart.php'); /* $cart = readCart(session_id()); $cartSubTotal = getCartSubTotal(session_id()); */ } else if (get('action') === 'add-product') { include('add-product.php'); /* $product = array(post('product_name'), post('product_no'), post('product_price')); storeProductInCart($product, session_id()); */ } Should give you a good start The comments below the include in the second example are a sample implementation. These belong in one file I separated them for easier reading. Give your teacher my regards Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.