A method in the controller.php file

public function invoke()
    {
        if (isset($_GET['book']))
        {
            // show the requested book
            $book = $this->model->getBook($_GET['book']);
            include 'view/viewbook.php';    
        }
        else if(isset($_GET['add_book_form']))
        {
            $action = $_GET['add_book_form'];
            include 'view/add_book.php';
        }
        else if(isset($_GET['add_book']))
        {   
            $title = $_POST['title'];
            $author = $_POST['author'];
            $description = $_POST['description'];

            $this->model->addBook($title, $author, $description);

            include("view/add_book.php");
        }
        else
        {
            // no special book is requested, we'll show a list of all available books
            $books = $this->model->getBookList();
            include 'view/booklist.php';
        }
    }

Body of my View/booklist.php

<table>
    <tr><td>Title</td><td>Author</td><td>Description</td></tr>
    <?php 

        foreach ($books as $book)
        {
            echo '<tr><td><a href="index.php?book='.$book->title.'">'.$book->title.'</a></td><td>'.$book->author.'</td><td>'.$book->description.'</td></tr>';
        }

    ?>
</table>

<a href="index.php?add_book_form">Add Book</a>

Body of the View/add_book.php file

<div>
        <form method="post" action="index.php?add_book" id="add_book_form">
            <input type="hidden" name="action" value="add_book" />

            <div class="label">
                <label>
                    Title: 
                </label>
            </div>
            <input type="text" size="40" name="title"/>
            <br />

            <div class="label">
                <label>
                    Author: 
                </label>
            </div>
            <input type="text" size="40" name="author"/>
            <br />

            <div class="label">
                <label>
                    Description: 
                </label>
            </div>
            <input type="text" size="40" name="description"/>
            <br />

            <input type="submit" value="Add Book" />
        </form>
        <a href="index.php?list_books" class="link">View List</a>
    </div>

My model.php class

class Model {

    private $books;

    public function __construct()
    {
        $this->books = array(
            "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),
            "Moonwalker" => new Book("Moonwalker", "J. Walker", ""),
            "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", ""));
    }

    public function getBookList()
    {
        return $this->books;
    }

    public function getBook($title)
    {
        // we use the previous function to get all the books and then we return the requested one.
        // in a real life scenario this will be done through a db select command
        $allBooks = $this->getBookList();
        return $allBooks[$title];
    }

    public function addBook($title, $author, $description)
    {
        array_push($this->books, new Book($title, $author, $description));
        //foreach ($this->books as $book)
        //{
        //  echo 'Title: '.$book->title.' __Author: '.$book->author.' __Description: '.$book->description.'<br />';
        //}
    }

}

Now my problem is that when I click the 'Add Books' on the add_book.php page there is a book thats get added to the $books array in the Model.php class. But since i am using array_push() - it adds one element to the end of the array but when i click the add button again, it just changes the last element that was previously added, instead it should add one element to the end every time I click the add button. And if I go back to the view_list.php page i don't even the last item that was added. If any body can help me on adding a Book to the books array in the Model.php class so that it displays on the viewlist.php page once added, i would be really thankful, please let me know where am I going wrong, I have been trying from quite a while without any success, I don't have i been missing.

You don’t store anywhere your booksList , despite that you just give values to that array in the constructor of your Model. As I understand this is visitor scope caching so you could be fine using session, BUT this is something the controller is responsible for not the model in my point of view. There are other MVC structure issues in your example but I will not expand my thoughts since this is has’t to do with the topic and are many MVC implementations.

do you have the program complete?
please send me the program, I am student, and I need a good example for my homework

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.