-
Posts
15,227 -
Joined
-
Last visited
-
Days Won
427
Community Answers
-
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.
-
requinix's post in small tweak to rank an array was marked as the answer
Without thinking too hard about it,
sort($ranking);that is the only place in the code that does any kind of sorting, so to reverse the results you would reverse the sorting. -
requinix's post in Multiple databases within Instance vs One global database was marked as the answer
That's short-sighted. It doesn't matter where data lives as long as the application keeps everything separate, and this only "helps" administrative stuff if you have to work within the database directly (SQL queries and such). But multiple databases like this is almost always a bad idea in the long run.
One database is how it should be.
Not really. What matters is how much data there is and how it's organized in the tables - multiple databases won't cause a performance problem simply because there are more than one.
That is actually an appropriate concern. It doesn't matter that the data is distributed (in fact that generally makes things more awkward to work with) but having that load distributed is reasonable. If you're at the point where multiple servers is worth the investment, of course.
You could have one lower-spec server handling data that's used less frequently or less aggressively (eg, big complicated queries) and another higher-spec server handling the data that gets processed more heavily.
Right.
And moving data across servers makes that harder, though IIRC SQL Server has native functionality for that. But if you can JOIN tables across servers in a query, I would expect that the processing has to happen on one server - it's not like the query is split in half and each piece executed by the server housing the associated data. So simply moving the data for the sake of moving it might not actually do you any good if you're querying both servers' data together frequently.
No. Not to me. If you have a particular client or three that is putting an exorbitant load on your server then you need do something about getting them a dedicated instance - as in a separate copy of the application and database on separate servers. But in general it should be the same database/server for everyone.
Meanwhile your thoughts about migration and the distributed architecture I can't help too much with.
Make sure you have proper indexing before thinking about this. Indexes are almost always enough to do the job without separating data. -
requinix's post in comma operator v/s binary op was marked as the answer
PHP does not have a comma operator.
-
requinix's post in 3 logical operators not working. was marked as the answer
Are you sure you didn't change anything else? What if you delete the addition and have that one line to back to how it was before?
-
requinix's post in I Need to install split() on php7 was marked as the answer
split() worked using POSIX regular expressions, so you might need to look up what you find sometimes.
However \n is a literal character and doesn't need regular expressions.
explode("\n", $foo)"\|" is a literal pipe character and doesn't need regular expressions either.
explode("|", $bar) -
requinix's post in n00b - displaying content of a specific row from MySQL in a table was marked as the answer
If you want a link to /results.php?ID=1 then the HTML looks like
<a href="/results.php?ID=1">Some text in here</a>Obviously you wouldn't write 1 - instead you'd use the value in $row.
As for the table, all you have to do is think in terms of rows and columns. With the table you want, "CREATOR" and "John Smith" would be in the first row and in the first and second columns respectively. Since HTML tables are written as rows containing columns you'd do
<tr> <th>CREATOR</th> <td>John Smith</td> </tr>Rinse and repeat for the other rows. Remember to remove the header row since your table won't be using one. -
requinix's post in Display records on week format was marked as the answer
So that's a little bit different from what you originally described... I assume the
is what you'll be repeating for each week? One table per week?
Here's the idea. Since you're "grouping" the tables by week, use a variable to track which week you're looking at. When it's a new week you start a new table.
But before that it helps to rearrange the code a bit. Get the first row before the loop, then fetch new rows at the end.
<?php $row = mysqli_fetch_array($mquery); ?> <table class="table"> <tr><td>Days</td> <?php while ($row) { echo "<tr><td>".$row['dow']."</td>"; echo "<td>".$row['no_one']."</td>"; echo "<td>".$row['no_two']."</td>"; echo "<td>".$row['no_three']."</td></tr>"; $row = mysqli_fetch_array($mquery); } ?> </tr> </table>The only big difference is that the fetch happens twice, once for the first row and later for subsequent rows.
With that change out of the way, here's the overall structure you'll be creating:
$row = fetch the first row $week = empty while ($row) { if $week is empty { start a new table $week = current week } show the data from $row $row = fetch the next row if no $row or $week != the week from the new $row { end the current table $week = empty } }Take a minute to understand how that works:1. Start without any $week information
2. When the first $row comes in you'll start a new table and update $week
3. Show that $row and fetch the next one
4a. If the new $row matches the current $week then don't do anything and just continue on
4b. If the new $row does not match the current $week, or if there aren't any more rows at all, then finish off the table
5. Keep going until there are no more rows
Steps 2 and 4 are important because HTML tables have starting and ending pieces that you need to make sure are created properly - you can't just output each $row and put some "break" in between them when the week changes.
Taking that structure and turning it into PHP code creates this:
<?php $row = mysqli_fetch_array($mquery); $week = null; while ($row) { if (!$week) { echo '<table class="table">'; echo '<tr><td>Days</td>'; $week = $row["week"]; } echo "<tr><td>".$row['dow']."</td>"; echo "<td>".$row['no_one']."</td>"; echo "<td>".$row['no_two']."</td>"; echo "<td>".$row['no_three']."</td></tr>"; $row = mysqli_fetch_array($mquery); if (!$row || $week != $row["week"]) { echo '</table>'; $week = null; } } ?> -
requinix's post in Processing data and identifying whether valid was marked as the answer
So this is for a different part of the application than the RPC messages?
Then I'd still follow what the spec says (regarding this particular bit) simply for the consistency - every part of the system uses the same result/error structure so there's no confusion.