KevinM1
Moderators-
Posts
5,222 -
Joined
-
Last visited
-
Days Won
26
Everything posted by KevinM1
-
Are you testing within Dreamweaver? I've heard that the previews it shows aren't entirely accurate. Be sure to test in the big four (IE, Firefox, Chrome, Opera) at the very least.
-
What you're trying won't work because JavaScript and PHP aren't the same thing. PHP is finished processing by the time JavaScript enters the scene. Therefore, you can't plug a PHP function into a JavaScript event (at least, not without doing things that are clearly beyond your present ability). To further clarify, your PHP function will fire twice. In both cases, if you're lucky, the proper return value will then be assigned to the JavaScript onclick event (I actually think it will be a null result, since you don't assign your return value to anything in PHP, but it doesn't really matter). onclick=1 and onclick=2 doesn't mean anything. It's gibberish, and won't magically set the values you want on the page. And this isn't even touching on you trying to use 'global' (in short, don't do it. it's a crutch, and a bad one at that. learn to write functions properly) or the syntax errors. What you should do is get a couple of good beginner PHP and JavaScript books, because you have some misunderstandings at the fundamental level which are too broad and deep to properly address on a forum.
-
Much better. And, I'm happy to see that the main layout doesn't use tables. Yay! A couple of things stand out like a sore thumb: 1. The spacing for the elements on the 'About' page is a bit messed up. Your bio box is way too far to the right. 2. Spelling error at the top. Firfox should be Firefox. Some other suggestions: You should probably have a few extra pixels of margin or padding to the top of the site. It feels a bit too close to the top of the browsing window. Nothing drastic, just literally a few (3-5) pixels should do the trick. You have a couple of text-only links on your links page that are hard to read, as they share the same color as your footer font. The spacing is also a bit off, as the other links are large images, and then there's a lot of empty space for the text-only links below them. Feels kinda empty down there. RE: Fix #1 - looking at some of the other pages, it looks like there's some more spacing issues. I never normally browse with a maximized browser window, but I have it set to a decent width so I never have to scroll horizontally to view content. I'm getting a horizontal scroll bar on both 'About' and 'Links'. Definitely need to fix that. A cursory look at your code shows me that it may have to do with the <div id="gallery"> element - you don't have a closing tag (</div>) for it. Still, much better. And, every time I look at your site, it reminds me I went into the wrong field. :'(
-
Checking if Variable contains a number at line 1
KevinM1 replied to Modernvox's topic in PHP Coding Help
I don't believe php tags are case insensitive. So, use <?php ?> and NOT <?PHP ?> Now, to the business at hand: What, precisely, is $itemDescription? Is it a string? From the way you describe it, you'll need to obtain the number (by using explode or regEx), then do your math from there. That said, there's a wide margin of error doing it that way since you're not guaranteed that the format you're expecting the data to come in as will be the way the user typed it in. Example: you're expecting "1 table" as incoming data, but the user enters in something like "1 table 2 other things" or "tables: 1" or any other combination (since users can be dumb and chaotic). If I were you, I'd have two fields here. A small text field to obtain the number, and a corresponding select element with all of the possible description options. This would rein in the incoming data, and make it easier to validate. -
You can't use an object member like that unless it's an array itself. You can simply use the title as a function parameter: function someFunction($arg) { // do something } someFunction($ct->title);
-
The '->' is the object-to-member operator, with a 'member' being either a field in the object (object data like a string, integer, etc.) or method of the object (function that acts on the object itself). So, for something like ct->title, 'title' is a field of the ct object. In the case of this current_theme_info() function, it's assigning all of these array values to particular fields within the ct object. After this function is run, if you wanted to output the title, all you'd need to do would be: echo ct->title; Hope this helps.
-
Try something along these lines (note: I have not tested this, so no guarantee it will work right off): <?php define ("MAX_SIZE", "100"); function getExtension($str) { $i = strrpos($str, "."); if(!$i) { return ""; } else { $l = strlen($str) - $i; $ext = substr($str, $i + 1, $l); return $ext; } } session_start(); $user_id = $_SESSION['user_id']; $errors = false; if(isset($_POST['Submit'])) { $image = stripslashes($_FILES['image']['name']); if($image) { $size = filesize($_FILES['image']['tmp_name']); $extension = strtolower(getExtension($image)); if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors = true; } elseif($size > (MAX_SIZE * 1024)) { echo '<h1>You have exceeded the size limit!</h1>'; $errors = true; } else { $image_name = $user_id . '.' . $extension; $newname = "romimages/" . $image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if(!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors = true; } } } if(!$errors) { ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form class="dark" action="" method="post"> <ol> <li> <fieldset> <legend>Rom Logo Uploaded!</legend> <p> </p> <ol> <li> <label for="">Your image has been uploaded, you can now close this window</label> </li> </ol> </fieldset> </li> </ol> <br> <input type="submit" value="Close" name="submit" onClick="return parent.parent.GB_hide()" > </form> <?php } } ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form method="post" enctype="multipart/form-data" action="" class="dark"> <li> <input type="file" name="image" width="45px"> </li> <br> <br> <li> <input name="Submit" type="submit" value="Upload image"> </form> In general, you really just need to tidy your code. There's an overall feeling of "I'm gonna throw this code at the wall and see what sticks" here. Go through it step-by-step and don't make it more complicated than it needs to be. Also, I strongly suggest writing semantically meaningful markup. Your error condition HTML can be made much simpler and much smaller. You shouldn't create a form with no action just to create a button. HTML has a generic <button> element that's used for this exact problem. Similarly, lists shouldn't be used just because. A list denotes, well, a list of things. They're often used with navigation because navigation is often just a list of links. Just use the right tools for the job. A couple of divs with proper styling is a much more efficient way of doing what you're trying to do.
-
Where does $extension come from? You use it on line 4 before defining it. Also, be sure to check your braces. I don't know if this is the way you normally indent your code, or if pasting it here messed up the structure, but it's hard to read, especially when it comes to lining up braces.
-
I wouldn't work on it unless you were guaranteed either: 1. That the time you spent working while on your vacation would not be deducted from your vacation time. Instead, it would be treated as if you were in the office for that/those day(s). 2. A small bonus for the work done on what is, essentially, ultra overtime. Of course, I'm the kind of guy who can't stand that kind of stupidity, so I'd have some choice words for your boss, as well. Then again, tact has never been my strong suit.
-
The only time you need to use {} in double-quoted strings is when you want to print an array variable or when you need to pluralize something: $beer = 'Coors Light'; echo "I had five $beers"; // <-- won't work echo "I had five {$beer}s"; // <-- works For variable vs. constant, it comes down to assignment. Will you be capturing/generating a value? If so, use a variable. Why? Variables can be assigned to. Constants, once defined, are exactly that - constant. You can't overwrite one with another value. The biggest, best habit you can get into is properly structuring your files and applications. While PHP gives one the ability to switch between code and markup on the fly, it's generally not a good idea to do so. Think about it like this: How many times have you written or read code that has PHP echoing out HTML, which has JavaScript inside of that written in an inline (meaning within an element: <a href onclick="blah"...>) manner? Or PHP if-else statements, or, even better, while-loops that exit PHP to print lines of HTML right there? And does any of that sound like it would be fun to debug, edit, maintain, or extend? So, first, the consensus way to structure things is to do ALL of your PHP processing - form handling, db interactions, generating/building results from all of that activity, and any header redirects - first, then output the results of that processing. The classic sticky form (google it, if you don't know it) is the textbook example, but ALL PHP should be written like that - from one page contact forms to apps as large as Facebook. Process, then output. Two, some other terms: Separation of Concerns: Your systems are best served when they are broken down into layers, and each layer has a well-defined job to do. Your HTML should only have PHP statements that output data. Logic/processing should be done in the PHP script proper. Similarly, CSS should be linked from external files, and JavaScript should be written in an unobtrusive (read: no JavaScript written within HTML elements other than <script></script>) manner. This keeps all of your components - back end logic, client side logic, and display instructions - in their own self-contained areas, making debugging, editing, maintaining, and extending your projects a hell of a lot easier. DRY: Don't Repeat Yourself. Find yourself writing the same code over and over again within a file? That's a sign that you're doing it wrong. Refactor your code. See if you can reduce it by making the repeated part a function, or see if you can write it in a clearer, more concise manner. Well written code reads like a book. One other thing, not related to coding: if you're trying to research a problem, the very first place you should go is the PHP manual. Chances are, there's a built-in function that does what you want to do. So, save this link and use it often: http://www.php.net/quickref.php I'm sure others have more useful tips as well.
-
Return doesn't give the value back to the function. In my example, $total doesn't go back to the function calculateTotal(). It goes to the code that invoked calculateTotal(). This value is stored by the variable $myTotal in the global scope. If I didn't put the assignment statement there, the value would still be returned, but would essentially disappear as it wasn't stored or used in any way. Return always leaves a function with a value when it's called. If a value or variable is specified (again, like $total in my example), that's the value/variable that leaves the function. If no value or variable is given, NULL is sent by default. That's what happens in example 3.
-
Just to clarify, the -> has nothing to do with return at all. It's an operator used in OOP code.
-
Well, like the manual states, it's primarily used when you want to return a generated value from a function back to the controlling scope (e.g., the code that invoked the function) to be used in its logic. Canned example: function calculateTotal($arrayOfValues) { $total = 0; foreach($arrayOfValues as $value) { $total += $value; } return $total; } $somePrices = array(2.99, 19.99, 14.99); $myTotal = calculateTotal($somePrices); echo "Total cost is: $myTotal";
-
Try setInterval()
-
I'm not familiar with 3rd party image gallery systems. My sites have never been focused on art. I'm sure someone else would be able to help there. That said, you should take a look at Lightbox as a way to show individual photos when a thumbnail is clicked. It's smoother than using an iframe. Also, and I mean this with all sincerity, kudos for taking the criticism in the way in which it was intended. A lot of people come here merely looking for a pat on the back and get angry when others don't simply shower them with praise. So, keep at it and keep looking to improve.
-
This, right here. Zandstra's book is a great introduction to the subject. Regarding your registration class, you have some questions to answer: 1. Do you need an instance of this class, or can it be static (no constructor, with all functionality handled by static methods)? 2. What will the registration class return when someone is registered, if anything? IMO, it makes sense to return a User object whose 'loggedIn' field is set to true so you can pass it around via sessions, thereby turning your registration class into a factory. Just a thought. 3. What else needs to happen during the registration process? Extra logging? Error handling? You may be well served by utilizing the Observer pattern here.
-
I guess I just don't see why you'd want to only keep 20 records in your database. For one, worrying about db size to that minute detail strikes me as a bit OCD. Databases are supposed to hold data, and a mere 20 records is a very small sample size. Also, if your HDD is really that tight, you have other problems to worry about. Two, wouldn't the sales numbers of the least effective people be just as important as the most effective? And wouldn't it make sense to track these numbers on a monthly basis? To me, your request indicates a llack of time spent planning the database design. Not trying to be overly harsh, but I think you're painting yourself in a corner, where it will be hard to modify and expand your project because you're locked into an inflexible db design. Just something to think about, IMO.
-
Also, you really, really, really need to separate your HTML from your PHP. Don't echo an entire HTML document. Stuff those strings into files you can include. It will make debugging much easier as you won't have to dig through miles of non-PHP code, let alone having to worry if the long strings are quoted properly.
-
Remember what I said about $(document).ready()? You've gotta use it here: <!doctype html> <html lang="en-us"> <head> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> // function DEFINITIONS go here (things like fill(), fillStudent(), etc.) $(document).ready(function(){ $('#searchit').click(function(){ var studentInfo = $('#sName').val(); fillStudent(studentInfo); }); }); </script> </head> <body> </body> </html> Why do you need to use $(document).ready()? It's due to the one problem that all beginning JavaScript developers run into: runtime errors. JavaScript is fast. It attempts to obtain HTML element references before the page is fully loaded. This causes errors, as you can't attach, say, a click event to a non-existent element. This, in turn, causes scripts to break and newbie developers to hate JavaScript because it's "temperamental" and "doesn't work right." For JavaScript to have a chance to work properly, you need to wait for the HTML document to fully load before having JS attempt to manipulate it. There are several ways to do this: using the window.onload() event, using a framework's ready() function (like jQuery's $(document).ready()), or by simply putting the JavaScript that actually executes logic (calls functions, manipulates elements, etc.) at the bottom of your code (I like putting it after the closing </body> tag but before the closing </html> tag). It doesn't really matter how you do it. Just be aware that it's a first necessary step when writing any JavaScript. --- In general, it's considered bad practice to write inline code (your original button code, for instance). The reason being it marries your logic to your presentation, making it hard to debug, edit, maintain, and build on both. I strongly suggest writing in an unobtrusive (read: no JavaScript within non-script tags in your HTML) style. It will make your life so much easier in the long run. Just a suggestion.
-
ORDER BY sales DESC LIMIT 20
-
There's a difference between '#sName' and sName. Your onclick won't automatically connect the two. Since you're using jQuery, why are you even bothering with inline JavaScript function calls? Give your button an id and use jQuery to handle the event. $('#button').click(function(){ var studentInfo = $('#sName').val(); fillStudents(studentInfo); }); Don't fight against the flow of the tools given. If you're using jQuery, try to code in a way that uses it to your advantage. Also, be sure to use $(document).ready() where necessary.
-
Is it intentionally retro?
-
Also, we have a stickied topic for this: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html
-
I don't think any of us have an issue with the members that say "My boss just dumped this on my lap. I'm a designer, not a developer, and don't even know where to start." That's an understandable reason for their ignorance, and a statement like that indicates to us that we need to treat the issue with delicacy. In fact, I like helping those people because, if I do it well, I may foster an interest in them learning more PHP down the road. The OP in the thread you linked clearly has some PHP experience, since he's familiar with the built-in postgreSQL functions. He can't use the "I'm a complete newbie" excuse. I don't think it's unreasonable for fenway, or anyone else, to question if he looked at the manual, especially since the answer to his question is right there and easy to find via Google. The first place to look, regardless of the problem, is always the language's documentation. This is problem solving 101. We're here to help, not be a substitute brain.
-
Eh, a Registration class could be useful if you need to do things above and beyond simply obtaining form info and stuffing it in a database. Things like logging, counting registrations, and creating end user objects that interact with the site. To the OP: you should really sit down and plan your site. Adding a class 'just because' isn't beneficial, and isn't really a good way to learn as you'll simply wind up writing procedural code wrapped in a class. The power of OOP comes from objects interacting with one another.