Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. OK, I thought you might need a SWITCH statement in the query to handle all the updates in one query. But, after further review I see that the UPDATES are all the same. The differences are with the WHERE conditions for determining which records to update. That males it a little simpler since you just need to build a combined WHERE condition. It looks complicated, but isn' that hard. You have to know what the final query should look like. Then create the code to dynamically create it. //Define the cuttoff conditions for WHERE clause $cutoff_scores = array(25=>7,50=>14,75=>28,90=>40); $CUTOFFS = array(); foreach ($cutoff_scores as $score => $rebox_interval) { $CUTOFFS[] = "(last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL {$rebox_interval} DAY) AND (right_count / (right_count + wrong_count)) <= '$score')"; } //Define the WHERE clause $WHERE = "WHERE box < 7 AND (\n" . implode("\n OR ", $CUTOFFS) . "\n)"; $query = "UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() $WHERE"; $result= mysqli_query($link, $query); The resulting query would look like this UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() WHERE box < 7 AND ( ( last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND (right_count / (right_count + wrong_count)) <= '25') OR ( last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 14 DAY) AND (right_count / (right_count + wrong_count)) <= '50') OR ( last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 28 DAY) AND (right_count / (right_count + wrong_count)) <= '75') OR ( last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 40 DAY) AND (right_count / (right_count + wrong_count)) <= '90') ) Also, you should see from the above the answer to your original question. Since you don't have a Right_Percent column in the database and calculate it dynamically (as you should) you just need to use that calculation in the WHERE condition as well. No need for a subquery.
  2. Hmm . . . that query is invalid. Are you modifying it before posting here? EDIT: I am seeing several things to be addressed: 1. You are trying to execute the variable $interval as the Query! WTH?! Not even sure what $interval should be used for as it doesn't appear in the $query. So, why is the loop even needed? 2. There are two values being set but there is no comma between them (this makes the query invalid) 3. You are creating a date string in PHP to be used in the query. That's unnecessary. You can either just use CURDATE() in the query OR if you want that date always updated whenever the record is updated, change the field to a timestamp with the option to auto-update. Then you don't even need to add the date field to your query. It would just be updated to the current timestamp whenever there is a change. 4. Same goes for $cutoff_date. It can be set directly in the query 5. You are correct - you do not need to run multiple queries. But, because there is an obvious problem with how the "values" of the array are not used in the query, I can't provide a solution. Edit #2: Scratch the comments regarding the problem with not knowing how you are wanting to use the value of the array in the query. I see it is used to calculate the cutoff date.
  3. Yes, it's absolutely possible. But,you are talking about how to "display" the data. You would just query the data and have logic (i.e. code) for determining the output to display as you wish. SO, this is really a PHP question (assuming you are using PHP). Please show the code you have so far and the problems you have. I suspect this is a homework problem and don't want to just give you a solution. And, don't bother wasting your time telling me it is not a homework problem. I won't believe you. EDIT: By the way, that DB schema is not very good. Having columns for CatA, CatB, CatC and CatD could be problematic. Would there always be exactly four categories for each record and no need to increase or decrease the number in the future? If no, then the schema is wrong. Also, is there a legitimate distinction between CatA, CatB, etc. or is it just a way to allow four categories? If there is no real significance between the four, then the schema is wrong. Not to mention that you should be using a date field for the month instead of a string. What happens when the year changes? Plus, you can't ORDER the results correctly based on name. The schema probably needs to be more like this: id | date | cat 1 2014-1-1 23 2 2014-1-1 34 3 2014-1-1 33 4 2014-1-1 78 5 2014-2-1 21 6 2014-2-1 38 7 2014-2-1 67 8 2014-2-1 78 etc . . .
  4. Maybe I misunderstood some of your requirements. As far as giving options for varying price, efficiency etc. you would have to add additional details about the "parts" and how they are used in the calculation. Assuming the parts already have a price associated with them. So, you would want some way to use an existing value (e.g. diameter) or assign a score to the parts for any other attributes you want to compare - such as efficiency or throughput. This really take a detailed analysis of the possible parts and how they affect the formula in order to configure correctly. Plus, I assume there are some dependencies between parts. For example, you can't just switch out a 2in valuve for a 4in valve on the same pump without necessitating other changes. So, you may have to have an option to increase the diameter for all parts. Here's an example of how the problem could be approached. Let's take the formula for flow rate: Flow Rate = 1/4 X π X (pipe diameter)^2 X velocity You could show options for greater efficiency based on parts with a higher diameter. "How" that option is displayed to the user is inconsequential. It should "just work". I suspect it would take a lot of time, analysis and coding to create the right framework. But, if done correctly, it shouldn't take any code changes to maintain.
  5. Whether the current solution is too convoluted or not is hard to say. If there are requirements to have some things "dynamically" change on the web page without a submission and/or refresh then you will need to have some integration between PHP and JavaScript. And the preferred method of exchanging data between the two is JSON. But, it could still be more complicated than it needs to be. It's really hard to provide much guidance without some knowledge of how the variable configuration options work. But. I have some experience with variable products that may help. I would start with a table of "options". This would just contain basic details such as the name of the option, default value, and the "type". The type would correlate to field types such as text, radio button group, check box, select list, etc. Depending on the type, they may require additional details. So, for the "select" types there would be a supporting table where there would be entries for the possible values for the select list. Likewise, a radio group would have something similar. A text type may have a supporting table with data such as max length (or whatever variable information about the options that you feel is necessary). You can then use the options table and the associated option details table to build your forms. If you are building a form from scratch, use the default values defined for each option. If you are recreating a form, pull the saved data and build a form using those values to populate the form. I would create a function for each field type for the purpose of creating the output. So, a function for a select list would take parameters for things such as the name of the field, an array of the possible value and the selected value. Either pass the default value or the saved value as the selected value. The function would create the select list and set the appropriate value. Now, if you want the user to be able to interact with the form and have content update dynamically without a page submission, then you do need to use ajax. It's typically considered a better approach to pass data back and forth and have JavaScript act upon that data. But, it may be easier to have PHP generate the HTML and pass it to the JavaScript to update the page. But, even if you were to just pass JSON to the JavaScript code to create the dynamic content, once it is done - it is done. If done properly, you can add/edit/delete options to your hearts content in the database and it should require no reaction in the code.
  6. +1 What he said. If you are creating dynamic tables, chances are extremely high that you are simply doing it wrong.
  7. That won't work for what the OP is wanting. He is wanting to automatically get content based on the execution of the JavaScript. But, it seems, he is just scraping the page. Can't execute JS from within PHP.
  8. Or, contact the website owner and first ask for permission to re-purpose their data. They may already have a service to return all the data in a more manageable format.
  9. I will second fastsol's response. If the example you posted was exactly what you saw then I would guess it is because someone ran into problems with undefined variables. Then that person received instructions on how to fix it (by defining it beforehand) and mistakenly assumed that it had to always be done. Some other instances where this would be used is any scripts that 'expect' data to be sent from the user, such as form processing scripts. I see a lot of instances of somethign like this if(isset($_POST['submit'])) { //Process the form $name = $_POST['name']; . . . etc. } That would cause a problem if the form was submitted without 'name' in the post data. That not the same as being submitted with an empty value. This shouldn't occur if the user is using your form, but it is bad behavior to assume that all the fields are included in the post data. I typically use something like this $name = isset($_POST['name']) ? $_POST['name'] : ''; It is always a good idea to turn error reporting on to it's highest level to identify undefined variable type errors. They won't typically stop a script or cause an error otherwise. But, doing this will greatly help in preventing lost time in debugging errors. If you were to mistype a variable name and no errors were displayed you might spend a lot of time to find the typo. But, if you enable full error reporting it would be displayed on the page.
  10. I can answer neither of those questions. I don't have enough understanding of the data structure or the logic of your code. And, I am not going to invest the time to obtain either. I stated pretty clearly in my first response that I was only providing very general advice. if($items['Masquerade Ball Ticket']=='ticket') So, are you saying ALL items have a property called 'Masquerade Ball Ticket' and that it has a value of 'ticket' if they are a ticket type of product? If so, that would likely work. But, I'm guessing that 'Masquerade Ball Ticket' is more likely a VALUE that some products have. E.g. $items['product_name'] = 'Masquerade Ball Ticket'. Why would all products have a property called 'Masquerade Ball Ticket'? As I stated, the correct way to do this is to have a property for all your products that lets you identify which one's are tickets and which ones are not. If you can't do that, then you will need to check the individual products in the code (my second example) which is a far less preferred solution. As for where it goes, that's up to you. You already stated you hard-coded the message for "This is a ticket". So, somewhere before that line is executed you need to make a decision as to what the text should be. It could be in the code to produce the invoice or before you call that code.
  11. I guess I'm not following. I don't see anything in the two code sections you posted with "This is a ticket" hard-coded. You state you only want to change the "This is a ticket" text based on the product ordered. But, then you provide two different code sections: one for a ticket and one for an invoice. So, I'm not sure if you just want to change that text or if you are wanting to select one of the two sections of code based on the order. Plus, I don't know if it is possible for a person to buy multiple things in an order and what you expect in that situation. So, I can only provide some very general advice. Assuming these products are in a database, I would have a property for the products to identify if they are a ticket or not. You may already have this. So, when you query the product details to display an invoice/ticket, include that property. Then use that property to display the right title. if($items['property_name']=='value_to_identify_ticket') { $title = "This is a ticket"; } else { $title = "This is an invoice"; } //Use title in the code to generate the output This is the best option since you only need to edit the product details when managing the products. You should never have to touch the code. However, if for some reason this isn't possible you would have to create code to check what product is being produced. This is problematic as you would have to edit the code to support more products. There are many ways to accomplish this, but a switch() would probably be easiest. You would want to use some value associated with the products which is unique such as a product ID or name. switch($items['id']) { case 'ticket1ID': case 'ticket2ID': case 'ticket3ID': case 'ticket4ID': $title = "This is a ticket"; break; default: $title = "This is an invoice"; break; }
  12. You obviously don't know what SQL Injection is. You should really go read an article or two to understand it. It has nothing to do with INSERTing records in the database. It has to do with injecting unintended SQL code into the query statement (SELECT, INSERT, UPDATE, DELETE, etc.)
  13. Do you know that Windows already has an On Screen Keyboard? If I was a disabled person I would already be using that. So, any website that added another keyboard via JavaScript would probably only get in the way.
  14. That isn't very helpful. What is it doing or not doing that is incorrect? But, I will comment on your logic: 1. Why are you passing the email address in the request? The activation code is all you need. 2. There is no error logging, so it is impossible to say why the code is not working as expected. The code to update the DB and send the email is wrapped within an if() condition. So, if one of those two values are not set, it will not run. Since we can't see the construction of the URL that is used, it's impossible to know if that may be the source of the problem. Or, the query could be failing for all we know. 3. No need to include the DB connection file if you may not run a query . . . but, don't bother doing a pre-validation of the key since if it isn't 32 characters it won't find a match anyway 4. You need to prevent SQL injection. 5. Why is there a limit on the query? Are you reusing the activation keys? There are more than you can comprehend. 6. I don't see anything in the code to define $username, $password or $randomnumber Here is a different take on the process that may help <?php $activation = isset($_GET['key']) ? $_GET['key'] : false; if (!$activation) { echo "No activation key passed"; } else { // Update the database to set the "activation" field to null include ('database_connection.php'); $activation = mysqli_real_escape_string($dbc, $activation); $query = "UPDATE members SET Activation=NULL WHERE Activation='$key'"; $result = mysqli_query($dbc, $query); if(!$result) { echo "An error occured trying to activate your account"; //Debug line only, log errors in a production environment echo "Query: $query<br>Error: " . mysqli_error($dbc); } elseif(!mysqli_affected_rows($dbc)) { echo "Your activation code does not appear to be valid"; } else { //Activation completed. Send a customized message. ### ADD CODE TO GET USERNAME & ACOUNT NUMBER FROM DB AND TO GENERATE TEMP PASSWORD $message = " Your Account has now been verified, Below are your Details.\n __________________________________________ Username: $username Password: $Password Account Number: $randomnumber __________________________________________ Please Ensure you keep safely."; if(!mail ($email, "Activation Success", $message, 'From:My site<no-reply@xxxx.com>')) { echo "There was a problem sending your activation details"; } else { echo '<div class="success">Your account is now active and your Account details sent to you. You may now <a href="login.php">Log in</a></div>'; } } } ?>
  15. I think this is what you need (not tested): SELECT * FROM table JOIN ( SELECT table.replyID, MAX(table.replyUnixTime) AS lastReplyTime FROM table GROUP BY table.replyId ) as lastReply ON table.replyID = lastReply.replyID AND table.replyUnixTime = lastReply.replyUnixTime ORDER BY lastReply DESC
  16. Actually, I don't think it will necessarily return what he is wanting. That query will return "one" record for each unique replyID along with the value of the oldest replyUnixTime value. When you do a group by, there is no way to determine from which record in the grouped set is being used for the ungrouped fields. I.e. just because you use a modifier to get the oldest replyUnixTime for the grouped records doesn't mean the rest of the data on that result is from the same record. I believe you have to do a subquery and JOIN the table on itself. http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html
  17. Why are letters in a language you don't understand "weird"? I believe those characters are from the Tamil language.
  18. Yep, looks like a homework assignment to me.
  19. Psycho

    Statistics?

    No, you create one database with separate tables to store different types of data. You don't separate it by teams. But, just looking at the example page you linked to, the database design will take a good amount of work to get right. If you don't understand database normalization you have got some research ahead of you before you can even get started. Otherwise you will design something that will be an absolute beast to work with.
  20. Make the text boxes read only. Then the user can't directly edit the fields. But, this is a very bad idea IMO and not sure what your reasoning is to do this. If a user has JS disabled they would not be able to use your form. Plus, what is the purpose? Are you thinking this adds security somehow?
  21. ^^ What he said Use this SELECT * FROM events WHERE date >= CURDATE() ORDER BY date ASC, time ASC LIMIT 5 Also, using * in your select is typically a bad idea unless you really need all the fields. List out only the fields you need
  22. You're not passing a value for the answers! You should have the answers in a separate table with their own ID so you can pass the ID of the answer. With what you have you need to pass the text of the answer which could cause all sorts of problem if you are not escaping the data and handling it both in the output and in the POST data.
  23. If you are "storing" this data, it should be in a database. You would want two tables. The first table would have fields for the values which each record would have only one value for. For the repeating data of a variable length, you would store those as separate records in another table that reference the parent record. But, as far as working with the data in your code, an array makes sense. For the repeating data, I would create a sub-array.
  24. If the structure of data can change over time, you definitely have to think ahead. But, the solution isn't that difficult. Also, "how" the data is provided by the user (text, radio, etc.) doesn't necessarily impact the database. The selected value is the selected value. For Orders, create a table with the data you know will ALWAYS apply to orders. Order No, date, user, etc. etc. Then create a 2nd table to hold custom data associated with orders. Each record will hold one value associated with an order. But, you also need a 3rd table that will define the 'current' custom fields that should apply to orders. As you add/remove fields from this table it would drive the options available when processing a new order. Then when an order is saved you add records for each value to the custom data table with a foreign key back to the fields table. Ass for products and their production steps. I would have one table of all the possible production steps. Then another table that will associate each product with the production steps that apply and the order they belong.
  25. You might also consider changing the format of the array. I notice that all of the arrays within "box" 3 have the same 'pos' value. Do all the records in a "box" always have the same 'pos' value? If so, you could simplify thing by formatting the array something like this: leitnerArray ( [boxes] => Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( [pos] => v5g [tenses] => Array ( [0] => agaru [1] => agarimasu [2] => agarimasen [3] => agaranai . . . etc. You should try avoiding repeating data such as that.
×
×
  • 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.