Jump to content

Sanjib Sinha

  • Content Count

  • Joined

  • Last visited

Everything posted by Sanjib Sinha

  1. Thanks IThinkMyBrainHurtsI guessed something like this. But why should I go for such complexity? Please consider this code to test the primality: function is_prime($num) { for($i=2; $i<= (int)sqrt($num); $i++){ if($num % $i == 0) { return false; } } return true; } if(is_prime(119)){ echo 'Prime'; } else { echo 'Not Prime'; } //output not prime
  2. I had searched web and found some of them very interesting and few of them seem complex like this one in wiki: function isPrime($n) { if ($n <= 3) { return $n > 1; } else if ($n % 2 === 0 || $n % 3 === 0) { return false; } else { for ($i = 5; $i * $i <= $n; $i += 6) { if ($n % $i === 0 || $n % ($i + 2) === 0) { return false; } } return true; } } Specially this part: for ($i = 5; $i * $i <= $n; $i += 6) { if ($n % $i === 0 || $n % ($i + 2) === 0) { return false; } } It started from 5. It is okay. But the logic flow seems pretty complex. Can anyone translate in simple English actually what is happening?
  3. I have written a function to check whether any number is prime or not. Is it okay? or I can better it by any means? function isPrime($num){ $number = array(); for ($i=2; $i <= $num; $i++){ if(($num%2)==0){ continue; } if (($num%$i)==0){ break; } $number[]=$i; } /* * how I back calculate to make it successful foreach ($number as $key => $value) { echo "$key = $value <br>"; } echo count($number); */ if (count($number)== ($num-2)) { echo 'it is prime'; } else { echo 'not prime'; } } isPrime(101112345909);
  4. To learn it more please google 'type hinting'. I think it will help a lot.
  5. Please check whether the root path is correctly defined or not. I think there is some problem.
  6. AS you said: It works if I remove the class.. just not WITH the class.. I think you would better write the class again. Keeping in mind, every class should have single responsibility. It is also not a good practice to hit the database directly from your class where you are displaying data so you can have it encapsulated. And finally as NotionCommotion said create an instance. Best Wishes.
  7. It can be done. The site will be role based. Admin from his dashboard can control everything. You need to create a proper database table structure for doing this.
  8. What Psycho said, is true. Client Side validation is always dangerous. But Server side also can bring some trouble if you don't take proper guard. I just thought how it would be to use a very simple 'Validate' class. I used trait and interface so that you could hook more functionality later. First the validate.php code: <?php /* * . */ trait ValidateTrait { public function checked() { return "Okay, Your Data has been saved"; } public function unchecked() { return "You have not provided proper data."; } } interface ValidateInterface { public function make($value); } class Validate implements ValidateInterface { use ValidateTrait; public $_value = array(); public function make($value) { $this->_value = $value; if (strlen($value) === 0 || strlen($value) < 3 || strlen($value) > { return FALSE; } elseif(is_string($value) && trim($value) === ''){ return FALSE; } elseif (is_array($value)) { return FALSE; } elseif (preg_match("/\@.\/i", $value)) { return FALSE; } return TRUE; } } Second the form.php: <form method="POST" action="action.php" accept-charset="UTF-8"> <input name="_token" type="hidden" value="EMnZhqPbryk7oVPcrjwxuTrlHto"> <label for="username">Your Name</label> <p> <input name="username" type="text" value="Your name" id="username"> <p> <label for="email">Your Email</label> <p> <input name="email" type="text" value="your email" id="email"> <p> <input type="submit" value="Register"> </form> and finally the action.php where I put the message of validation: <?php /* * */ require 'validate.php'; if ($_POST['_token'] === "EMnZhqPbryk7oVPcrjwxuTrlHto"){ $value = [$_POST['username'], $_POST['email']]; $validate = new Validate(); if ($validate->make($value[0]) && $validate->make($value[1])){ echo $validate->checked(); } else { echo $validate->unchecked(); } } else { echo 'You Crackers! Go back!'; } You can obviously add many more fields to it as you need. I just checked username and email. username field can not be blank, and between 3 to 8 characters etc. Best wishes to all.
  9. Yes, I am agreed with Adam. To start with and getting acquainted with the MVC approach, CodeIgniter is fine. Hopefully it will catch up with the advancement taking place in the PHP world; but, till then you can start with CI.
  10. Tons of Basic WordPress video tutorials are there in you tube. For the beginners I think, video tutorials make lasting impacts primarily. Please try it before you go down writing your own themes and plugins.
  • 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.