Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. Echo out the query to the page so you can verify what the variable content and the resulting complete query contain. My guess is that $fruitid does not contain what you think it does (i.e. it is empty, thus the " . . . the right syntax to use near ''". Plus, if it should be a numeric value, mysqli_real_escape_string() is not the function you want to use. You want to ensure the value is numeric.
  2. You can and do want to use preg_split() to split on the space. But, you need to use a lookbehind in the RegEx. That will find a space that is preceded by the four digit year without the year being consumed as part of the match. $cars = array( 'Subaru WRX 2010 Manual Trans', 'Ford Fusion 2012', 'Chevy Silverado (standard key) 2011 Automatic' ); foreach($cars as $car) { $parts = preg_split('/(?<=[0-9]{4}) /', $car); print_r($parts); echo "<br>\n"; } Output
  3. Microsoft even has a white paper on the subject that delves performance and functionality concerns regarding the two: : http://research.microsoft.com/apps/pubs/?id=64525
  4. That is true, yes. But unfortunately not all browsers will pass the value of an INPUT TYPE="SUBMIT" when the form is posted using the enter key (may not be the case with current browsers, haven't checked in a while). Checking the REQUEST_METHOD is a sure fire way to know if a form was POSTed.
  5. The queries you provided are referencing fields that do not exist in the tables you provided. Can't really help if the information provided is inacurate. Specifically, I don't see the following fields in the tables: production, unitname, & subtype
  6. The issue is that the query is failing. You need to check for errors. The query isn't selecting anything. It would look something like this SELECT COUNT(*) FROM banned WHERE ip=$user_ip" But, if your intent is to prevent a user from inserting a duplicate value. If that is the case, that is the wrong way to approach the problem. The reason is "race conditions". The process of checking if a value exists and, if not, inserting the new record takes some amount of time between those two processes. Yes, it is typically a very small amount of time (milliseconds) but it can and does happen. You instead need to put a unique constraint on the database. Then you just need to perform the INSERT query. If the new value would create a duplicate the query will fail.
  7. I don't know what your actual data looks like, but I *think* the issue you are facing is probably due to the WHERE clause on the second query WHERE T2.subtype = 'k' Since that is not part of the first query, the second query is likely excluding data that is included in the first query. You should be able to solve this by implementing a LEFT JOIN and putting the unitname filter as part of the JOIN criteria Give this a try SELECT filename, hour, SUM(IF(type='x', production, 0)) AS x_total, SUM(IF(type='y', production, 0)) AS y_total, SUM(production) AS k_total, FROM T1 LEFT JOIN T2 ON T2.unitname = T1.unitname AND T2.subtype = 'k' GROUP BY filename, hour
  8. There are many pros and cons for storing files in the database vs. in the file system. You should do some research on what those are and then review what it is you are specifically trying to accomplish to determine which is appropriate for your needs. But, by and large, I store files in the file system. The number 1 reason would be performance. If the file is an image that will be displayed on a web page it would be much, much more efficient (and easier) to just pull the file path from the database and create an image tag. If the image content is stored in the DB you have to pull all that data and then construct an image resource to pass to the user. But, if I had a requirement where the images contained sensitive data I might consider storing them in the DB with encryption.
  9. So, if the user submits a form by pressing the Enter key on their keyboard you just ignore the submission? Kind of an odd way to treat your users, but to each his own.
  10. Or, if ($count == 0){ //handle the 0 } elseif ($count >= 1 && $count<=10) { //handle 1 to 10 }
  11. Hmm, not to be rude, but did you actually read the code? I even provided comments as to what it is doing. And, it does exactly what you state it should do. Upon page load, it calculates the number of millisecond till the next full second (previous script calculated number of millisecond till next 10 second period) and sets the setTimeout() interval to that number of milliseconds. So, if the user loads the page at 1:23:42 and 150 milliseconds, the timer (in the updated script) is set to reload the page at 850 milliseconds (exactly 1:23:43 and 0 milliseconds). The previous script would have set the reload time to 7 seconds and 850 milliseconds (i.e. exact 1:23:50 and 0 milliseconds). So, no matter what time the initial page load occurs, the first sync (and every sync thereafter) will be at the exact same time (based on each PCs system clock).
  12. I had another thought. I am assuming that the intent of this is that users will see the updated content at exactly the same time. And, perhaps, they will be competing against each other. So, not only would you want the users to see the content at the same time, we want to ensure that the first one to respond actually gets credit for responding first - again we can't control how fast any response is from or to any PC from the server. If the above is (or is similar) to what you are trying to achieve, I probably would use AJAX and take the following approach: Using a setTimeout() event as above to execute at exactly every event second: 1. Check if previous update content was retrieved. If so, output it to the page 2. Request new updated content 3. Repeat from #1 This way, if it takes one user 15 milliseconds to retrieve the content and another person 45 milliseconds, they will still see it at the exact same time (assuming their computer clocks are synced) Secondly, I would implement a JavaScript submit function that would populate a hidden field with a timestamp and then submit the form. This would ensure that each user is credited with the exact time they submitted their response. And, on the server-side you would have to make sure each response is saved with that timestamp and there would need to be some delay (a couple seconds) before actually reporting who responded first.
  13. Not sure I follow. The above script will update the page on 10 second intervals based on the users system time. I did misread the requirement to be 10 seconds as opposed 1 second. But,per the requirements, he wants to refresh based on the computer clock. The above script will do exactly that. If the clocks are not synced, there's nothing that JavaScript can do. Of course, we could set the refresh time on the server to make sure they will all be set exactly the same irrespective of the user's computer time. But, with only 1 second intervals, the transmission/load times would probably make it just as inaccurate. Here is the code updated to only 1 second intervals to occur at exactly the 1 second mark based on the computer's clock. But, with 1 second intervals I question the value. Just running the page locally takes 15-30 milliseconds to refresh. If the page it being requested via the internet it will take considerably more. So, just forcing a refresh at the same time does not guarantee that each user will get the refreshed page at the same time. <html> <head> <script type="text/JavaScript"> function autoRefresh() { //Create timestamp upon load var loadTS = new Date(); //Calculate milliseconds to next even 10 second interval var millisecondsToRefresh = 1000 - (loadTS.getMilliseconds()%1000); // ##### BEGIN DEBUG CODE ##### document.getElementById('loadtime').innerHTML = loadTS; document.getElementById('loadmilliseconds').innerHTML = loadTS.getMilliseconds(); document.getElementById('refreshmilliseconds').innerHTML = millisecondsToRefresh%1000; // ##### END DEBUG CODE ##### //Set page refresh setTimeout("location.reload(true);", millisecondsToRefresh); } </script> <title>PHPMIND – Javascript Tutorials</title></head> <body onload="autoRefresh();"> <h2>JavaScript Auto Refresh Page</h2><br> <p>This page will be automatically refreshed at every even 1 second interval.<p><br> <p>Page was loaded at: <span id="loadtime"></span> and <span id="loadmilliseconds"></span> milliseconds.</p><br> <p>It will be refreshed in <span id="refreshmilliseconds"></span> milliseconds.</p> </body> </html>
  14. You don't need AJAX. You can just use JavaScript to force a refresh at every even 10 second interval (1:25:00, 1:25:10, 1:25:20, etc.). Although you have to keep in mind it always takes some length of time to refresh a page - it is not instantaneous - even if it is a few milliseconds. The more content on the page the longer it takes to load. Here is a working script. Just put the script in the head of your page and call the function in the onload trigger of the body. Remember to take out the debug section when you put it on your actual page. The first page load may trigger a refresh anywhere from 0-10 seconds in order to sync up with the even 10 second intervals. <html> <head> <script type="text/JavaScript"> function autoRefresh() { //Create timestamp upon load var loadTS = new Date(); //Calculate milliseconds to next even 10 second interval var millisecondsToRefresh = 10000 - ((loadTS.getSeconds()%10*1000) + loadTS.getMilliseconds()); // ##### BEGIN DEBUG CODE ##### document.getElementById('loadtime').innerHTML = loadTS; document.getElementById('loadmilliseconds').innerHTML = loadTS.getMilliseconds(); document.getElementById('refreshseconds').innerHTML = Math.floor(millisecondsToRefresh/1000); document.getElementById('refreshmilliseconds').innerHTML = millisecondsToRefresh%1000; // ##### END DEBUG CODE ##### //Set page refresh setTimeout("location.reload(true);", millisecondsToRefresh); } </script> <title>PHPMIND – Javascript Tutorials</title></head> <body onload="autoRefresh();"> <h2>JavaScript Auto Refresh Page</h2><br> <p>This page will be automatically refreshed at every even 10 second interval.<p><br> <p>Page was loaded at: <span id="loadtime"></span> and <span id="loadmilliseconds"></span> milliseconds.</p><br> <p>It will be refreshed in <span id="refreshseconds"></span> seconds and <span id="refreshmilliseconds"></span> milliseconds.</p> </body> </html>
  15. No. The very last line in that script will include the form. You point the user to this script - not the form. The file with the form would not be directly accessed, only included in the script above.
  16. There are many ways to do that. I typically make the form a separate file then have a single 'controller' page that is always called. That page determines if the form will be displayed (initial request for the form or there were errors) or if the form passed validations and should be processed. Here is a simple example: contactform.php (this is the controller page that would be user facing, i.e. requested via the browser) <?php //Set variable to hold any validation errors $errors = array(); //Check if form was POSTed if($_SERVER['REQUEST_METHOD']=='POST') { //Perform validation logic //If there are any errors append them to the array $errors //Check if there were any validation errors if(!count($errors)) { //Perform additional processing logic, e.g. inserting into DB //Errors can occur at this stage as well, if so append to the $errors array } //Verify if all validation and processing succeeded if(!count($errors)) { //Redirect user to a success page using a header() redirect //This has the added benefit of clearing the $_POST values to prevent double submissions with a page refresh header("Location: success.php"); } } //Include form. If the form was posted and there were no errors, this logic would not be run include('mycontactform.php');
  17. Here was the link: http://www.programmerinterview.com/index.php/general-miscellaneous/html-get-vs-post/ Also, here is a relevant link from the official W3C site: http://www.w3.org/2001/tag/doc/whenToUseGet.html
  18. $_GET values are meant to "get" data. So, if you have a page to display a product page you might have a URL such as showProduct.php?prod_id=55 because you want to "GET" the details for product 55. Get requests should not change data. $_POST values are meant to "post" (submit) data to the server to do something with it (add/edit/delete). Post requests are meant to change data. I'm guessing your "action" variable is used to determine what you are going to do. For example, I have some applications where all requests get run through a single core page which determines what actions I will take. Instead of setting the action directly through a POST/GET variable, you can dynamically determine the action based on what data you received. Here's a page that gives a more thorough explanation. I didn't read the entire thing, so I can't say whether it is all 100% accurate.
  19. No. For that, you want preg_split()
  20. Not enough information to really answer the question. Are there more details than the email address and company associated with these records? If there are two records for the same company and the same email address - what is the logic on which record to keep? They could have different data in the other fields. And what other fields ARE there? To do this we would likely need something such as a primary key field or a timestamp to ensure we delete all but one record.
  21. Well, this doesn't seem right: //Every 1000 documents stop and send the bulk request. if($1 % 1000) { $responses = $client->bulk($params); 1. The variable should be $i not $i. In fact, I believe that would cause an error. have you not even run the code at all? 2) The modulus will return an integer of 0 or greater. When you have an integer as a condition (i.e. if() statement) and any positive integers will be treated as TRUE. Only a 0 will be considered FALSE. With an incrementing numerator and a divisor of 1,000 the result will be a TRUE condition 999 times out of 1,000. You should change $i to be $i+1 and then check for that condition to be FALSE if(($i+1) % 1000 == false) { But, when you are running iterative processes over folders you have to be careful that you will not have an exceptionally long processing time. If you are going to run from the command line there won't be an issue with a timeout, but there could be memory issues. I really don't know enough about what you are doing and the purpose to give great advise on how best to proceed. If this is user facing you might want to store all the folders in a DB, then have an AJAX request to process x number of records until they have all been completed.
  22. So your real request is to write the code for you? Have you made an attempt? Start with the following then come back with any questions. 1. When loading any page that will display links/controls to make a vote: Do a check to see if the user has already responded. If so, disable/hide those controls and/or provide a message that let's them know they have already voted. If not, then enable/display the controls. This is really only for usability - not specifically to prevent multiple votes. 2. When a user submits a vote. You will also want to perform a check if they have already voted because anything you do on the actual page can be easily overwritten. If the user has a previous vote, then don't save the new one (or overwrite the current one if you want to allow users to change their vote). If you are going to have multiple polls then you will want two tables. One to define the polls and another to log the votes by each user.
  23. Do you realize that many people can (and will) be using the same IP when behind a NAT? Any home (and many businesses) use a router with, NAT, that connects through the ISP and has one external IP address. Then, internally, all the connected devices have local IP addresses. All requests go through the router using the external IP address. When the responses come back, the router determines which internal IP to direct the response to. So, a web application will only see the external IP address of all the machines behind that router. There is really no perfect way to prevent people from voting multiple times. You could use a cookie as a first-line of defense (of course it can be deleted by the user). Then, if you really want to make it difficult, require that users are registered and authenticate before they can vote. Then only allowed users to vote once. Of course, they could create multiple accounts, but you would verify the email address and it would be a major PITA for users to vote multiple times at that point.
  24. Because your regex doesn't match the content format. You show double double quotes in the content, <div class=""footnotes"">. But, in the regex you are using backslash double quotes '/(.*)(\div class=\"footnotes\"\>.*?\<\/div\>)/s' There's no reason to backslash the double quote in the regex since you are delineating it with single quotes. You are backslashing a lot of things that don't need to be. Plus, this preg_match would be a better solution than replace in this scenario. <?php $input = 'Some beginning content <div class=""footnotes""> <br> <hr align=""left"" noshade=""noshade"" size=""1"" width=""150"" /> <blockquote> <a href=""#f1"" name=""fn1""> <span class=""superscript""> * </span> </a>Some footnotetext</blockquote></div> some ending content'; preg_match('/(.*)<div class=""footnotes"">/s', $input, $match); $text = $match[1]; preg_match('/(<div class=""footnotes"">.*?<\/div>)/s', $input, $match); $footnote = $match[1]; echo " Text: " . $text; echo "Footnote: " . $footnote; ?> Output: Text: Some beginning content Footnote: <div class=""footnotes""> <br> <hr align=""left"" noshade=""noshade"" size=""1"" width=""150"" /> <blockquote> <a href=""#f1"" name=""fn1""> <span class=""superscript""> * </span> </a>Some footnotetext</blockquote></div>
  25. I would make one slight tweak to what Barand provided. I would still use a session value to save the user's language selection, but I wouldn't use that variable when determining which language to use for the output. Instead I would determine separately 1) What language to use and 2) What is the user's selected language. Here's why: The fact that a user has not selected a language may be an important piece of data. Yes, you should have a default language to fall back on, but sometimes you may need confirmation that the user has selected a language. For example, if you just showed pages in a specific language that the user is not familiar with it may be difficult for them to understand what items to select to change it. Instead, you may provide them with a prompt when they visit your site if they have no solved selection that includes text in all the supported languages asking them to make a selection. So, I would do something such as this: session_start(); //Set the default presentation language $presLang = 'Esp'; if (isset($_GET['lang'])) { $_SESSION['lang'] = $_GET['lang']; // store newly selected lang } if (isset($_SESSION['lang'])) { $presLang = $_SESSION['lang']; // set presentation language if user selected } Then, use $presLang when determining which language to create output in. When showing a select list for the user to choose a language I would use the session value and add a "Select Language" option if they have not previously selected one. $langOpts = "<option value=''>- Select a Language -</option>\n"; foreach ($langs as $k => $l) { $sel = ($k == $_SESSION['lang']) ? 'selected="selected"' : ''; $langOpts .= "<option value='$k' $sel> $l</option>\n"; }
×
×
  • 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.