Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,370
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. the code that QuickOldCar posted, using the session_id as an array key, is overly complex and problematic. a single session_regenerate_id() statement (which you would be using if you are concerned about session security) will loose the contents stored in the cart. there was nothing wrong with the original definition of your cart and in fact it looked like something that would have been suggested here on phpfreaks. part of what i recommend would have separated your main program logic from the unchanging part of the page, so that you can POST JUST THE RELEVANT part of your code that you need help with. no one will try to sift through hundreds of non-relevant lines of code to just locate, then figure out the code you are having a problem with. besides the error about the session_id variable, that you shouldn't have changed your code to use anyway, what sort of problem is your code having that you need to fix? and please, just post the relevant part of your code.
  2. some recommendations - 1) you should be using css instead of inline styling. this will simplify and clean up all your html makeup and get your page up to 21st century programming standards. if you are trying to develop programming skills, you must start with current coding standards, not something that is out of date. 2) Don't Repeat Yourself (DRY). both of your pages repeat about half of the code, because you are trying to produce a constant page layout (you even have an error between the pages, where you corrected something only on one page.) you can and should actually have just one page, but if you are not up to this point in your programming, you should make the common part of the page layout a php based template that you include on each page that needs it. the main content that's different between the pages would be produced and stored in a php variable, that you just echo at the appropriate place in the 'template' code. 3) your form processing code should come near the top of your file, before any html markup. your code should be laid out in this general order - initialization, post method form processing, get method processing, html page/template. the post method processing code, when it has successfully ran without any errors, should do a header() redirect back to the same url of the page. if you want to display a 'success' message, pass it in a session variable that will be tested and used in the html page/template code. the get method processing code is the php code that gets/produces the data that the html page/template needs to display the dynamic information on the page. 4) your add to cart code contains a typo in - $_session. this should be $_SESSION. your add to cart code should not be testing a variable named - $_POST['Bread'] as that doesn't tell anyone reading the code what the purpose of the variable is. you should have a variable that identifies what the submitted form was for. how about using - $_POST['add_to_cart'] ? all of your add to cart form processing code should be inside the conditional statement that tests if the correct form was submitted. see the following example code for what your add to cart from processing can/should be - // post method form processing code if($_SERVER['REQUEST_METHOD'] == 'POST') { // add to cart if (isset($_POST['add_to_cart'])) { $pid = (int)$_POST['prodid']; if(!isset($_SESSION['cart'][$pid])){ $_SESSION['cart'][$pid] = 0; // create an entry (it will be incremented in the following code) } $_SESSION['cart'][$pid]++; // increment whatever the existing quantity is $_SESSION['message'] = "Your product has been added to your cart"; // uses in the html page/template code } // other form processing code would go here... // after successfully (no errors) processing all post data, do a header() redirect to the exact same url that the form submitted to if(empty($errors)){ $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; header("Location: http://$host$uri"); die; } // if there were errors in any of the above form processing code, continue on this page, displaying any errors, redisplay form... } 5) if you are learning php now, you should be using either the mysqli or PDO database library of functions. the mysql functions are obsolete and will be removed from php soon. again, you should start with current coding standards, not something that is out of date. i would also recommend that your config settings be assigned to an array - $config, so that you can distinguish them from other variables. your navigation menus on the page should also be dynamically produced (from configuration settings), rather than hard coded html.
  3. the example that boompa posted wasn't copy/paste code for you to use. it was an example for you to look at, learn from, and modify your code to make use of. the line - $result = $pdo->query(); needs to include your $query variable as a parameter. you also need to change the foreach() statement to use $result, rather than to run the query again.
  4. if you enable pdo exceptions, you can catch and handle all the database statement errors in one place without having to add logic for each individual database statement.
  5. i'm wondering if one of your previous installations of an amp package didn't include this vhost application and now its got a service running that is being triggered by tcp/ip traffic from newly installed apache/mysql services. check in your list of installed programs and in the services for anything related to vhost.
  6. you are blaming the amp stack, without first finding the cause of the problem. randomly trying different things as a way of solving problems is a huge time killer when dealing with an exact science like programming. programming requires that you find what's causing a problem before you can fix it. see the latest reply in your code thread.
  7. based on the fatal error, i'm guessing that your item class is being included, but it's not before the session_start() statement, which it will need to be in order to restore the instances of the item class in the session data.
  8. i recommend clearing your $_SESSION['cart'] (it wouldn't hurt to have an 'empty cart' function in your code.) your code works for me and i suspect what you are seeing in the print_r() output is left over from what previous coding stored in the session variable. some of your code is still out of order. this - if ($productid->count()) { is the logic statement that's verifying that the submitted id was found in the database. ALL the code dependent on verifying that the id exists should be within the scope of that conditional block.
  9. the following line of code is incorrect usage. it is overwriting the item in the cart with whatever value the updateQuantity() method returns - $_SESSION['cart'][$result->id] = $item->updateQuantity(10); to modify the quantity of the item in the cart, the code would look like - $_SESSION['cart'][$result->id]->updateQuantity(10); $_SESSION['cart'][$result->id] is (when you don't overwrite it) an instance of the item class. your goal would be to call the updateQuantity() method for that instance/item. you are still creating an instance of the item class in the wrong place and there's no point in looping over $productid->results() when there is at most only one matching result from the query. i think it will probably help you if you first define what a block of code is going to accomplish, then put that definition in as comments in the code. for example, the $_GET['prodid'] block of code appears to be an 'add' to cart process. what steps do you need - 1) validate the id/retrieve the corresponding product information for the id. if found, continue. if not found, output an error message. 2.a.) if the item is not in the cart, create an instance of the item class, with the submitted quantity (or a quantity of 1) and add it (assign it) to the cart. 2.b.) if the item is already in the cart, modify the quantity according to the submitted quantity.
  10. without the actual error message and where it is occurring at in the code, there's not much chance at helping. the only two things that are apparent from the posted code are - 1) you should only be making an new instance of an item if the item doesn't exist in the cart. the $item = new item(....); belongs inside the else {...} block of code. 2) your total won't work. you are assigning the sub-total to $total. you should be adding it.
  11. if your intent is just for someone to troubleshoot and fix the problem for you, you would want to post in the freelancing - Job Offerings forum section. the programming help forum sections are for coders, and those learning to code, to get help with code they have written. these forum sections are not here to get free programming services.
  12. you would use a multi-dimensional array. firstly, you should separate the cart session data from other session data by giving it a primary array associative index name - $_SESSION['cart'] secondly, your id should uniquely identify an item, it should be the auto-increment id from your database table where you have defined the items, and the id should be used as the next array dimension's index - $_SESSION['cart'][id_value] lastly, to allow multiple of any item to be stored in a cart, the value stored should be the quantity - $_SESSION['cart'][id_value] = quantity to add a quantity of one (1) to the cart for any id, the basic code would look like - session_start(); if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); // create an empty cart } // form processing code if($_SERVER['REQUEST_METHOD'] == 'POST'){ // add quantity one to cart if(isset($_POST['add'])){ // validate/cast input data $id = (int)$_POST['hiddenid']; // the validation logic will produce a 'safe' $id to use if(!isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id] = 0; // create an empty item (it will be incremented next) } $_SESSION['cart'][$id]++; // increment - add one to whatever the existing quantity is } // code for other form processing operations would go here.... }
  13. the code you have is not looping over anything. it's also (still) using the $event->nodeValue, which is just the last event that was added to the $events array. example that takes the statement of what to do and produces the code to do it - // i would loop over the $alertValues array, checking if each value in turn is in the $events array (see in_array()) and stop on the first match. $message = ''; // default to an empty string for the result foreach($alertValues as $value){ // loop over the $alertValues array if(in_array($value,$events)){ // checking if each value in turn is in the $events array $message = $value; // a match was found, save it as the result break; // stop on the first match } } after the above code runs, $message will either be an empty string or the highest type event found in the $events array.
  14. i would loop over the $alertValues array, checking if each value in turn is in the $events array (see in_array()) and stop on the first match.
  15. when the database connection fails and you run a statement that uses the connection variable, like - foreach ($connect->query($sql) as $row) you get a fatal runtime error that halts code execution at that statement. when something fails, you need to take an appropriate action, which would include not running any code that's dependent on whatever failed. why are you setting error_reporting to zero in the catch{} block of code? for development, you would want to report and display all errors, not hide them and on a live server you would want to report all errors, but log them rather than display them.
  16. to get php to show fatal parse errors in your main file, you must set the error_reporting and display_errors settings in the php.ini on your development system. you would find the php.ini that php is using (see the Loaded Configuration File value in the output from a phpinfo() statement), then find and change the error_reporting and display_errors lines in it, to be error_reporting = E_ALL and display_errors = On stop and start your web server to get any changes made to the master php.ini to take effect. once you put these settings in the php.ini, you don't need to put them into your .php files.
  17. only checked check-boxes are submitted and you have no relationship between the check-box and the text input field holding the updated date. you can see this is you examine the submitted post data. add the following debugging line of code to see what is actually being submitted - echo '<pre>',print_r($_POST,true),'</pre>'; your database table should have an autoincrement id (identifier) column. i recommend that you use the id as the form field array index for both the check-box and the text input field. this will serve to tie the check-box to it's corresponding text input field.
  18. if the html of the form is actually present (have you checked the 'view source' of the page in the browser type where it doesn't work), browser problems like this are usually due to invalid markup/css that some browsers ignore the problems in, but other's don't. your page has 70 makeup errors and 67 warnings - http://validator.w3.org/check?uri=compareandchoose.com.au%2Fhome_loans&charset=%28detect+automatically%29&doctype=Inline&group=0 most of the errors are because the html comments are broken, which means that some browsers will see part of the html as being part of the comment. if you are dynamically producing these pages, why do you even have html comments in it? you should have php comments in the php code.
  19. similar to my reply in your other thread, if you are at the point of trying to make an installer script, you should be dynamically producing any form and dynamically processing the form data, rather than writing out the specific html/php code for each value for each step. you would do this by having the form fields and validation steps for each field/type defined in a data structure somewhere (a database table or an array.) the data structure would contain the dynamic information for each field in each step, such as the form field name, the label for the form field, the type of form field, any lists of choices (select/checkbox/radio), and what validation step(s) to 'call' (using variable functions) for each field.
  20. if you are at the point of trying to make an install script, all your code manipulating the values should be general purpose, rather than hard-coded with specific variable/defined constant names. if you are using variables, use an array ($config['db_host']). if you are using defined constants, use a prefix on the names (CONFIG_DB_HOST) so that you can distinguish them from any other defined constants you have in your code. the only thing in your config file should be the settings. there should be no functional code, such as the database connection. then, at the point of saving the values, why not just loop over the array/prefixed named defined constants and write out the php code containing the current values?
  21. we actually know what you are trying to do, what we need to know is where you are stuck at when you tried to do it, because the purpose of programming help forums are not to write complete working code for you or to think out the details of each step that you need to do, we are here to help when you get stuck on something, after you have made an attempt at doing it. have you defined the user steps for the confirm-order process? what information do you have and what will be displayed at each step, and what new information do you need from each step? seems that if the visitor is viewing the contents of the cart and are ready to finalize the order, wouldn't the next step be to collect any new customer information or confirm existing customer information, such as ship/deliver address (it could be different for each order), billing name/address (if they use a different payment method, this can be different for each order), ... then create a record in an 'orders' table for this customer on the date and time in question that would assign an order_id, that you would then use to relate and store the contents of the cart in an 'order_items' table? once you have done this, you would have a 'pay' form/button that would submit/take them to the paypal site for the actual payment process. the code for the particular step of inserting the data into the 'order_items' table, would in its simplest form, be to just loop over the contents of the cart, form and run the INSERT query with the order_id, the dish id, quantity, and if the price can vary, the price at the time of the order. for bonus points, you can form and run one efficient multi-value insert query, rather than running a query in a loop. the code you have for your session based cart needs to be simplified. you should create a $_SESSION['cart'] variable that is an array of the cart contents - $_SESSION['cart'][dish id] = quantity. this will eliminate all the substr() statements in the code. also, if the quantity of an item in the cart is/reaches zero, you should remove it from the cart. since the value stored in the cart is the quantity, you can just use php's array_filter() function to remove empty items from the cart.
  22. the program logic that you are showing us makes no sense. the $event->nodeValue you are testing to come up with a color is the last value from the loop that's building the $events array. unless there's only one event or all the events are the same type, this won't result in the color matching the event type. regardless of your intent to loop or not, the value you test to come up with the color needs to be the $message value you are actually displaying. as to your current question, it's not clear if you want the first event or the first event of each type to be displayed. if you show an example of what the $events array looks like (and is the data from the rss feed in any particular order?) and what result you want based on that data, someone can tell you how to proceed. btw - your big long switch statement can be changed to a single program statement, assuming you switch (pun intended) to using a data driven design. see the following - // define the event type/name to color association (this would typically be stored in an included config file or in a database table) $color_mapper['Tornado Warning'] = 'rgba(255, 0, 0, 0.4)'; $color_mapper['Severe Thunderstorm Warning'] = 'rgba(255, 165, 0, 0.4)'; $color_mapper['Tornado Watch'] = 'rgba(255, 255, 0, 0.4)'; $color_mapper['Severe Thunderstorm Watch'] = 'rgba(219, 112, 147, 0.4)'; $color_mapper['Flash Flood Warning'] = 'rgba(139, 0, 0, 0.4)'; $color_mapper['Flood Warning'] = 'rgba(46, 139, 87, 0.4)'; $color_mapper['Flash Flood Watch'] = 'rgba(0, 255, 0, 0.4)'; $color_mapper['Flood Watch'] = 'rgba(46, 139, 87, 0.4)'; $color_mapper['Flood Advisory'] = 'rgba(0, 255, 127, 0.4)'; $color_mapper['Winter Storm Warning'] = 'rgba(255, 105, 180, 0.4)'; $color_mapper['Winter Storm Watch'] = 'rgba(70, 130, 180, 0.4)'; $color_mapper['Winter Weather Advisory'] = 'rgba(123, 104, 238, 0.4)'; $color_mapper['Special Weather Statement'] = 'rgba(255, 228, 181, 0.4)'; // at the point of mapping the event type/name to the color $alertColor = isset($color_mapper[$message]) ? $color_mapper[$message] : 'default value goes here...';
  23. how many different databases? 5, 10, 100? there are no wild-card ALL database commands. to do this under program control would require that you get (from the schema data) or manually make a list of the databases with the tables you want to use as the source, then run a query for each source database that inserts into the destination database using an INSERT ... SELECT query. the mysql documentation for the INSERT ... SELECT query states it can be used to insert from multiple tables, which should include (untested) the ability to specify the database.table. assuming this will work for a UNION query, you could build the SELECT part of the INSERT ... SELECT to be a UNION query between all the database.tables that you need. to do this manually, the most efficient way i can think of would be to do .sql backups of all the source databases/tables that you need. then edit them so that the USE database commend points to the destination database. then simply import the data using the .sql files.
  24. here is the sample code i posted above, with more details showing how it would control the building of the sections - // the business logic retrieves and stores the query results in an array $data. this serves as the input to this section of ocde if(empty($data)){ // there's no matching data, output an appropriate message here... } else { // there is data, start producing the output... $last_type = ''; // initialize this before the start of your loop that's display the data foreach($data as $row){ $strPrefix = substr($row["Prod_Type"], 0, 2); // output the product type heading each time it changes if($last_type != $strPrefix){ // the type changed if($last_type !=''){ // not the first section, close out any previous section here... // close the </div> for example } // start a new section here.... start the <div>, output any type image, .... $img = $types[$strPrefix]['img']; $alt = $types[$strPrefix]['alt']; echo "<img border='0' src='$img' alt='$alt'>"; $last_type = $strPrefix; // remember the new value } // output the data in each recored here.... } // you would close the last section here. whatever logic you have in the if($last_type !=''){ ... } above would be repeated or in a user written function that you call }
×
×
  • 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.