Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,354
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. see post #2 in the thread, point #2, about how i came up with categories from your existing code. this example is for your hard-coded rules for 'ManDoors' and 'Windows', converted into general purpose, data driven code. 'ManDoors' and 'Windows' are not specific items, they are at best categories that items are organized under. in general, you would use id's. since your current code doesn't have id's for categories, only names as part of method calls, i used the only information that i had available in the example. the offset is there for when you want to offset the quantity by a value, not dependent on how may 'used on' items there are. different variations of something, insulated vs non-insulated, are/should be different items/part numbers. each different item would have their own unit rule. since ManDoors seems to be a category, you would have categories for insulated ManDoors and un-insulated ManDoors. for the example given, you would add as many rules are there are cases - for white caulking rules - $unit_rules['ManDoors'] = array('mult'=>.75,'offset'=>0); $unit_rules['Windows'] = array('mult'=>.75,'offset'=>0); $unit_rules['ManDoors-insulated'] = array('mult'=>1.2,'offset'=>0); $unit_rules['Windows-insulated'] = array('mult'=>.8,'offset'=>0); for any cases like this, i would add a line item to the BOM, 'building height adder' that the rules in the the scissor lift quantity calculation would use - $unit_rules[building_height_adder_item_id_goes_here] = array('mult'=>0,'offset'=>1);. then if the building height adder is on the BOM, the scissor lift quantity calculation would result in 1 scissors lift rental. see the commentary in post #2. the whole point of what i posted is to move the values that are hard-coded in the program logic into a data structure, making this a data-driven design. once you get the hard-coded values out of the code, you can define them in a database table and let new rules get added or existing rules get edited by providing a user interface for the database table.
  2. promised example code - // define, or get from an sql query, the choices for the select/option menus $author_data = array(); $author_data[] ="ken davies"; $author_data[] = "arthur smith"; $author_data[] ="gill rafferty"; $author_data[] ="molly brown"; $author_data[] ="gilbert riley"; // define and populate the data for the other select/option menus. left up to you as a programming exercise. $genre_data = array(); $year_data = range(2002,2008); $publisher_data = array(); // define a list/array of expected inputs for the dynamic WHERE clause $choices = array(); $choices[] = 'author'; $choices[] = 'genre'; $choices[] = 'year'; $choices[] = 'publisher'; // produce the dynamic WHERE clause $and_terms = array(); // holds each term for the WHERE clause // loop over the defining array of choices foreach($choices as $choice) { if(isset($_GET[$choice])) { // the choice is set, add it to the terms to use $and_terms[] = "`$choice` = '$_GET[$choice]'"; // you need to either - validate that the input value is exactly one of the possible choices, properly escape the value using the database escape string function, or use a prepared query to supply the value to the sql statement. left up to you as a programming exercise. } } // db connection - convert to PDO and use exceptions to handle errors. left up to you as a programming exercise. $con = mysql_connect("localhost","root",""); If (!$con){ die("Can not Connect with database" . mysql_error()); } mysql_select_db("authors",$con); $where_clause = !empty($and_terms) ? 'WHERE ' . implode(' AND ', $and_terms) : ''; // build the sql query statement $sql = "SELECT * FROM books $where_clause"; // execute the query and retrieve the data into an array $result_data = array(); $myData = mysql_query($sql,$con); while($row = mysql_fetch_assoc($myData)) { $result_data[] = $row; } // start the html document/template ?> <html> <head> <title>My Page</title> </head> <body> <br> <form> <select name="author" size="2"> <?php foreach($author_data as $value) { $sel = isset($_GET['author']) && $_GET['author'] == $value ? ' selected': ''; echo "<option value='$value'$sel>$value</option>\n"; // you need to apply htmlentities() to any data being output. left up to you as a programming exercise. } ?> </select> <input type = "submit" value = "go"> <select name="genre" size="4"> <?php // use code similar to the above to produce each set of select/option choices. left up to you as a programming exercise. ?> <option value="adventure">adventure</option> <option value="biography">biography</option> <option value="crime">crime</option><br /> <option value="romance">romance</option> <option value="thriller">thriller</option> </select> <input type = "submit" value = "go"> <select name="year" size="4"> <option value="2002">2002</option> <option value="2003">2003</option> <option value="2004">2004</option> <option value="2005">2005</option> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> </select> <input type = "submit" value = "go"> <select name="publisher" size="4"> <option value="blue parrot">blue parrot</option> <option value="yonkers">yonkers</option> <option value="zoot">zoot</option> </select> <input type = "submit" value = "go"> </form> <?php // you need to use css to style your table border. left up to you as a programming exercise. echo"<table border='3'> <tr> <th>id</th> <th>author</th> <th>title</th> <th>publisher</th> <th>year</th> <th>genre</th> <th>sold</th> </tr>"; foreach($result_data as $row) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; // you need to apply htmlentities() to any data being output. left up to you as a programming exercise. echo "<td>" . $row['author'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['publisher'] . "</td>"; echo "<td>" . $row['year'] . "</td>"; echo "<td>" . $row['genre'] . "</td>"; echo "<td>" . $row['sold'] . "</td>"; echo "<tr />"; } echo "</table>"; ?> </body> </html>
  3. your form should use method='get' since you are determining what will be displayed on the page. also, don't use nonsense variable names like $bird, $cat, ... and in fact, if you aren't doing anything to the value in a variable, there's no good reason to copy one variable to another, just use the original variable. next, when you are doing the same processing/operation on a set of data (your series of drop-down select menus), don't write out every possible combination of values. just make a list (array) that defines the expected inputs, then loop over this list to access the input data. you would produce and store each column = 'value' and store them in an array. then, just implode the array with the ' AND ' keyword to produce the WHERE clause in the query (implode will work even if there is only one entry.) you would also want to dynamically produce the select/option menus, so that you can make the selection 'sticky' so that a user doesn't need to keep re-picking things they have already selected. you would do this by making the choices a list/array, then just loop over the list when you produce each select/option menu, outputting the 'selected' attribute in any existing selected option. you would also use these defining lists/arrays to validate the input data. if your list of possible choices are actually coming from a database table, you would query for this data to produce the select/option menu, rather than have them in a hard-coded array. lastly, you should use a prepared query, with place-holders for each data value. when you switch your code from the obsolete mysql statements to use PDO (the best choice) or msyqli, you would also convert the query to a prepared query. if i have some time, i will post an example. edit: you should also separate the concerns in your code. the majority of the php logic should come first, then at the end have your html document/template that has a minimum of php logic in it. this will help make it easier to switch from the obsolete mysql statements, since the php logic using them will be grouped together and not be interspersed in the html markup for your document.
  4. the easy answer to the Sliding Tec Screws, is, to just enter the number of screws per door as the multiplier for each door entry.
  5. creating a new entry would be fairly straight forward - 1) enter the name and select the category for the new item. inserting this into the product/item table would assign an item id to it. the item id would be used in any related data and logic. 2) if the new item is used by other unit items or categories of items, you would need a way of searching/listing and selecting (checkboxes) which of the existing items/categories it goes with. the category id or item id of the selected item would become the $unit_rules key/index. you would enter any multiplier/offset or other values you determine you may need. any group rules you have define would have a way of selecting just the possible choices. if at all possible, i would avoid allowing any arbitrary logic/tests to be entered. i'll have to give some thought to the Sliding Tec Screws case.
  6. feel free to use, modify, or ignore any of these thoughts - 1) in general, if you find yourself making variables/properties/constants and functions/methods where the name is specific to the name of a data item, so that any time you change the amount of data items, you must write new/delete variables/properties, constants, functions/methods, you have an inflexible hard-coded design, that is both taking your time writing and testing, but also maintaining. your code should instead be general purpose, where the names of things indicate their purpose or function, not the name of the data they operate on. see the next item for some examples - 2) $this->getTotalQuantityManDoors(true) + $this->getTotalQuantityWindows(true)). i'm betting that the code for each of these methods is identical, except for some values being used, and you have a bunch more of these type of named methods. these are operating on categories. if your product/item table has categories, you could simply use one common getTotalQuantityByCategory(category_goes_here) method. the same is probably true for a bunch of other hard-coded functions getting quantities of items. just have a common getTotalQuantityByItem(item_id_or_name_goes_here) method. 3) customer supplied items. it sounds like you have a bunch of code/logic/parameters to handle this special case, repeated many times? i'm assuming prices are stored in a product/item table and are multiplied by the BOM table quantity to come up with the total price for each item? to handle customer supplied items, just add a price multiplier column to the BOM table. for items that are customer supplied, store a zero in this column. for items that are purchased for the project, store a one/use a one as the default value for the column. multiply the item price * bom quantity * this new column to come up with the total price for any item. no extra handling is needed. the BOM quantity can be used as is to calculate any dependent quantities. 4) i would use a data driven design, where the rules are defined in data (that can be stored/retrieved from a database table.) this will remove the values from the logic and let you have just one copy of the logic that is re-used with different sets of rules. you can have 'unit' rules, that apply to each quantity of an item/category, and group rules, that apply to the total quantity in a group (i would put rounding up/down, box quantities .. here). for each item/category, define a multiplier and an offset. as you are looping over the main items, just apply the multiplier and offset to come up with the quantity. for any group, once you have the quantity for the group, apply the group rules. here is a pseudo code example for the white caulking - case (self::whiteCaulking) : $unit_rules = array(); // note: this example is for categories, not individual items, though you could mix them easily by adding an indicator to the defining array if the $key it a category or an item and call the appropriate method to get the correct quantity $unit_rules['ManDoors'] = array('mult'=>.75,'offset'=>0); $unit_rules['Windows'] = array('mult'=>.75,'offset'=>0); $group_rules = array(); $group_rules[] = array('operation'=>'function','name'=>'ceil'); $quantity = 0; // apply any unit rules foreach($unit_rules as $key->$rule) { $quanity += ($this->getTotalQuantityByCategory($key) * $rule['mult']) + $rule['offset']; } // apply any group rules foreach($group_rules as $rule) { switch($rule['operation']) { case 'function': $quantity = $rule['name']($quantity); break; } } $quantities[] = $quantity; break; i didn't work out how this would apply to the slidingTecScrews example, but it should be possible to make this work for any case you have now. you will find that the actual php logic for each case statement is or can be made to be the same, just with different defining arrays. when you get to that point, you would just set up the two arrays of data for each case, then call a common function/method to calculate the quantity and return it to the calling code. once you get to this point, you can eliminate all the switch/case logic and just store/retrieve all the rules in a database table and loop over them in turn to calculate the bom quantities.
  7. if someone tries to log in and the entered username/password combination doesn't match, that doesn't mean they haven't registered. what if they miss-typed the value for either field? you would output a generic 'The username/password didn't match' message for this case and let them try to log in again. if the current visitor is not logged in, you would provide a link to the registration page, near the login form, for them to click on if they desire to register. you would this when they register, not when they log in.
  8. you would only allow logged in users to post. your log-in system would store the user id in a session variable. you would both test if the session variable is set before presenting any output needed to post comments and processing any form submission and you would get the user id from the session variable when forming and running the INSERT query to save the posted comment. you would get the product id from the content you are displaying that you are allowing comments to be posted for.
  9. <option ... ></option> tags need value='...' attributes, not id's. i recommend that you write and test one small section of code at a time and only go onto the next section when you have determined that previous section works correctly.
  10. it's not clear what you are asking, since we don't know what your 'business rules' are that determine which query to run or what result you expect. however, i suspect you are asking how can you form and run any arbitrary SELECT query without writing and repeating different variations of the php logic depending on what the query is? if so, you need to write a general purpose query method, that you extend the main database class with, that accepts a built up sql query statement and an optional array of bound input data that corresponds to any place-holders in the sql statement. your main application code would build the sql query statement and the array of bound input data, then just call the general purpose query method when it needs to execute a query. unfortunately, you shouldn't use msyqli to do this, since it will require dynamically binding the input and the output data, which will require either using call_user_func_array() or the reflection class, since you must supply a variable number of parameters to the mysqli bind_param() and bind_result() calls. if you instead use the PDO extension, doing this is simple, straight forward, and will run fun significantly faster than using msyqli based code.
  11. since there was a php syntax error, that means the php.ini where you have placed the error_reporting/display_errors settings likely isn't the one that php is using. you need to make sure that your development system is set up to report and display all errors before you waste any more time on problems.
  12. because, for the example code you are posting, it has a php syntax error in it, and the code never runs. do you have php's error_reporting set to E_ALL and display_errors set to ON in the php.ini on your development system so that php will help you by reporting and displaying all the errors it detects? note: for a php syntax error in your main file. you cannot put the error_reporting/display_errors settings into your code since your code never runs in this case to make use of those settings.
  13. there's a whole bunch of things wrong with this code that will prevent anyone from helping with it. some of the problems - 1) global $some_variable; only has meaning when used inside a function, where it shouldn't be used anyway since it leads to bad coding practices and, if this code is inside a function, that's a bad design since a function should only do one small thing. 2) you cannot (successfully) use the ip address to associate a cart with a user. if the user is logged in, you would use the user id. if the user isn't logged in, you would use the session id. 3) to display the contents of the cart, you would run one JOIN'ed query to get the cart and the product details at one time. 4) making the product price an array, then using array sum on an array containing one entry makes no sense. you would also want to take into account the quantity of the item when calculating the total of the cart. calculating the quantity times the running total inside the post method form processing code makes no sense. 5) any post method form processing code should be separate from your html markup and be near the top of your code file. it should also validate the submitted data (what if the value is negative number, what if the value is a zero) and use a prepared query to protect against sql injection. your update query is also missing an important part, which row(s) is it supposed to be updating? after successfully running the post method form processing code, you should do a header() redirect to the same page to cause a get request to re-display the page. 6) you would want to output the existing quantity as the value in the quantity form field so that the user can edit the quantity. 7) you would also process any remove checkboxes in the form processing code. 8 ) the form field for the quantity needs to be an array name and use the product id as the array key and you might as well have the 'remove' checkbox use the product id as the array key instead of as the value. 9) if the cart is empty, you would want to output a message stating so.
  14. your design has some user experience UX problems. by using a select/option menu, the user must first click the select button to see the two choice to pick from and since each week is its own form, only one selection can be submitted at a time. if someone was setting up all 52 weeks, it would take 156 clicks and 52 form submissions and re-displays of the page. if you instead use radio buttons and a single form, the user can go through and select or change any of the week values by simply clicking on the choices and hitting one submit button. that would take only 53 clicks and one form submission to set up all 52 weeks. p.s. you are missing a " in your html markup, probably lost due to the escaping \" going on. you should use single-quotes ' inside of a double-quoted string so that the code is not cluttered up with escape characters.
  15. For the case you have just posted, php's output buffering is turned on in the php.ini or possibly .htaccess file (if php is running as a server module) on your server. You can determine this by looking at the output from a phpinfo() statement.
  16. It sounds like your code also contains output buffering statements. these will have an affect on the OUTPUT of the content to the browser and could cause one instance of a header() to work and the other to not. if that isn't what is causing the current symptom, you will need to post the code that reproduces the symptom and identify which header() statement in it does and which doesn't trigger the error message. also, for the case where you don't get the error message, does the header() redirect work or does the code stay on the same page? it could be that the error is occurring, but the message is being hidden or suppressed.
  17. i would run one JOIN'ed query, then calculate the $result1 value while looping over the rows from that one query. the following query should (untested) work - $query = "SELECT g.Location, g.Date, g.HomeTeam, g.AwayTeam, t1.Teamdata1, t2.Teamdata2 FROM datagmaes g JOIN gamesdata t1 ON g.HomeTeam = t1.Team JOIN gamesdata t2 ON g.AwayTeam = t2.Team"; you should NOT apply number_format() to the Teamdata1 and Teamdata2 values. number_format() is an output function and should only be applied when you display the number, not when calculating or storing data.
  18. textarea's don't have value='...' attributes. you output the existing content in between the <textarea>content goes here</textarea> tags.
  19. firstly, you are not dealing with web links. include/require statements deal with files, i.e, paths and names. what does echoing $_SERVER['DOCUMENT_ROOT'] give? If the server is set up correctly, it will give - /home/youraccount/etc.., i.e. the path to your account's document root folder, and note the leading / which refers to the root of the current disk. you would just append the folder to where the included files are at to this value, not append the entire path to your document root folder again.
  20. i'll give same advice i gave a little over an year ago when you moved from shared to dedicated hosting - http://forums.phpfreaks.com/topic/294527-transfer-my-website-from-shared-hosting-to-dedicated-server/
  21. yes, just about anything is possible in programming, that's why this is called software. if it doesn't do what you want, it can be changed so that it does. however, this code is badly organized, to the point that it would take rewriting and retesting it just to get it to the point where it could be understood and then be modified to work differently. the code should be organized as follows - 1) initialization 2) post method form processing code 3) get method business logic - code that gets/produces data that's needed for the dynamic content on the page 4) get method presentation logic - code that knows how to produce the dynamic output on the page (for simple processing, this can be handled in the html document/template system.) 5) html document/template - produce the html document, or portion this code is responsible for, based on the result from the above sections
  22. there's no (good) reason that an application wouldn't allow any number of users to concurrently visit any page on a site and produce a result specific to the visitor. the code must therefore be doing something wrong, which we cannot tell you without knowing what exactly the code is. code would typically either use session variables or store data in a database using the user's id and any number of different visitors could be accommodated.
  23. assuming you are doing this as a learning exercise - you don't. users/admins to a site don't need to know or care what the database table structure is, so, you don't need to limit what tables (i'm assuming you mean database tables) they have access to and if you are dynamically making database tables to hold the content for each different user/admin, that's not how you would do this. the database tables you have, are what your APPLICATION needs to accomplish it's goal. it's your application code that has access to the database tables. all the content for a Content Management System would be stored in the same database table. controlling who can Create, Update, or Delete record(s) in that table would be handled by the user permission system, based on the user's assigned permissions or assigned role and ownership of the record(s). you should have a (one) user table that holds the unique user information. the user permission system would store its data in a separate set of tables, using the user id from the user's table to associate the assigned permission to the user it goes with. as to a user permission system, do some research on ACL (Access Control List.) in its simplest form, this defines actions that can be performed (create, read, update, delete) and assigns those actions to specific users. by creating groups of actions, you define roles, that you can assign to a user (in addition to any of the specific, overriding, actions), such as an owner(superadmin), admins, authors, members, guests. to limit Create/Update/Delete access to only those records a specific user created/authored, in addition to storing the author's user id in a column in the row of data, there would be a set of defined actions that limit access to the user with the matching user id. to allow an admin to be able to edit anyone's content, there would be a different set of defined actions that ignore the id of the author of the record. initially, to get the permission system started, you would manually store the user id of the person who is the owner/superadmin into the permission system, with a defined action that allows him/her to perform all possible actions. when that user logs in (and re-authenticates when making changes to user permissions), they would be able to, using an appropriately written user interface to the permission system, assign roles and/or specific actions to other users and also create and manage the actions and roles of the permission system. the code for any 'action' on a page would test if the current user has permission to perform that action and if the specific permission requires it, ownership of data, before performing the action.
  24. and if you make an array of the bedspace values, like they had been retrieved from a db table, you can dynamically produce the bedspace select/option menu by looping over the array, without needing to write out and the test and fix all that code and markup. btw - the <select ... value="..."> tag doesn't use a value="..." attribute and the one you are showing in your code should be removed since it doesn't do anything.
  25. there is no 'regex replace' in mysql. the regex pattern match would only let you find the rows containing the data that you need to update. to do this in a query would require that you use string functions to find the position of the start/end of the number in the string, split the string to get the number part, add the amount to the number, then replace the value in the string. you would be better off retrieving the data using php and use preg_replace to modify the data. if this is a regular occurrence, i.e. you set/update the value regularly, the value should be stored separate from the string it is in.
×
×
  • 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.