Jump to content

hansford

Members
  • Posts

    562
  • Joined

  • Last visited

  • Days Won

    4

Posts posted by hansford

  1.  

     

    Why use a framework over just coding it from scratch as above?

     

    For one thing...modern frameworks are constantly being updated and improved. Another thing is that is what companies want when looking to hire a developer. You should understand the MVC design pattern and how each part works, but then go learn a modern framework.

  2. I wrote a custom one when I was trying to wrap my head around it.

    This is what my index.php page looks like.

    <?php
    require($_SERVER['DOCUMENT_ROOT'] . '/config.php');
    require_once(ROOT . '/common.php');
    
    // set the path separator
    define('PS','/');
    
    require_once(APP . '/common.php');
    
    // load system files
    require_once(SYS . 'Model.php');
    require_once(SYS . 'Load.php');
    require_once(SYS . 'View.php');
    require_once(SYS . 'Controller.php');
    require_once(SYS . 'Router.php');
    
    // load the database
    require(DB);
    
    // load routes configuration
    require(APP . '/config' . PS . 'routes.php');
    
    // launch application
    $router = new Router($routes);
    $router->init();
    
    • Like 1
  3. Hi guys and girls,

     

    A recruiter asked me to do this coding challenge and was wondering if you had a more elegant solution or your thoughts on it.

     

    EXERCISE FOR DEVELOPER CANDIDATES: Unit Number Sorting Exercise

     

    We often deal with unit number / resident name data. These two pieces of information are used by our users to identify a lease. Many of our screens and reports show a list of leases.

     

    The task is to sort lease data read from a file and to print the sorted data to STDOUT. The data looks as follows (sample also attached):

     

    #50 - Smith

    #8 - Johnson

    #100 - Sanders

    #1B - Adams

    #1A - Kessenich

     

    Each line contains a unit number and a resident name. The data should be sorted by unit number.

     

    Develop a solution in PHP and one other language of your choice that reads the data from a file and prints the data (sorted by unit number) to STDOUT. The printed strings should not be modified from how they appear in the input file.

     

    Here is my solution:

    <?php
    define('br','<br />');
    
    // open the file
    $fp = fopen('input.txt','rb+');
    
    if (false === $fp)
    {
        die ('failed to open file');
    }
    
    $numbers = array();
    $letters = array();
    
    // read the file line by line
    while (($line = fgets($fp, 1024)) !== false)
    {
        // split the string by unit number and name
        $str = explode(' - ',$line);
    
        // separate unit numbers from units with letters
        if (preg_match('/[a-zA-Z]/', $str[0]))
        {
           $letters[$str[0]] = $str[1];
        }
        else 
        {
            $numbers[$str[0]] = $str[1];
        }
    }   
    
    if ( ! feof($fp))
    {
        die ('error reading file');
    }
    
    fclose($fp);
    
    // sort each array
    ksort($numbers,SORT_STRING);
    ksort($letters,SORT_STRING);
    
    // merge arrays with unit letters appearing first
    $output = array_merge($letters, $numbers);
    
    // output data
    foreach ($output as $key => $value)
    {
        echo $key . ' - ' . $value . br;
    }
    
    
    
  4. You are over-thinking this whole thing. You can call whatever class/function you want in the PHP file that will be accessed by your Ajax call.

    in the data you pass to your PHP page, you can tell it what class should be called, what function etc.

     

    In this following example we have 'action' which tells your PHP page to call the "delItem" method on a known class and pass the method the "item" argument.

    $.ajax({
    url: 'http://www.yourwebsite.com/file_name.php',
    type: 'POST',
    data: {action: 'delItem',item: item},
    success: function (response) {
        if (response.status === 'success') {
            console.log("success");
            $("#myModal").modal('hide');
            $('#respostas .modal-title').html('Sucesso');
            $('#respostas .modal-body').html('Informação: ' + response.message);
            $('#respostas').modal('show');
            $('#respostas').on('hidden.bs.modal', function () {
                window.location.reload(true);
            });
        }
    }
    });
    
  5.  

     

    Is validation part of the model? Yes.

     

    The rules went out the window long ago when MVC stopped matching up to real world of computer use.

    "Business Rules" belong in the model. MVC is a pattern, not a rule. All of the frameworks around today break the rules. 

    I know I'm not telling you anything you don't already know. I'm just arguing lol

  6. There is argument on both sides of this debate. Personally, I handle validation from within the controller. 

    No need to clutter up your controller with validation statements though - create a validation class and let it handle all of that.

    • Like 1
  7. You are going to have to use Ajax calls if you want to dynamically shift the questions up or down based on vote clicks.

    As far as initially displaying the page.

    <form action="feed.php" method="post">
        <textarea required="" name="question" placeholder="Write Something..." id="text" cols="30" rows="10"></textarea>
    <input type="submit" name="submit" value="ask">
    </form>

    feed.php

    if ( ! isset($_POST['question']))
    {
        // give error or send them back to the page etc.
        exit;
    }
    
    // here is where your database will come in 
    // based on the number of votes you display the page html
    
  8. $sql = "SELECT status from users WHERE id={$_SESSION['username']}";
    
    if ( ! $result = $mysqli->query($sql))
    {
        //do something
    }
    

    If you want to log users out after so much time of inactivity then put a timestamp column in your table and each time they do an activity check the current time against the db time and log them out if it's been too long or update the timestamp column. 

  9. Hopefully, this makes requinix' comments a little more clear.

     

    Shape.php

    abstract class Shape {
    
        public abstract function getArea();
    
        public abstract function getPerimeter();
    
        public abstract function scale($scale);
    
    }

    Rectangle.php

    class Rectangle extends Shape {
        
        private $width;
        private $height;
        
        public function __construct($width, $height)
        {
            $this->width = $width;
            $this->height = $height;
        }
        
        public function getArea()
        {
            return $this->width * $this->height;
        }
        
        public function getPerimeter()
        {
            return $this->width * 2 + $this->height * 2;
        }
        
        public function scale($scale)
        {
            $this->width *= $scale;
            $this->height *= $scale;
        }
    }
    

    index.php

    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
    define('br','<br />');
    
    $rect = new Rectangle(3,4);
    
    echo "Area of 3 x 4 rectangle is: {$rect->getArea()}" . br;
    echo "Perimeter of 3 x 4 rectangle is: {$rect->getPerimeter()}" . br;
    echo '-------------------' . br;
    echo "After scaling a 3 x 4 rectangle by a factor of 2" . br;
    echo '-------------------' . br;
    $rect->scale(2);
    echo "Area of scaled rectangle is: {$rect->getArea()}" . br;
    echo "Perimeter of scaled rectangle is: {$rect->getPerimeter()}" . br;
    • Like 1
  10. Most data that gets passed around through sessions and hidden form fields seems to be non-critical data. Google and Facebook don't seem to think the data they pass around about you is all that "critical". However, my bank and credit card companies sure do. They will log me out if I spend too long taking a leak in the bathroom.

  11. Leave it out, leave it an evil empty string it's still going to submit back to the submitting page as of now. The specs say a lot of things, but the browsers have the last say in whether it's implemented or not. However, in the nature of goodwill and standards promotion, if you're going to submit back to the page, just omit the tag(edit: attribute) and make everyone happy.

  12. Since you are using international chars, use utf-8 for your encoding.

    $.ajax({
    
    url: "get_id.php",
    type: "POST",
    dataType: "json",
    contentType: "application/x-www-form-urlencoded;charset=utf-8",
    data : {action: 'visitaValidacaoByUserAdmin', id_visita: 'id_visita', plano: 'plano', valido: 'valido'},
    cache: false,
    success: function(response) {
    
          alert(response['message']); 
    }
    

     

    PHP page example:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // set headers
    header("Content-Type: application/json; charset=utf-8");
    
    if (isset($_POST['action']))
    {
        // call function
        $ret = $_POST['action']();
        
        // return result
        echo json_encode($ret);
    }
    
    function visitaValidacaoByUserAdmin()
    {
        return array("status" => "success", "message" => "A visita já foi validada...Obrigado");
    }
    
×
×
  • 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.