Jump to content
Larry Ullman's Book Forums

HartleySan

Members
  • Posts

    3047
  • Joined

  • Last visited

  • Days Won

    243

Everything posted by HartleySan

  1. No problem, Larry. Thanks for writing awesome books, and actively posting on your own forums. You're the only web development related writer I know that does that.
  2. Oh, okay. Well, if you're only grabbing one row at a time, then my first solution would have worked, like I suggested. Although, if that's the case, you also don't need the while loop. Or I should say, you could use the line you erased in place of the while loop. You definitely don't need both though.
  3. Well, I agree with you there, but specifically for the purpose of creating an Ajax object, try-catch just seemed like overkill. But you're right, with try-catch, the user can add extra functionality easily, if necessary. I was looking at w3schools.com's implementation for creating an Ajax object from the following website: http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp Their code is as follows: var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } Their implementation completely skips out on one of the ActiveXObject types that you test for. What do you think about this method, as compared to yours?
  4. Without looking at your code (sorry, but I don't have the time at the moment), I can say the following: The header("Location: $url"); function simply jumps to the URL stored in $url. For example, if $url is equal to http://www.amazon.com, that's where the page is going to go. The fact that it worked on your local machine, but is not working on a server simply suggests that the $url is no longer correct/exists. As I suggested before, I would simply echo the final value stored in $url, and compare that to what the path should be for the URL of index.php (or whatever file you want to jump to), and do whatever is necessary so that the path is correct. For example, let's say we have the following site structure: http://www.example.com/index.php http://www.example.com/add_user.php http://www.example.com/edit_user.php And you want the script to jump to index.php, unless one of the other two pages is set in a variable. Simply do the following: if (isset($page_name)) { $url = 'http://www.example.com/' . $page_name; } else { $url = 'http://www.example.com/index.php'; } header("Location: $url"); exit(); Is that what you want to do? Just remember that the server is not doing anything differently from your local machine in regards to the header() function. Just know that the path to the file might be different, which you need to account for. Without more specific information, we cannot really help you, though. Sorry.
  5. Could you please give us some more details? Perhaps post some relevant code segments, or if there are any messages being output to the screen, what are those? Also, try echoing things to the screen, to see if you can diagnose the problem yourself.
  6. Sorry. I somewhat misspoke. When you call the absolute_url function without any arguments, it defaults to "../index.php". Sorry. That's my bad. Because you issue is very much dependent on your specific environment, try outputting the final value of $url to the screen without redirecting the page, and see what it is. After you see what URL is being called, I bet it'll be obvious what the issue is. Something about that ../ part seems odd to me, but I don't have the book in front of me at the moment to confirm what Larry does.
  7. While it's probably not the main issue, try erasing the following line right above the while statement, because I do not think it's necessary: $row = mysql_fetch_array($result); I think the main problem is that you're assigning values within the while loop, but not outputting anything within the while loop, so you simply continue to overwrite the values in the variables, and are left with only one set of results (mainly, the last set of results). Actually, the fact of the matter is, I don't see any echo statements whatsoever, so I have no clue when/where you're doing the displaying.
  8. Creating that first database, and creating that first connection, and having everything work together for the first time is a really cool feeling. After that, the possibilities grow exponentially.
  9. I'll take a stab at this one. First off, when you use ../ for a directory path, you're telling the system to go to the parent directory of your current directory (i.e., the directory in which your current script is being executed). As such, if you change "../index.php" to just "index.php", it'll probably work. But again, that depends on your directory structure. Assuming that your index.php file is stored directly below the path stored in $_SERVER['PHP_SELF'], it should work. As for while the logout script is working, well, if you look at your call to absolute_url, you're not sending any arguments, in which case the following is returned: 'http://' + $_SERVER['HTTP_HOST'] + dirname($_SERVER['PHP_SELF']) + '/' While that's just a path, and not a file name, remember that if there is either an index.html or index.php file in that directory, that file will be automatically loaded without having to specify the file name. For example, if I had an index.php file in the following path: http://www.example.com/scripts/index.php I could load that index.php file by simply typing the following in my URL bar: http://www.example.com/scripts/ And most likely, the same thing is happening in your logout script. I could be wrong, but after looking at your code real quick, that's what I'm thinking is the issue. Please let us know if that helps. Thanks.
  10. As an afterthought, the field I tested my recommended statement out on was the TIMESTAMP type, and it still worked perfectly fine with NOW(). Just wanted to let you know that it should work fine with TIMESTAMP as well.
  11. Well, according to an earlier post, your field name is "Log", not "log", so be careful of that. Also, I believe Larry talks about this in the book, but you should use the DATE_FORMAT() function in MySQL to format the DATETIME data the way you want it when you return it from the database. See the following for more info and examples on how to do that: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format Other than that, you echo statements look fine for outputting the info to the screen. Again, Larry talks about this sort of thing in his book quite a bit, so I would check that out for more examples and explanations.
  12. All right. I cannot really comment on the eval() or parseJSON() functions, as I never use them. Sorry. If something else strikes me, I'll try and help, but otherwise, please post your solution when you find it.
  13. Most likely the data you are returning from the PHP file is not in the right format. I have read a bit about this topic, and honestly, I think the best solution is to simply return a string in the format of a JS object declaration. For example, if we wanted to declare the following object in JS: var oCar1 = { iWheels : 4, iTopSpeed : 120 }; Then we could return a similar string in PHP as follows: $iWheels = 4; $iTopSpeed = 120; echo '{ iWheels : ' . $iWheels . ', iTopSpeed : ' . $iTopSpeed . ' }'; And then back in your Ajax response event, do the following: var oCar1 = oAjax.responseText; The following site talks more about JSON, which you seem to already be familiar with: http://www.json.org/ Anyway, I just as well figure that avoiding the use of eval() and paraseJSON() altogether is best.
  14. Is your query working at all? I mean, do you get an error, or does it just not update the time? I ask because I pulled out a database that I had locally and I used the following statement in phpMyAdmin to test out my suggestion: UPDATE tutorial1 SET lastupdated = NOW() WHERE id =3 Anyway, I did that, and it updated my lastupdated column (which is of the type TIMESTAMP) perfectly fine for the row where the id is 3. I'm not sure what else to tell you without you providing us with some more information first. Sorry.
  15. Don't have the means to verify this at the moment, but try the following: Replace the following: $login = 'NOW()'; $query = "UPDATE users SET Log = '$login' WHERE UserID ='$UserID'"; With this: $query = "UPDATE users SET Log = NOW() WHERE UserID ='$UserID'"; I think the usage of the single quotation marks (which interpret everything literally (i.e., not as functions, etc.) is screwing things up.
  16. No problem with answering your questions. That's what the forum is for. I suppose everyone has their own way of doing things, but I personally like to use a virtual server set up on my machine, and test everything out there before going live. It's a lot easier and faster, and also, you don't have to worry about a potential bot compromising an incomplete site. There are a lot of WAMP (Windows, Apache, MySQL, PHP/Perl/Python) packages out there, but I prefer XAMPP. Perhaps it's just because it's the one that I have always used, but I have never had any issues with it, so I can't not recommend it. Just go to the XAMPP website, download the Windows version and follow the simple instructions on screen. After the installation finishes, a command prompt will come up with some questions to answer. Generally, I elect to not bother setting a password for MySQL at the beginning (since I'm not worried about security issues on my local machine). Note that if you go that way, the default installation will put everything in a folder called xampp on your C drive. All scripts have to go into the htdocs folder within the xampp folder. If you wanted to, for example, test out a script called myScript.php, you'd put that in the htdocs folder (subfolders are also okay), start up the XAMPP Control Panel (from the Start menu, etc.), start the Apache server, and then type http://localhost/myScript.php to run the script. It should work fine. Note that localhost points to C:\xampp\htdocs by default, but that can be easily changed, if you want to. Also, if you want to access phpMyAdmin, etc., you also need to first start up MySQL from the XAMPP Control Panel. After that, the easiest thing to do is simply type localhost into your browser URL bar, and then click on phpMyAdmin from the bar on the left. As a final note, if you use the default settings for MySQL (including no password), you need to use the following settings to access a MySQL database: Host: localhost User name: root Password: (none) (Generally, you need to specify two single quotation marks without any spaces between them ('') to represent this in PHP.) Database: database-name (Obviously this depends on the actual database name you use.) Well, I don't mean to sell you on XAMPP, but it really is the easiest way to go, I think. Good luck, and feel free to ask any other questions.
  17. Well, I think that's your answer right there. You either don't have the show_image.php file, or it's in the wrong folder. It's been a while since I went through this book, so I don't recall well, but I checked the index of the book, and there is a show_image.php script starting on page 566. Perhaps if you wrote that script out and placed it in the appropriate folder. I find it odd though, as the show_image.php script is in a completely different section, and no real reference is ever made to that script in 10.4. Hmmm...that is odd. I'm thinking if you write the script on page 566, and modify it as necessary, it should work. Please let us know if that works or not.
  18. chris, I'll try and lend a hand, but I am honestly confused about a couple of things. For one, all we have to go on is the following: "i can,t get the window to load in script 10.4 The html form works, the links are there, the images are in the folder, sharing is on." Honestly, this isn't very clear. When you say that the window won't load, do you mean the pop-up window? Also, you say the "HTML form", but there is no form in your code. Do you mean just the HTML page? Anyway, I'm going to venture a guess at the problem: Probably, the images folder cannot be accessed for one reason or another. I'm not sure about WAMP, but I use XAMPP, and in that, all files/folders that are accessed from localhost must be in the htdocs folder, so I'm thinking that your virtual server (or whatever it is that you're testing this on), cannot access the images. Are there any errors that are output when you click on a link, etc.? If you're using a browser like Google Chrome, you can load up a JavaScript debugger, and see if there are any errors being generated on the JS side of things. Again, it's hard to diagnose the error without more information. Whenever I have an issue with PHP, I generally start echoing things left and right. I highly recommend echoing a lot of stuff out to the screen, and making sure that everything is as you have intended. If everything is as it should be, then there is probably an issue with the environment you have set up (e.g., permissions are not set properly, a folder isn't where it should be, etc.). Anyway, try some stuff out, and please report back. Thanks. Edit: If the problem was a syntax error, then some sort of error would be output to the screen, but since just nothing is being displayed, then there is some sort of runtime error.
  19. Paul, I am no expert on this topic, but I'll share what I (think I) know, and certainly, others can chip in: Basically, when it comes to servers, Windows sucks. It's kind of a well-known fact that the only computers in the world where MS has any foothold are those used in people's homes for general use and in the office for simple tasks. Otherwise, Unix/Linux reigns supreme, and I have no doubt that phpMyAdmin vs. MyLittleAdmin is the same thing. I have never ever heard of MyLittleAdmin, so I can't comment on that. However, I imagine that MyLittleAdmin would be robust enough that you could easily back up any databases you have already built in it, and then import those databases into phpMyAdmin, although I don't know for sure. Certainly though, it does not seem like it would be a problem. In terms of having to back up your website, I think what they're referring to is that they're not willing to move everything over for you for free. Most likely, if you simple take all your HTML/PHP pages, backed up databases files, etc., and temporarily back them up on your hard drive until your hosting service ports your account over to Linux and phpMyAdmin, then you can easily re-upload everything to your new Linux server and import the database files using phpMyAdmin, and it'll all work fine. In terms of the difference between Windows hosting and Linux hosting, like I mentioned above, without a doubt, Linux hosting is better and you should have no issues. I don't know all the details, but it sounds to me like your best bet would be to back up everything you want to keep onto your local HDD ASAP, and then tell your hosting service to switch you over to Linux, and then re-upload everything. A couple of things that you might want to specifically ask them are the following: - Is it possible and easy to back up a database from MyLittleAdmin and import that database into phpMyAdmin? - Is all your account information the same (i.e., is your user name and password the same)? Beyond that, you should be good to go. If you aren't already, I recommend using FileZilla for your FTP client. And if you're on Windows, I prefer XAMPP for setting up both a virtual Apache server and MySQL database environment for testing everything locally. In fact, if you want to download XAMPP right now, you can play with phpMyAdmin in there, and see if you like it, and you're able to do everything you want to do. Well, hope that is of some help. Let us know if you have any additional questions.
  20. Yeah, I guess that's my point. Try-catch seems to be for error handling, but since you're not doing anything in the event of an error, why not just use the standard if-else if-else syntax?
  21. That is odd. I just used phpMyAdmin supplied with XAMPP to copy and paste your statement into the SQL entry box, and it worked perfectly fine. Honestly, don't know what to tell you. I'd check out some MyLittleAdmin forums and see if anyone is having similar problems, etc.
  22. One other thing I mistook is the following: $iTotalHours .= $value; If you use .=, it'll add up the values as strings (i.e., append the values). For example, if $iTotalHours is '0', and $value is '1', you'll get '01'; As such, you need to do the following: $iTotalHours += $value; That'll treat the values like integers, essentially typecasting them. Anyway, I made a really simple script that seems to work. It has no error testing, so feel free to adapt it to your needs. I have confirmed though that so long as you enter integer values, it works everytime. It also automatically handles the absence of a value as 0. flightlogtest1.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>Flight Log Test #1</title> <meta http-equiv="Content-Type" charset="text/html; charset=utf-8"> </head> <body> <?php if (!empty($_POST)) { $bIsHour = true; $iTotalHours = 0; $iTotalMins = 0; foreach ($_POST as $key => $value) { if ($bIsHour) { if (is_numeric($value)) { $iTotalHours += $value; } } else { if (is_numeric($value)) { $iTotalMins += $value; } } $bIsHour = ($bIsHour) ? false : true; } $iTotalHours += floor($iTotalMins / 60); $iTotalMins %= 60; echo 'Total hours: ' . $iTotalHours . '<br>' . 'Total minutes: ' . $iTotalMins . '<br>'; } ?> <form action="flightlogtest1.php" method="post"> <p><label for="hour01">Hour 01: </label><input type="text" name="hour01" id="hour01"></p> <p><label for="min01">Minute 01: </label><input type="text" name="min01" id="min01"></p> <p><label for="hour02">Hour 02: </label><input type="text" name="hour02" id="hour02"></p> <p><label for="min02">Minute 02: </label><input type="text" name="min02" id="min02"></p> <p><label for="hour03">Hour 03: </label><input type="text" name="hour03" id="hour03"></p> <p><label for="min03">Minute 03: </label><input type="text" name="min03" id="min03"></p> <p><input type="submit" value="Submit"></p> </form> </body> </html>
  23. I think I found the problem. Submitted form data is always in string format. As such, if you test form data using the is_int() function, you will always get false, thus the problem. However, you can use the is_numeric() function to test for the existence of numeric string data (i.e., numbers submitted via a form). Try replacing is_int() with is_numeric(). It should work then. For more info on the is_numeric() function, see the following: http://www.php.net/manual/en/function.is-numeric.php
  24. Paul, thanks for the correction. I use a Japanese keyboard, and the - and = are on the same key. I must have forgotten to hold down the Shift key. Anyway, sorry you're getting an error everytime. I have not tested my code out at all, so there may be a small issue that I overlooked. I recommend going through each step, and testing for where the problem is. Obviously, for some reason, at least one of the values is causing the $bNoErrors flag to be changed to false, which is causing the error message to be output everytime. Actually, I really should have tested out my code last night (but I didn't). I would first run the foreach loop and output all the key-value pairs to ensure that your data is there as you expected. For example: foreach ($_POST as $key => $value) { echo 'Key: ' . $key . ', value: ' . $value . '<br>'; } You should get something like the following: Key: hour01, value: 3 Key: min01, value: 50 Key: hour02, value: 4 ... If you don't get something like that, then something is up with the input data being posted to the other PHP script. Also, test out the absence of a value and letter values, etc., to ensure that regardless, the data is getting to the PHP form all right. After you have ensured that, you might want to echo something for each step of the loop to see where values are going in the loop. For example: foreach ($_POST as $key => $value) { if ($bIsHourValue) { if (is_int($value)) { $iTotalHours .= $value; echo $key . ' is an integer and was added to the total hours.<br>'; } else if (!empty($value)) { $bNoErrors = false; echo $key . ' is not an integer and this is where the loop is broken.<br>'; break; } $bIsHourValue = false; } else { if (is_int($value)) { $iTotalMin .= $value; } else if (!empty($value)) { $bNoErrors = false; break; } $bIsHourValue = true; } } Naturally, you would add similar echo statements to the minutes part as well. Again, I wasn't able to test my code, so I'm not sure, but perhaps all the data being received from the form is not being interpreted as integer values, but as strings. This will be obvious if the loop is being broken after the first POST value everytime. Anyway, I apologize for the errors, and I'm at work again, so don't have the means to test anything, but please let me know what you find, and we'll resolve this issue. Thanks.
  25. All right, there are several approaches to take for this, but I'd recommend something like the following: I can't recall if Larry mentions this or not in the book, but the superglobals in PHP (POST, GET, etc.) are arrays, and can be treated as such. More specifically, when you post multiple values, they're all in the $_POST array, in the order that you submitted them in. We can use this knowledge to go through each submitted value: $iTotalHours = 0; $iTotalMin = 0; $bIsHourValue = true; // Used to alternate between hour and minute values in the POST array. $bNoErrors = true; // Flag used to indicate that there is some invalid input. foreach ($_POST as $key -> $value) { // $key represents the names of the various submitted data items (e.g., hour01, min01, hour02, etc.). if ($bIsHourValue) { // Is an hour value. if (is_int($value)) { // PHP function testing for an integer $iTotalHours .= $value; } else if (!empty($value)) { // If we get here, we know that we don't have an integer, so if there is any value at all, then it must be invalid. // If it's empty, we don't need to do anything. $bNoErrors = false; break; // No need to continue the loop if we've already found an error. Note that this is fairly rudimentary error handling. } $bIsHourValue = false; // Alternate the isHour flag. } else { // Is a minute value. if (is_int($value)) { $iTotalMin .= $value; } else if (!empty($value)) { $bNoErrors = false; break; } $bIsHourValue = true; } } if ($bNoErrors) { // If there was no invalid input, do the calculations. $iTotalMin .= 60 * $iTotalHours; // Total time in minutes $iTotalHours = floor($iTotalMin / 60); // Divide the total time by 60, and round the result down to the nearest integer for the total hours. &iTotalMin %= 60; // Kind of a funky notation, but takes the total minutes and sets it equal to the reminder of itself divided by 60. // For example 280 % 60 = 40 (minutes). } else { // Output a basic message indicating that there was some invalid input. echo 'Learn to input data correctly, bozo!'; } At this point, assuming no errors, you have the total hours and minutes stored in $iTotalHours and $iTotalMin, respectively. You can simply echo these values in the appropriate places in the form you created using notation like the following: ...value="<?php if (!empty($iTotalHours)) echo $iTotalHours; ?>">... Note that my error handling is really simple, too. It only tells the user that there was an error, but not where. A better implementation would be to have an array that stores each of the errors, and then tells the user where they made mistakes (if in multiple places). I believe this is covered in Larry's book, which is why I glossed over it. Also, it's probably best to tell the user from the beginning that only integers are valid, and that omitted values are counted as 0. Well, does that answer your question?
×
×
  • Create New...