-
Posts
15,274 -
Joined
-
Last visited
-
Days Won
432
Community Answers
-
requinix's post in Has something changed that I am not aware of? was marked as the answer
I can't tell what your other account is. What's the username or email? You can post it or PM me.
I'm being stupid. It should be obvious what the original account is.
I do see the account being locked from too many unsuccessful login attempts from 37.147.174.213, but that lock has since expired (while the site was offline). You should be good to go now.
I can't really speak to the support ticket thing... Nibble Netwrx owns PHP Freaks so that's why you got that email from them.
-
requinix's post in Cron Job API Integration was marked as the answer
You'll probably have to make some other minor changes so that this script can run from a cronjob, but
Essentially the change you need to make is to take all of that code, which works for a single page of data, and stick it inside of a loop that goes through all of the pages. Starts at page 1 (or perhaps 0), runs the code that you have now but edited to use that page number, then the loop moves to the next page and starts again until eventually there are no more pages.
-
requinix's post in Downloading selected multiple file was marked as the answer
Look at the examples and maybe try some Google searches for using PHP to download multiple files with a ZIP archive. There's a lot of information out there and it isn't too hard to find samples you can reference.
-
requinix's post in Redirect domains to a URL+domains was marked as the answer
What? Sure you do: that second server you were planning to use. The one where the domains redirect to, and where you were going to redirect from.
Take that, add a virtualhost for a catch-all domain (so any domain that doesn't match some other known site), then give it a redirect to go to destinationtarget.com/(requested domain). It's one configuration that handles the redirects for every single domain you throw at it - assuming you all want them to use the same pattern for the redirect.
Then you change the DNS for the domains you want to redirect from such that they resolve to that second server. Browser will look up "domain1.com" or "domain2.com" or "domain3.com", all of them resolve to that second server, browser asks the server for the webpage at that domain, server responds with a redirect to destinationtarget.com.
In fact, that "second server" can simply be whatever server hosts destinationtarget.com. I mean, this redirect business is very lightweight. It's not going to add any noticeable load to anything.
So there. You have a server that hosts destinationtarget.com. Give it a new configuration for a catch-all domain that does the redirect I said, then update the domains to point to that server. And if you decide to handle some redirected domains differently then you don't have to update DNS for anything.
-
requinix's post in Executing time consuming functions in an EventLoop was marked as the answer
In this case it's about using the generator as a coroutine. Generators can suspend execution and return to some caller, then that caller can have the generator resume.
The use case here is that your doWork becomes a generator, and it yields (suspends) every email or so. When it suspends ReactPHP would have a chance to execute as it needs (ie, run through the event loop), and when it's ready it has doWork continue. Rinse and repeat until hair is sufficiently washed.
https://3v4l.org/qlXCj
As long as doWork is running, ReactPHP's loop will not execute. Because it's not truly asynchronous - it just looks that way.
The basic problem you have is that doWork takes too long. The basic solution is to split the work according to whatever is not "too long". Since your metric is time, I suggested running work until some suitable amount of time has passed. Like one second.
doWork is probably self-contained, right? It determines what work needs to be done and does it. You'd refactor that so that determining the work to be done happens before doWork, then doWork goes through that set until either too much time passes or it runs out of stuff.
class DoesWork { private $work = []; public function __construct() { $this->work = ???; } public function addWork($item) { $this->work[] = $item; } public function doWork() { // exact code varies, but basically... $now = microtime(true); $end = $now + 1; while ($this->work && $now < $end) { $item = array_shift($this->work); // whatever $now = microtime(true); } } public function hasWork() { return !empty($this->work); } } // setup $worker = new DoesWork(); // inside the event loop, if ($worker->hasWork()) { $worker->doWork(); } If you look closely, you'll notice the code structure looks similar to what was in that 3v4l link...
-
requinix's post in html table with data from 2 sources was marked as the answer
That is the correct answer. Why do you not want to do it?
-
requinix's post in For Each Loop on Common Value in DB was marked as the answer
First you'll have to sort the results by the type. Because otherwise stuff will bounce between one type and another. Then you should probably sort by a secondary another field - I suggest the item name, since that's what the user sees.
Really, most of the time you're running a query for data that gets displayed to a user, you'll want to sort by something. Even if you don't need anything grouped or sorted alphabetically, picking and sorting by a unique column (like an ID) guarantees that you'll get the same data in the same order every time.
Anyway, when you have that sorting, your loop will need to know when the type changes. That means remembering what the previous one was.
Get yourself a variable for it. Start it off empty. Inside the loop, if the current type doesn't match the previous type (and the first row definitely will not) then that means you need to start a new group. If there was a previous type then there was a previous <optgroup> already going for it and you'll need to close that out, then either way you start the new <optgroup>.
Finally, you need to think about that last group. When the loop finishes you'll have been working on one group but the <optgroup> will still be open. So close that one out too.
-
requinix's post in Can't escape this quote was marked as the answer
HTML doesn't do backslashes for escaping characters. You have to use an entity for it.
'label' => __( 'Title -
requinix's post in Can I access $_GET['id'] if the page is html? How does this code do it!? was marked as the answer
"Reliable"? If the code is solid then sure, it's reliable.
Method I use:
var id = (document.location.search.match(/[?&]id=([^&]+)/) || [])[1] || ""; And if you don't have access to PHP then it doesn't really make sense to ask this in the PHP forum, does it?
-
requinix's post in Controller and Model Paradigm was marked as the answer
The model represents the data, so it's often a database model but it can also be a so-called "business" model: represents data as the application needs to use it, and would use simplistic database models to get the data it needs so it isn't connected directly to the database. But it's not too common to have both.
However a database model isn't limited to strictly reading from and saving to the database. Some people do that, personally I don't. Really the model is the central place for dealing with data - yes, working with the database is a key component of that, but it can also involve testing the data (eg, verifying login credentials) or manipulating the data (eg, changing a user's password) or many other actions.
So yes, the model should not be able to log a user into the application because that is beyond the scope of managing its data. It should have a method to test login credentials, because that is directly related to its control of the data, but the controller is what would do the actual logging-in with the session and cookies and whatnot.
-
requinix's post in Updating quantity if item is already in cart was marked as the answer
Oookay, so we're going further back into the basics of PHP than I was expecting.
0 == false. If you try to say "if (0)" then it will always execute the else branch. So if you say "if ($itemExists)" and that variable is 0 then it will also always execute the else branch.
Since your function can return 0 or false, and you want to know if the value is not literally false, then you need a literal - aka "strict" - comparison.
if ($itemExists !== false) {On top of that is your latest post.
One equals sign is assignment. Two equals signs are a loose comparison, where values are compared in spirit without regard to their exact type. Three equals signs are a strict comparison, where values must be the same type for them to possibly be the same.
That if statement you have will assign the value of "0", which is a string by the way and not a number, to the variable $itemExists and then try to decide if that same value is loosely true. Which it is not. And so the else branch will execute. Again.
If you fixed your condition to use two equals signs for a loose comparison then it would work for items in the first position in the cart, except now every other case would be thoroughly messed up.
If you fixed your condition to use three equals signs then we'd be back to the original situation of the else executing every time, but this time it would be because the function never returns the string "0".
-
requinix's post in Obtain PHP array definition script based on JSON was marked as the answer
Try var_export.
-
requinix's post in securing file uploads from a form was marked as the answer
Linux allows every character.
Store the original name as metadata with the upload. Actually name it on your server as something completely different.
See above.
If you want.
The type is not safe to use. Don't even look at it. To detect type yourself, the file extension is most important and MIME identification can also help.
Of course. -
requinix's post in post form data (textarea) as file was marked as the answer
The default is actually application/x-www-form-urlencoded. That's the normal key=value&key=value&... syntax you should recognize. multipart/form-data, mostly used for file uploads, is a far less compact format but better suited for lots of content.
Doing this as a fake file upload really sucks. Use the Blob and FormData classes.
var fd = new FormData(); var blob = new Blob([/* textarea contents */], { type: "text/plain" }); fd.append("nta_big", blob); var xhr = new XMLHttpRequest(); xhr.open("POST", /* url */, true); xhr.send(fd);Untested but should be close.
With jQuery you might be able to use a Blob with its AJAX methods, as in
{ "nta_big": blob }as part of the sent data. -
requinix's post in Ajax, multiple form elements was marked as the answer
onchange="showdata(this.value)"All that does is send the value of whatever element to the function. Not only will the function not know which element is calling it, the function needs all three values at the same time.
There are many ways to do this - personally I would pass the function not the values but the form itself. The function can then get the values from the form.
onchange="showdata(this.form)" <script> function showdata(form) { var project = form.fm_project.value, module = form.fm_module.value, stage = form.fm_stage.value;Then update the rest of the function to use those variable names. Because they're much more descriptive than stuff like "stra". -
requinix's post in Preventing scientific notation in a string was marked as the answer
Use sprintf to get a different string representation for $const1.
$const1string = rtrim(sprintf("%.20f", $const1), "0"); -
requinix's post in getting Undefined Index error. was marked as the answer
Presumably the error is pointing to
$user_id = $_SESSION['user_id']; //We'll get the user_id from the session"Undefined index" means there is no user_id in $_SESSION. Either the user is logged out or your code is looking at the wrong array index. -
requinix's post in Link With Anchor Causes Tab-Formatting JS To Fail was marked as the answer
Clicking the link puts the #anchor in the URL. Refreshing the page will keep that URL but reset the page to the state it was delivered by the server.
Either
a) Clicking the link should not update the URL. Refreshing will go back to the original state.
b) Update the URL, and include some Javascript that runs on page load to check if there is an #anchor and update the page as needed.
-
requinix's post in Adjust Div Width To Content, And Center It Horizontally On Top Of Another Div was marked as the answer
DIVs are block elements by default and will expand their widths to their containers automatically. Give the text centering to its container and make the DIV be either inline or inline-block.
-
requinix's post in Add descriptor text to output in certain places was marked as the answer
Regex would be an easy way to handle this.
$line = preg_replace('#^=+<br/>$#', '<hr>', $line); $line = preg_replace('#^(\d\d:\d\d) (.*?) (\d{10})<br/>$#', 'Time: $1 - Description: $2 - ID Number: $3', $line); -
requinix's post in iimagettfbbox and font file on hosted server was marked as the answer
Find out what distro it is then Google the default font path. Might be in /usr/share. If there are any installed at all.
Or just ignore all that and upload the font you want. That's not bad or anything.
-
requinix's post in Read JSON instead of XML, need some HELP was marked as the answer
1. Fix those three lines near the top so they find the right files.
2. Replace the three stupid pairs of simplexml_load_file/json_encode lines with one that uses file_get_contents to load the JSON data into $json. Keep the json_decode lines.
3. There is no third step.
That's assuming the JSON structure mirrors the XML structure. If it does not then you have to adjust the code accordingly.