-
Posts
15,229 -
Joined
-
Last visited
-
Days Won
427
Everything posted by requinix
-
$modelName[strlen($modelName) - 1] = "";Don't do that. You cannot use [] to remove characters - only replace. What the code above actually does is replace with a \0. I don't know what character you're trying to remove but I suggest looking at rtrim. [edit] The character is 's'. Which should be obvious given the code and output above. Eh.
-
a) Use multiple different Command objects, where each represents a distinct "command" and thus behaves differently, and provide a method in Client to change the loop frequency. public function changeFrequency($interval) { // ... } // switch(type of command to execute) { // case Change Frequency: // case Something Which Entails Changing Frequency: $command = CommandThatChangesFrequency($this); // ... // } $command->execute(); class CommandThatChangesFrequency implements ICommand { public function __construct(Client $client) { // ... } public function execute() { $command->changeFrequency($whatever); } }b) Same, except you keep the unified "Commands" thing and have to pass it every bit of data that could possibly be relevant. class Commands { public function __construct($foo, $bar, Client $client, $baz) { // ... } public function execute() { // switch (whatever the command is supposed to do) { // case command needs to change frequency: $this->client->changeFrequency($whatever); // ... // } } }c) Something else. Whatever you do, Client has a public method to change the polling frequency. Then you pass Client to whatever needs it. So just typical object-oriented programming.
-
.serialize is a function. You have to actually, you know, call it. And now your AJAX code and your PHP code don't agree. Are you working with JSON or HTML? Pick one.
-
Sounds like a Command. $obj1 doesn't need to read or write anything related to $obj2. Not directly. $obj1 interprets the response, constructs a "command" that contains the data necessary to perform whatever action is appropriate for the response, then passes it to $obj2 which executes the command. Or you say that $obj2 is itself the command and it gets executed directly (ie, $obj2->execute), cutting out the middleman. What happens from there depends on the command - maybe there's a return value, maybe not, whatever. Still too abstract though, and there is no single answer at this level. How about explaining why $obj1 needs to "modify" $obj2? And what is $obj2? What kind of range of possibilities of responses and actions is there, technically speaking?
-
1. You need to send the form data to AJAX2.php. data: $(this).serialize(),2. Your code is confused about what variable to use in the success callback. success: function(result) { alert(data); },3. The AJAX page needs to be consistent about whether it returns HTML or JSON. <?php header('Content-Type: application/json'); $variable = $_POST['var']; echo json_encode($variable); ?> <?php header('Content-Type: text/html; charset=utf-8'); $variable = $_POST['var']; echo '<p>' . htmlspecialchars($variable) . '</p>'; // for example ?>(your AJAX code expects JSON so obviously you need to use the former)
-
Prepared statements are the better way. Are you asking for the sake of hearing that answer repeated once more, or are you unsure how to deal with the variable number of placeholders?
-
Reading values from global variables is fine. Writing values is where the complications are, because it's not always easy to tell where a value comes from. If you only write in a small number of places (ideally one) then there's no problem. Put another way, don't just repeat what you hear without knowing why.
-
And a fourth: What's your question?
-
summary content before search keyword in text
requinix replied to artablog's topic in PHP Coding Help
You'll have to be more precise: you know what the output should be but why is it? What decisions did you make to arrive at that output? If you can explain what you did then you can express it as code. -
Re: large integers The largest possible result would be thirteen 9s = 2,541,865,828,329, which is beyond the limits of 32-bit PHP but still safe with 64-bit PHP, so as long as you're using a 64-bit build of PHP (eg, PHP 7) then you're okay. Otherwise it'll overflow to floating-point which uses approximations and could thus cause problems for you. Re: the loop To clarify, Jacques is saying that all you have to care about is the first (oldest) and last (newest) digits. Say you have four digits 7 1 7 6 -> 294: when you get the next number 5 then you can take 294/7 = 42 to factor out the first digit then 42*5 = 210 to factor in the new digit, rather than recalculating the full 1*7*6*5. Oh, and a quick optimization tip: if you encounter a 0 then you can skip forward thirteen places because all the multiplications during then will equal zero.
-
What you want to do is despicable. Are you saying the onbeforeunload works but the window.open does not? If it's not a problem with the code itself (eg, "URL" undefined or invalid) then it's likely that the browser is deliberately preventing that from happening.
-
Just [0-9] (or \d for short) will only match a single digit. If you want one or more then use +. Not sure how the "any number or exactly 900000" is supposed to work. Isn't 900000 a number too?
-
What you're describing doesn't make sense. You say parallel. How? AJAX? And is there an actual problem you're trying to solve or is this all just conjecture?
-
Well you removed all that code so it won't do that anymore. Why did you do that? You have the table showing if the form was submitted but it needs to show only if the form was submitted and there were results; then you show a message if the form was submitted and there were not results. As in if form was submitted { get results if there are results { show table } else { show message } }
-
...Probably? What second file? What primary page? What result? Probably. It's quite possible for one to affect the other - like if there are file locks involved, which can block one process (page) from executing while another has a lock. Yes.
-
That code is running because of the if form was submitted {right? But the variables will only be defined if there were results. So that "form was submitted" condition should be modified so that the form has to have been submitted and there must have been results.
-
Well, if you want to work with PHP then you're going to have to learn some. I don't know about the quality but I'm sure there are plenty of videos on YouTube to learn from. Right now your code looks like <?php if form was submitted { query if results { gather data into variables create $output with data } else { create $output with error message } } ?> show form, table, and $outputIt needs to look more like <?php if form was submitted { query if results { gather data into variables create $output with data } else { create $output with error message } } ?> show form <?php if form was submitted { ?> show table and/or $output <?php } ?>
-
How much of the code there now did you write? Do you know any PHP? Have you used things like if statements?
-
No. We're not going to fix it for you. Try making the changes I said yourself. If you have problems, post the code you have and a description of what's happening.
-
It looks like it does show that "no data found" message: at the bottom of the form after the table. Problem is you aren't deciding whether to show the table or to show the message depending on the results. You do everything and hope that all the variables have values. You need something like that "if num_rows > 0" you had earlier but covering the rest of the code and output too.
-
Sending array within array in PHP cURL CURLOPT_POSTFIELDS
requinix replied to isfak's topic in PHP Coding Help
How does the service expect to receive multiple values of selectedFields? If it's not PHP style as "selectedFields[]=" then it's probably the more traditional style of using multiple "selectedFields=", which you'd have to build by-hand as PHP doesn't have a native function to do it for you. curl_setopt($ch, CURLOPT_POSTFIELDS, "task=download&selectedFields=1&selectedFields=2&selectedFields=3"); -
Programming style for multidimensional associative arrays
requinix replied to cyberRobot's topic in PHP Coding Help
I took a quick look through some a personal project to see if I could find any examples that match the sort of thing you're dealing with. Didn't quite find what I was hoping for, but here's some lines I found: return call_user_func(self::$instance->data["crypt"]->data[$decryptor], substr($value, $decryptorlen + 2)); info("kernel.cache", ["Cache provider '%s' already registered as %s", $provider, get_class(self::$providers[$provider]["object"])]); self::$routers[$name]["object"] = instance(self::$routers[$name]["class"], [$name, self::$routers[$name]["config"]]);It also helps to use an editor with syntax highlighting - makes it easy (easier) to spot arrays when the "array" or []s are colored differently than variables and strings. -
Programming style for multidimensional associative arrays
requinix replied to cyberRobot's topic in PHP Coding Help
I wouldn't. It looks fine to me: even at a glance I see three distinct array keys being used at the top level, and the first and third only need another half-second to see they're using their own array keys.