Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    19
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Rewriting/converting a single php file into a MVC format.

    Friends,
    Please help me how to write the php echo scripts that needs to be embedded in HTML. All codes are presently saved in a single PHP file.

    Below is the display in the web page which shows list of the remaining months in 2014 with total monthly event count.

    WEB PAGE DISPLAY
    July (2)
    August (2)
    September (1)
    October (0)
    November (6)
    December (2)


    PHP CODINGS CURRENTLY SAVED IN A SINGLE FILE

    <?php
    require_once ('../model/database.php');

    //this function will provide all events count per month for this year
    //the current year will be sent as parameter
    function get_eventsCount($year) {
    global $db;
    $query = 'SELECT MONTH(E.dateStart) AS month, COUNT(*)
    AS nbEvents FROM events E
    WHERE YEAR(E.dateStart) = :year
    GROUP BY MONTH(E.dateStart)';
    $data = array();
    try {
    $statement = $db->prepare($query);
    $statement->bindValue(':year', $year);
    $statement->execute();
    while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    $data[$row['month']] = $row['nbEvents'];
    }
    $statement->closeCursor();
    //we will get something like

    /*
    Array (
    7 => 2,
    8 => 2,
    10 => 1,
    11 => 1,
    12 => 1
    )
    */

    return $data;
    } catch (PDOException $e) {
    display_db_error($e->getMessage());
    }
    }

    // get the number of events for a specific month
    function getNbEventsForMonth($m, $data) {
    $nb = 0;
    if (is_array($data) && isset($data[$m])) {
    $nb = $data[$m];
    }
    return $nb;
    }

    //get all events number per month for the current year (date('Y'))
    $data = get_eventsCount(date('Y'));

    //get the timestamp for the first day of the current month / which will be our start month
    $timeStamp = strtotime('first day of this month');

    //get the month number (for the current month)
    $m = date('n', $timeStamp);

    //display with using of the getNbEventsForMonth() fucntion
    echo '<p><a href="yourlink">'.date('F', $timeStamp).' ('.getNbEventsForMonth($m, $data).')</a></p>';

    //while we do not reach the end of the year
    while ($m < 12) {

    //same as above / just we want to get here even the missing month (for example september)
    $timeStamp = strtotime('next month', $timeStamp);
    $m = date('n', $timeStamp);

    echo '<p><a href="yourlink">'.date('F', $timeStamp).' ('.getNbEventsForMonth($m, $data).')</a></p>'
    ;
    }

    //the date('F') allow us to get the full month name

    ?>

    I highlighted the codes that needs to be embedded in HTML file for display.

    Many thanks in advance.

    espi62 (newbie)
    Last edited by espi62; 07-16-2014 at 11:28 PM. Reason: rephrase the question

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    MVC is not a format, it’s a Pattern. you’ll end up with a couple to some hundred new files.

    I recommend to use an existing Framework to rewrite that (almost all major frameworks somehow implement MVC).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    19
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hello Dormilich,

    Thank you very much for your email and your comment.

    Let me correct my statement in my post as stated below.

    Please help me how to write the php scripts echo portion (together with their $variables - highlighted in red) that needs to be
    embedded in html.

    I am very new in php and hoping to get help from highly skilled developers like you.

    Thank you very much in advance.

    espi62

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    I am very new in php and hoping to get help from highly skilled developers like you.
    then you should forget MVC for now. (unless you want to spend quite some time understanding programming patterns first)


    Please help me how to write the php scripts echo portion (together with their $variables - highlighted in red) that needs to be
    embedded in html.
    I recommend to look into Slim and Twig.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    espi62 (08-13-2014)

  • #5
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    19
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hello Dolimich

    My apologies for not responding to you immediately.

    I was able to get the solution for my problem in an MVC.

    Thank you for your recommendation and your time sharing.

    espi62


  •  

    Tags for this Thread

    Posting Permissions

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