Jump to content

Sanjib Sinha

Members
  • Posts

    97
  • Joined

  • Last visited

Posts posted by Sanjib Sinha

  1. Thanks 

    IThinkMyBrainHurts

    I 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. 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.

  5. 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.

  6. Im also looking for a framework,  but Laravel seems too fast moving for me.  I want something with a relatively long lifecycle per release.  I don't have time to relearn stuff every couple of months.

     

    Any suggestions.  I need really lightweight,  shallow learning curve,  keeps out of the way,  but comes with a nice feature set that I can use if I want to.  I find out of the box Auth never does what I want ect so don't want to be forced to use something.

    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. 

  7. 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. 

  8. So far i know, wordpress is just like any other open source cms. It has its own syntax, rules that you need to follow. You can download it from wordpress.org. You can also go through their documentaion or buy some books. Besides, wordpress.com is a place where you you can register and write your blogs. In that case you will have a domain like this: yourblog.wordpress.com

    Hope it helps.


  9. As a beginner please try to understand how data passes from one page to other. Get accustomed with super global arrays. Look at the url, how it changes and you will understand how data travels. 


    You need to understand few very important aspects like array. If you can not visualize array, you will never be able to grasp how a 'form data' passes from one page to other.


    There are good tutorials in phpfreak. Read them. If you stuck, ask in forum.


    Best of luck.


  10. many thanks trk.

    it was probably my bad English sentence construction, that changed the actual meaning for what i actually wanted to mean.

    i wanted to say, when i query the database, there is always a LIMIT. when i paginate, that LIMIT either reduces or decreases and to capture that fine balance you need to use some mathematical functions.

    i just downloaded an available pagination class and found, that array functions are not used at all! 

    may be that is the correct approach. i feel slightly confused in that regard.

  11. Any advice please? usually pagination being done by calculating LIMIT in sql code. i tried to avoid that mathematical calculation depending mainly on three array functions. 

    can this approach be encapsulated in a class? or i should take any other approach to solve this problem?

  12. I tried a simple pagination. As a web hobbyist, i tried to visualize the whole aspect first, and then tried to implement them. I need experts opinion. Is my approach okay? How i can i modify it (i am sure it is too simple an approach, and can be modified)?

     

    1) i keep an MyArray class at my lib folder and name it commonfunction.php. it goes like this:

    class myArray implements ArrayAccess {
    
        protected $array = array();
    
        function  offsetSet($offset, $value) {
            if(!is_numeric($offset)){
                throw new Exception("Invalid key {$offset}") ;
            }
            $this->array[$offset] = $value;
        }
    
        function  offsetGet($offset) {
            return $this->array[$offset];
        }
    
        function  offsetUnset($offset) {
            unset ($this->array[$offset]);
        }
    
        function  offsetExists($offset) {
            return array_key_exists($this->array, $offset);
        }
    
    }
    

    2) Next, i have a published comments table, that i want to paginate. To start with, i create a pagination1.php page. The code is like this:

    <?php
    include_once 'lib/commonfunction.php';
    
    if(!isset ($_REQUEST['next'])){
            echo "";
        }
        
    mysql_connect('localhost', 'root', '');
    mysql_select_db('appababa');
    
    //////////////////////////////////////////////////////////////////////////////////
    //trying pagination///
    $myrow = new myArray();
    $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'");
    $myresults = array();
    while ($myrow = mysql_fetch_array($myquery)) {
        $myresults[]=$myrow[0];
    }  
        
    foreach ($myresults as $key=>$value) {  
        
        $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$value."'");
        while ($fullrow = mysql_fetch_array($myquery)) {
            //echo "Description : ".$fullrow['description']."<br>";
                if ($fullrow['id']==1){
                    echo "Description : ".$fullrow['description']."<br>";
                    $next=next($fullrow);
                    $next++;
                    echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>";
                }
        }
    }
    ?>
    
    

    3) Next i have a pagination2.php. The code is like this:

    <?php
    include_once 'lib/commonfunction.php';
    
    if(!isset ($_REQUEST['next'])){
            echo "";
        }
    
    mysql_connect('localhost', 'root', '');
    mysql_select_db('appababa');
    
    //////////////////////////////////////////////////////////////////////////////////
    //trying pagination///
    $myrow = new myArray();
    $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'");
    $myresults = array();
    while ($myrow = mysql_fetch_array($myquery)) {
        $myresults[]=$myrow[0];
    }
    $present=0;
    if(isset ($_REQUEST['next']))
    $present = $_REQUEST['next'];
    
    if ($present==0){
        header('Location:http://localhost/ClassesAndObjects/pagination1.php');
    }
    
    $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$present."'");
    
    while ($fullrow = mysql_fetch_array($myquery)) {
            
            //echo "Description : ".$fullrow['description']."<br>";
                if ($fullrow['id']==$present){
                    echo "Description : ".$fullrow['description']."<br>";
                    
                    $next=next($fullrow);
                    $next++;
                    echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>";
                    echo " ------ ";
                    $prev=prev($fullrow);
                    $prev--;
                    echo "<a href='http://localhost/ClassesAndObjects/pagination3.php?prev={$prev}'>PREV</a>";
                    
                }
        }
    $row = new myArray();
    $query1 = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'");
    $results = array();
    while ($row = mysql_fetch_array($query1)) {
        $results[]=$row[0];
        //echo $row['description']." = "."<a href='http://localhost/ClassesAndObjects/update.php?id={$row['id']}'>UNPUBLISH</a>"."<br>";
    }
    
    //echo "Total number of published comments : ".count($results);
    $count=count($results);
    $count++;
        if ($present==$count){
        header('Location:http://localhost/ClassesAndObjects/pagination1.php');
    }
    
    ?>
    
    

    3) Next i have pagination3.php. The code is like this:

    <?php
    include_once 'lib/commonfunction.php';
    
    if(!isset ($_REQUEST['prev'])){
            echo "";
        }
    
    mysql_connect('localhost', 'root', '');
    mysql_select_db('appababa');
    
    //////////////////////////////////////////////////////////////////////////////////
    //trying pagination///
    $myrow = new myArray();
    $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'");
    $myresults = array();
    while ($myrow = mysql_fetch_array($myquery)) {
        $myresults[]=$myrow[0];
    }
    $present=0;
    if(isset ($_REQUEST['prev']))
    $present = $_REQUEST['prev'];
    
    if ($present==0){
        header('Location:http://localhost/ClassesAndObjects/pagination1.php');
    }
    
    $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$present."'");
        while ($fullrow = mysql_fetch_array($myquery)) {
            //echo "Description : ".$fullrow['description']."<br>";
                if ($fullrow['id']==$present){
                    echo "Description : ".$fullrow['description']."<br>";
    
                    $next=next($fullrow);
                    $next++;
                    echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>";
                    echo " ------ ";
                    $prev=prev($fullrow);
                    $prev--;
                    echo "<a href='http://localhost/ClassesAndObjects/pagination3.php?prev={$prev}'>PREV</a>";
    
                }
        }
    
    ?>
    
    

    Finally, it works faultlessly. There is apparently no problem. It is based on mainly three array functions, count(), next(), and prev(). I tried to avoid mathematical calculation as much as possible. 

     

    I need your opinion. Many thanks.

  13. This is a 'catch-all' error generated by the Web server. Basically something has gone wrong, but the server can not be more specific about the error condition in its response to the client.

    So you need to guess where it happens. I think it is session. I would have done in a different way, like this:

    function check_auth()
    {
    if($_SESSION['admin_id'] and $_SESSION['admin_email'])
    return true;
    else
    return false;
    }
    

    After creating a check_auth() function you can start your session at top and get your page like this:

    if(!check_auth())
    header("location:logout.php") or die();
    $page="adminpage";
    

×
×
  • 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.