Jump to content

roopurt18

Staff Alumni
  • Posts

    3,746
  • Joined

  • Last visited

    Never

Everything posted by roopurt18

  1. It's because startNumber is a String object and + means concatenate. outputNumber = (Math.floor(Math.random() * numberDifference) + parseInt(startNumber));
  2. Well, I don't see why file_get_contents() would go to the trouble of converting dates itself. 1) It's just extra overhead 2) It's undesirable; if I'm calling a function to get a file's contents I want the contents, not modified contents. My guess is the difference in format is occurring at donbest.com I bet their applet is set up to spit the date out in a human-friendly format if the requesting agent is a browser and spits it out in a computer friendly format for robots or other requests, such as that made by file_get_contents. (edit) BTW you're not crazy, I was able to reproduce the same exact thing. (edit2) You can test this with a plug-in that allows you to switch user agents. Just have your browser identify itself as a google or yahoo bot and try to access the same page.
  3. Perhaps connection.php isn't establishing the connection properly? Someone else suggest removing users. from your column names, since you're only selecting from a single table. I can't see how that'd make a difference, but it might. You might also enclose all of your column names and the table name in backticks. (EDIT) You should really echo the output from mysql_error(); that'd be your biggest clue.
  4. Hmmm. I don't know crap about IIS so I can't help you in solving that. I can suggest that if you're doing this just for learning purposes to install XAMPP and just use that.
  5. I take two different approaches. When something is not mission critical, which is to say it doesn't directly apply to something I'm currently working on, I can take a more leisurely approach. In this case, I usually buy a book and read it in my spare time or during a hot bath. I find reading in the bath to be quite effective for a number of reasons. There's limited distractions because running water drowns out other noise, there's no clock so I don't feel pressed for time, I'm soaked so I don't feel like falling asleep (unless I'm unusually tired), etc. I just find that environment allows me to focus more on what I'm reading, which means I retain it better later. My goal with leisurely learning is not to learn something in refined detail; but rather to familiarize myself with an overall process, discover things I didn't know before, etc. As an example, for a long time I didn't know MySQL directly supported importing comma-separated data from files. As such, I would write a PHP script to import such data. However, now that I know that I can do this directly with MySQL, next time I need to do so I'll use the built in mechanism supported by MySQL. I don't know the syntax off the top of my head, I'll still need to look it up. But just knowing that it can be done will save me time from having to write a short script. When something is mission critical and I need to learn it ASAP, I first gather any resources that may be helpful. The first place I look is google or online documentation. If I can't find what I need in a few minutes, if I know I own a book that mentioned it I'll check in that book. Otherwise, I spend as much time as necessary finding examples and tutorials on the web. I don't immediately try and apply them to my situation. I always create a simplified example first that strips out any extra complexity introduced by whatever application I'm trying to use them in. For example, if I'm creating a complex file upload form and want to apply AJAX to upload files as soon as the user selects them in the browse control, I'll create a test script with only that control and the bare minimum of Javascript to get it working. Once I've assured myself that I understand the concept and principles, and found any little "gotchas," only then do I apply it to the real problem. In either situation, the actual learning comes from me writing small examples of my own and making sure they work. I know that a lot of people when working through a tutorial will actually follow the tutorial to the letter. I don't usually do this. Almost every tutorial contains some amount of bloat and I try to strip that out as well and focus only on the concept I'm learning. (edit) I forgot to mention that when I'm taking the hands on, mission critical approach, I'll get the concept in as little as 5 min (sometimes I only need to read a short article) or as long as a couple days. It all depends on how good the documentation I can find is.
  6. Well, everything looks OK at first glance. The best thing to do in these circumstances is to gather information. Place this at the top of your script, after the <?php // Spit out $_POST echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>'; Next, change this line: $result = mysql_query("select ID, users.FirstName, users.LastName from users where users.UserName ='".$User."' and users.Password ='".$Pass."'"); to these lines: $sql = "select ID, users.FirstName, users.LastName from users where users.UserName ='".$User."' and users.Password ='".$Pass."'"; echo $sql . "<br>"; $result = mysql_query($sql) or die(mysql_error()); Once we get your querying issues sorted out you should work on cleaning data before attempting to use it in the database. (edit) Good eye, thorpe. I missed the incorrect operators while setting $_SESSION!
  7. You need to use Javascript to update the page as you type. It is possible to create every possible permutation of your data and feed it to the Javascript upon the initial page load. However, it is extremely inefficient to do so. Almost every site that has auto-suggestion type features uses XHR in Javascript.
  8. *ahem* I do my hosting through web host freaks and serverpowered. I don't get the least bit angry when it goes offline because I've always gotten excellent treatment from them. I know they strive for 100% uptime (doesn't everybody) but nothing is fail safe. I know they don't just willy-nilly go around taking servers offline for no reason; so if it is offline there is a good reason behind it. It's inconvenient yes, but I hardly find it something to be annoyed or, worse, angry about. I suggest that if you get angry at your web host you: 1) Switch hosts 2) Look into some anger management classes. Life's too short to spend it angry over minor details.
  9. No. You're not quite getting it. You have an array: Array( 9, 3, 17, 5 ) We can see very immediately the maximum value is 17 and it is at index 2. You said you wanted to search for the max in one array and see if it had the same index as the minimum in another array. The function min() takes an array as its argument and returns the minimum value. So if we pass the array above into min() we will get 3; three is the smallest value in that array. You said you wanted the index. Now we call array_search; array_search searches the array for the value and returns the index. We know the smallest value is three; we pass that into array_search and array_search tells us the index at which it occurs, i.e. 2 in our example. To summarize: 1) Start with an array 2) Determine the minimum / maximum value 3) Search for that value in the array and determine the position or index at which it occurs.
  10. What do you mean? Are you extracting the extension and just making sure it exists in Array( "gif", "jpg", "jpeg", etc. )? That's exactly what I said you shouldn't be doing.
  11. Look at the PHP manual documentation for the parameters expected by array_search; you will see the second parameter is the array to search in. <?php $a = Array( 1, 10, 8, 17 ); // The following two lines: $tmp = min($a); // get min value $i = array_search($tmp, $a); // search for min value in $a and return index // are just the long winded way to write: $i = array_search(min($a), $a); // basically instead of calling and assigning to a temp variable we are just passing // the returned value from one function into another ?>
  12. Teaching is only a waste of time if people refuse to learn.
  13. Lesson learned: Never take anything for granted in a program. Even while typing that response, I almost left off $pi, but then I thought better of it and said, "Hell, it's just a little more typing, might as well make sure it's what we think it is." We've all done this at one point or other. But! If you'd started with the line that was failing (which is what I recommended), you'd have saved that time. Trust me, most of us feel your pain.
  14. If all you want is the extension of the file: <?php $file = "some.file.txt"; $tArr = explode(".", $file); $ext = $tArr[count($tArr) - 1]; $new_name = rand(5, 10) . $ext; echo $file . "<br>" . $new_name . "<br>"; ?> Bear in mind however, that the extension of the file has no bearing on the type of file. You need to use another mechanism to validate that a file is the type you are expecting, such as an image, plain text, etc.
  15. OMG! I forgot code tags. /slap_self Depending on how you wrote it, you could write a library function that would handle that for you. The function would need as parameters: $sql - the query without an ORDER BY clause (because the function is going to add it) $sort_fld - the name of the sort field in $_GET $order_fld - the name of the order field in $_GET $fields - an array of the possible sort fields $default - the default field to use if one can't be found in get I think this should do it... <?php function query_build_sort( $sql, $sort_fld, $order_fld, $fields, $default ){ // Determine the sort field // $fields are the possible fields, $sort_fld is the name of the $_GET index if(isset($_GET[$sort_fld]) && in_array($_GET[$sort_fld], $fields)){ $sort_fld = $_GET[$sort_fld]; // use the one in $_GET }else{ $sort_fld = $default; // Not provided in $_GET, so use default } $order_fld = isset($_GET[$order_fld]) ? $_GET[$order_fld] : "ASC"; $order_fld = $order_fld == "ASC" || $order_fld == "DESC" ? $order_fld : "ASC"; return $sql . " ORDER BY " . $sort_fld . " " . $order_fld; } ?>
  16. Clicking should reload the page but it's up to you to get values out of the URL and modify your SQL query accordingly. This is one of those things that isn't built into the language and you have to handle it yourself, manually, each and every time you wish to use it. <?php $sort = isset($_GET['sort']) ? $_GET['sort'] : ""; $sort = mysql_real_escape_string($sort); $order = isset($_GET['sort']) ? $_GET['sort'] : "ASC"; $order = $order == "ASC" || $order == "DESC" ? $order : ""; $sql = <<<MYQUERY SELECT * FROM table WHERE ... MYQUERY; if(strlen($sort)){ $sql .= " ORDER BY " . $sort; if(strlen($order)){ $sql .= $order; } } echo $sql; ?>
  17. You'll also want to add an &order=the_order to your links so that you can sort in ascending or descending order.
  18. [quote]I voted self taught, but to be honest, I believe their should be another field, although this field would conflict with self taught. *text books* or just *books* in general, such as "SAMS teach yourself PHP in 24 hours" and books like that.[/quote] That's covered by self-taught.
  19. Heh, sorry. But you can prove to yourself they're the same by echo'ing each of the queries. The changes you recommended produce the same results. The problem stems from the OP not using mysql_real_escape_string() to clean all of the data before inserting it. However, there is a quick and painless way to accomplish what he wants. So before he rights a ton of messy code that sanitizes everything, it would be better to show the OP the easy way.
  20. First off, making the changes recommended by MasterACE won't accomplish anything. Second, check the MySQL documentation for INSERT ... SELECT. http://dev.mysql.com/doc/refman/5.0/en/insert-select.html You can directly insert from one table into another if you build your query correctly. Let us know if you get stuck from there.
  21. echo $pi, $angle[0], and $angle[2] before executing the statement $angle[1] = $pi - $angle[0] - $angle[2]; and figure out which is incorrect. Then figure out what in your code is causing it to be incorrect. Perhaps if you attached your entire script to a post someone could help you more.
  22. What is the value of $side[2] before this line: $angle[0] = asin(($side[0]*sin($angle[2])/$side[2])); And is that value correct? What exactly are you trying to compute anyways? My trig is a little rusty!
  23. SELECT * FROM tbl WHERE dateCol >= '2007-05-01'
  24. I doubt there's any performance to be gained from including them as the PHP interpreter has to parse extra characters; however the parser is incredibly fast so you should not concern yourself with this. The reason you include them is in case you add a statement to the body of the if later. What if you are debugging the page and want to quickly add a logging function to confirm the test is passed, but you forget to add the curly brackets then: // #1 if(isset($_GET['dir']) && ($_GET['dir'] == 'desc' || $_GET['dir'] == 'asc')) Logging::write("dir is set: {$_GET['dir']}"); $dir = $_GET['dir'] // #2 if(isset($_GET['dir']) && ($_GET['dir'] == 'desc' || $_GET['dir'] == 'asc')) $dir = $_GET['dir'] Logging::write("dir is set: {$_GET['dir']}"); In #1, the log will only be updated if the test is passed, but the $dir variable will always be set. This may or may not cause effects on the page that affect your ability to debug it. In #2, the log will always be updated which can confuse your debugging. Each of those cases only affect your ability to debug the script, which isn't a huge deal because eventually you'll notice. But what if you had put actual script code that affected the outcome of the script? It could take you a long time to figure that one out. Every book I've read on programming and every professor that I admired for their ability and knowledge always said the same thing: Use curly brackets, even when they're optional. I had many class mates who would only use them when necessary and they also tended to be the people that could never turn in a working program. Programming is a methodical process; be consistent in your style and it will pay off with time saved later. As a last note, I once fixed a very annoying bug when working as a contract programmer for a game development company. We were developing a casino game in C++ for windows and occasionally the damn thing would crash. None of us could figure out why. Well, one day on pure, random chance, I saw this in the code: for(int i = 0; i < len; i++) SAFE_DELETE(list[i]); What that code was intended to do was loop over a list of dynamically allocated objects and delete the memory associated with each of them. SAFE_DELETE was a macro defined as: #define SAFE_DELETE(x) delete (x); (x) = null; In C / C++, a macro is directly substituted into the code, so the actual loop became this: for(int i = 0; i < len; i++) delete list[i]; list[i] = null; Since the curly brackets are absent, the code is interpreted as: for(int i = 0; i < len; i++){ delete list[i]; } list[i] = null; What this does is for list[0] to list[max - 1] delete the item, which is correct; but it doesn't set them to null. This means elsewhere the code could still attempt to use memory at list[0], list[1], list[2], etc. that was no longer allocated for the program. Finally, after the loop ends, i is equal to the size of the list, so list[ i ] = null is actually trying to set a value that is beyond the end of the array; normally such an action would crash the program immediately and you'd catch it very quickly. However, for some reason or other, setting an array index beyond the bounds of the array never caused the program to crash so it was only my chance encounter with that code that caused the bug to be fixed. Once I fixed it, we all noticed that the program "mysteriously" crashed less. Lastly, some people like to place the curly brackets on a line all by themselves; I find this to be a waste of a perfectly good line and place them right after the closing paren of the expression, like cooldude demonstrated.
  25. IF (isset($_GET['dir']) && ($_GET['dir'] == 'desc' || $_GET['dir'] == 'desc')) $dir = $_GET['dir'] Place curly brackets around your if statements, always; just trust me on that one. Also, you are testing against 'desc' twice, what happened to 'asc'? Pen and scratch paper mostly or a white board and digital camera.
×
×
  • 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.