Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Everything posted by benanamen

  1. A function also known as a method when in a Class should do one thing and do it well. It should also be easily re-usable and not have to be edited to use it elsewhere. What you have is none of that. What you will probably discover as the first problem is that you have hard coded the groups which means if you want a group other than uk you now have a problem on your hands. Duplicating the function for every group is not the answer. What you want to do is pass the groups value to the function as a parameter. Now you can use the same function for any group. Same thing goes for the hard-coded table name. There is also no option to specify specific columns. Again you would run into problems with the hard coded column headers and every other hard coded item. Here is something you get you going towards a dynamic solution. (It's not a cut n paste solution) <?php declare(strict_types=1); $sql = 'YOUR-QUERY-HERE'; $stmt = $db->pdoQuery($sql); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) { echo '<H1>No Records Available</H1>'; } else { $data = []; foreach ($row as $key => $val) { $data[ucwords(str_replace('_', ' ', $key))] = $val; } ?> <div class="table-responsive"> <table id="myDataTable" class="table table-bordered table-striped table-hover table-sm"> <thead class="thead-dark"> <tr> <th><?= join('</th><th>', array_keys($data)) . "</th>\n" ?> </tr> </thead> <tbody class="searchable"> <?php do { echo "<tr>\n<td>" . join("</td>\n<td>", $row) . "</td>\n"; echo " </tr> \n"; } while ($row = $stmt->fetch(PDO::FETCH_ASSOC)); ?> </tbody> </table> </div> <?php }
  2. A quick observation.... You have tightly coupled the Request and Response Classes. It would be better to use Dependency Injection (DI) to give the class what it needs by passing the Response instance to the Request Class.
  3. Which is why you cant say "@Barand's test makes more sense." Based on OP's code, he expects there could be a negative int, otherwise he wouldn't be checking for it. If "int" is negative, @Barands solution will fail (false). var_dump(ctype_digit('-128')); // False Casting the string to an int wouldn't work either. $x = (int) '5'; Yes , it will make it an int and support negative values, but it will cast "anything" you put in there into an int such as the following and then you cannot check if it is a real int. $x = (int) 'xx'; // int 0 So what to do then? One option that also addresses the source as a string argument would be the following. function fact($x): int { return $x <= 1 ? 1 : $x * fact($x - 1); } $x = '-2147483648';// OK $x = -2147483648;// OK $x = 5; // OK $x = '5';// OK $x = 'ABC'; // Fail: Please provide an integer echo (filter_var($x, FILTER_VALIDATE_INT) === 0 || filter_var($x, FILTER_VALIDATE_INT)) ? fact($x) : 'Please provide an integer';
  4. Hi @Barand, I am wondering why you would be introducing strings when this is specifically about int's. IMO is_int should be used instead of ctype_digit. OP, the else is redundant. You can remove it. Additionally, IMO the int check should not be in the function. The function/method should do one thing and do it well. As is example function fact($x) { if (!is_int($x)) { return 'Please provide an integer'; } if ($x <= 1) { return 1; } return $x * fact($x - 1); } echo fact(5); Suggested Example function fact(int $x): int { return $x <= 1 ? 1 : $x * fact($x - 1); } $x = 5; echo is_int($x) ? fact($x) : 'Please provide an integer';
  5. As to the specific error, you need to add quotes like so getenv('QUERY_STRING') Additionally, just about everything that could be wrong with this code is wrong.
  6. @johnwalsh1020, This thread died over a year ago and the OP is long gone. Besides that, the tutorial you linked to is poorly coded and a bad example of how to do login/reg code. md5 for passwords should have been your first red flag.
  7. I am surprised there has been no mention of foreign key constraints. OP, if you set up proper foreign key relationships, all you would have to do is delete the parent record using ON DELETE CASCADE and all the child records would automatically delete as well. Here is some information about it. http://www.mysqltutorial.org/mysql-on-delete-cascade/
  8. Do you mean something like this?
  9. When it comes to the forums I have never seen it not be. OP, tell us about what you actually have going on rather than asking about your attempted solution to it.
  10. Why do you have duplicate table structures? This points to a design flaw.
  11. PRG - Post, Redirect, Get https://en.wikipedia.org/wiki/Post/Redirect/Get
  12. One of the gotcha's I was unaware of. Thanks.
  13. Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).
  14. It may be beyond your skills, but If that is the goal you can write a router that will call whatever file you want regardless of the name and URL. Look into MVC design.
  15. Yes but you don't want to run both at the same time. If you really wanted to, you would need to change the Apache port on one of them as they both use port 80
  16. You would find Laragon to be even better. (Coming from a former Xampp user) https://laragon.org/
  17. You will need a session id and a WHERE clause in your query.
  18. For starters, you don't need to check if the session has been started. Just set it and be done with it. You also need to kill the script after a header redirect and don't create variables for nothing.
  19. The first thing you do is issue a redirect and kill the script. How do you expect it to do anything after that?
  20. OP, I would really suggest you actually learn how to code instead of using a WYSIWYG. The output is absolutely horrendous.
  21. What should be suggested is that you stop using dangerous obsolete mysql code that has been completely removed from PHP and was warned about for well over ten years and to stop running a php version that is hundreds of releases behind and reached end of life long ago. Toss that code in the trash. Every bit of it is bad. You need to use PDO with Prepared Statements. This tutorial will get you going. https://phpdelusions.net/pdo
  22. OP, you were handed a complete properly coded example on the other forum by a gracious expert. Why are you still messing around with this bad code?
  23. It means you posted this in multiple forums and someone already took the time in another forum to answer you so we are not going to waste more experts time answering something that has already been answered.
  24. OP was given answer in another forum.
×
×
  • 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.