Jump to content

requinix

Administrators
  • Posts

    15,065
  • Joined

  • Last visited

  • Days Won

    414

Everything posted by requinix

  1. First, use an array for the decoded JSON. Having to use that {""} syntax is unfortunate and awkward. $zooelements = json_decode($elements, true); $name = $zooelements["0ad767cantbebotheredtocopypaste"][0]["value"];Second, are you actually using a @ when you call mysql_query? That will hide any errors that may happen. Hiding errors is very bad. Don't use @. Is your question about how to update $zooelements? Surely the answer is just $zooelements["0adblahblahblah"][0]["value"] = "NameEFGH";
  2. How can you make the checkbox be checked for the user? Put a "checked" in the HTML if you want it checked. There's something funky happening with your code in that form, so I'm going to go out on a limb and fix it. />The value doesn't really matter, and instead it uses the value in $result to show the "checked" or not.
  3. "Get function"? And for two, your form is using POST and not GET. Those textboxes and checkboxes have names, right? Like "water-purveyor" and "v1-na". So those will be in $_POST - though checkboxes only if the user checked them, so you need isset() for those. $water_purveyor = $_POST["water-purveyor"]; $v1_na = (isset($_POST["v1-na"]) ? $_POST["v1-na"] : /* default value? */ null);
  4. That code? You're throwing an exception only to catch it on the next line. That's pointless. You might as well just public function indexAction(Request $request) { if(!is_object($request)) { $this->logger = $this->get('logger'); $this->logger->info("You have the following error: { $error }", array("error" => "Param must be an object")); } }By the way, inconsistent indentation drives me crazy. I'm trying not to go into a treatise about how exceptions are supposed to work (too much to say that has been said elsewhere on the internet), with mixed success, but basically you use them if there is something unexpected that happens and your code is not able or supposed to handle it. Look around to see what people say about how to use exceptions - even if it applies to other languages (notably Java and C#). First of all, in your situation $request is going to be an object because you used the typehint. Not that it will prevent someone from calling the method with a non-Request or non-object, but if they did then there will be an error about it and your logger will see that (assuming your logger is set up for PHP errors, which it should be). If you still want exceptions here then I'd use something more like public function indexAction(Request $request) { if (!($request instanceof Request)) { throw new InvalidArgumentException('$request must be a Request object'); }The main point here is that indexAction cannot recover from this problem. There is nothing it can do. The Exception "forces" someone in the call stack to handle an InvalidArgumentException - or worst case nobody does and PHP fatals. Also note the InvalidArgumentException instead of just Exception. Pretty much every sort of exception you'd want to throw really, really needs to be a subclass. Throwing Exception everywhere severely limits what you can do when catching them: (a) You would have to resort to crazy hacks to figure out what kind of problem it represents, but more critically (b) You'll catch and handle types of circumstances your code isn't prepared for. If you set up a try/catch because you know that (eg) the function you're calling can error because a file doesn't exist, you'll get that, but you'll get every other exception too. And if you discard the exception because the missing file isn't a big problem, you're now discarding every exception. The InvalidArgumentException class specifically indicates that there was a problem with an argument to a function. I don't ever catch this class[1] because it represents a fundamental problem with the code itself - not merely some sort of unexpected situation or unusual circumstance. Continuing that train of thought, only catch the exceptions you want to handle. If something can throw an exception but your code doesn't know what to do in that case, don't catch it there. Instead, go backwards up the call stack until you find a place where it makes sense. Common example: set up a try/catch just before you try to "execute" the code for a particular page on your site, then in case of an exception you can (log it and) show an error message to the user. So public function indexAction(Request $request) { // do whatever public static function /* Controller:: */ invokeAction($controllerClass, $action) { $controller = new $controllerClass(); $method = $action . "Action"; $request = Request::current(); // no try/catch because there isn't any type of exception this method is prepared to handle $controller->$method($request); } try { Controller::invokeAction($controllerClass, $action); } // one type of exception that you want to use special handling for catch (SomeParticularTypeOfException1 $e1) { // ... } // another type of exception with special handling catch (SomeOtherTypeOfException2 $e2) { // ... } // catch every other exception so we can present an error message to the user catch (Exception $e) { ErrorController::showErrorPage(500, $e); // public static function showErrorPage($code, Exception $error = null) }[1] Unless I'm working with a system that must not fail[2], I do this. Which is to say I have a logger set up for uncaught Exceptions, log their presence, then let PHP die.[2] Well, that or there's a chokepoint in the code where I want to wrap any exceptions caught inside another exception. In which case I use the "$previous" parameter to Exception, like try { callSomeFunction(); } catch (Exception $e) { throw new BadMethodCallException("callSomeFunction failed: " . $e->getMessage(), 0, $e); }Side note: make sure your logger can handle nested exceptions.
  5. Variables defined outside of functions are not available inside of functions. Pass what you need as arguments, like function output($connection, $sql) {
  6. That was addressed to me, right? I'm not sure what you mean. I'm talking about four functions that look like /** * @param string $filename * @param array $variables * @return mixed */ function include_once_clean() { if (func_num_args() > 1) { extract(func_get_arg(1)); } return include_once(func_get_arg(0)); }and include_once_clean("/path/to/file"); include_once_clean("/path/to/file", ["var" => "value"]); What?
  7. It's not negligible but if you need a wrapper for something like logging then it's probably worth it. I have my own wrappers which I use to make sure files are run in a new scope - no inherited variables and whatnot.
  8. If you entered it into the website then we have it. Not rocket science.
  9. Adding code to a system increases the likelihood of there being bugs? You don't say! This is IPB 3. Sorry that the theme looks too nice.
  10. Posting something so I can mark this as solved.
  11. Is there a problem with prompting the user for which site to use? For example, what UPS and FexEx do. There's a pretty substantial advantage to prompting in that the user controls which site they use. With that said, defaulting and then offering a way to switch shouldn't be too bad. Otherwise what you're looking for is generically called geolocation.
  12. elfynity, your account is suspended and will no longer work. erilidon, sorry you don't like the policies you agreed to, but fortunately they will no longer be an issue for you as I've done the same for your account.
  13. We don't like deleting accounts, but I can "suspend" you so your account is no longer active.
  14. Empty for me too. I don't know why Opera is downloading it. There's nothing indicating it should be downloaded. Can you see what the request and response headers are for it?
  15. I'm going to start suspending accounts for people who ask for it. mrbraq, rpoelking, darkcarnival: I'll suspend yours tomorrow (to give you time to see this post).
  16. If you see them again, try to get the full URL to the ad. We can then submit it to our advertisers who can do something from there.
  17. You're talking about ads being shown here, right? Can you provide more details about which ad? Like the full path to it? All we can do is report the bad ad. In the meantime I suggest doing something to block the downloads *coughoruseadblock*.
  18. 1. All of these classes are shapes, right? And they all have the same set of methods, like getArea and getPerimeter (notice the spelling) and scale, right? Making each class completely distinct does not fit into the idea of "all of these are shapes", and simply using methods with matching names does not work for "the same set of methods". Look into inheritance and polymorphism. abstract class Shape { public abstract function getArea(); public abstract function getPerimeter(); public abstract function scale($scale); }2a. The scaling can (should) be done merely with a number. 1.0 is the same size, >1.0 is larger, 2b. Writing up or down tells PHP to look for a constant with that name. If you want a string then use quotes. 3. "a" and "b" and "w" and "l" are not appropriate variable names for things that matter, like the length of a side of a square or of the width of a rectangle. Use good names like "width". 4. A method called "get*" should get a value. As in return something. It should never output anything. And they shouldn't try to format numbers either. 5. Put your classes in separate files. You should learn about autoloading classes at this point, but requireing each file is okay for such a small assignment.
  19. Nope. Why would you want to? If they're using Chrome then they made a deliberate choice not to use IE?
×
×
  • 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.