SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    from traditional PHP to MVC

    I need some help to put this simple code into MVC any help would be appreciated. It is a simple form which retrieves data from an SQL DB.. it works fine as one file but I have problems to put it in MVC, especially with submit part.
    I know for many people this is an easy thing to do in MVC... but i am in a learning process. Plz help

    =================== HTML ========
    </head>
    <p><body>
    <h3>Search Contacts Details</h3>
    <p>You may search either by first or last name</p>
    <form method="post" action="search_start.php?go" id="searchform">
    <input type="text" name="name">
    <input type="submit" name="submit" value="Search">
    </form>
    </body>
    </html>

    ================== PHP ============
    <?php
    if(isset($_POST['submit'])){
    if(isset($_GET['go'])){
    if(preg_match("/[A-Z | a-z]+/", $_POST['name']))
    {

    $name=$_POST['name'];

    //-query the database table
    $sql="SELECT * FROM Contacts WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";

    //-run the query against the mysql query function
    $result=mysql_query($sql);

    //-create while loop and loop through result set
    while($row=mysql_fetch_array($result)){

    $FirstName =$row['FirstName'];
    $LastName=$row['LastName'];
    $ID=$row['ID'];
    $Email=$row['Email'];

    //-display the result of the array
    //echo "<ul>\n";
    echo "<li>" . "<a href=\"search_start.php?id=$FirstName\">" .$FirstName . " " . $LastName . "</a></li>\n";

    //echo "</ul>";
    }
    }
    else{
    echo "<p>Please enter a search query</p>";
    }
    }}

    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    My suggestion is to read about what MVC is. It's not just pushing a "button convert to MVC", most of the times it's preferred to use some MVC framework and for each the code and approach will slightly differ.

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,263
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    I suggest reading From Flat PHP to Symfony2. It takes you step-by-step from the kind of PHP you're writing now to a more organized structure.
    "First make it work. Then make it better."

  4. #4
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    I suggest reading From Flat PHP to Symfony2. It takes you step-by-step from the kind of PHP you're writing now to a more organized structure.
    Definitely +1

    And maybe you should post the result of your refactoring so someone can use it as an example and learn from it (or we can even improve what you've done).

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    So is this part of a larger site or what? The reason I ask is that wrapping a whole bunch of abstraction around something so simple might not be answer.
    The only code I hate more than my own is everyone else's.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ronalds View Post
    My suggestion is to read about what MVC is. It's not just pushing a "button convert to MVC", most of the times it's preferred to use some MVC framework and for each the code and approach will slightly differ.
    @ronalds ;, you mean you haven't found PHP's convert-mvc feature yet?

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)

  8. #8
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you to use CodeIgniter framework. Really easy, light and clear

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Vote for Zend Framework 1.x here

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,263
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Are we voting now?

    +1 for Symfony2

    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •