Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just posted a thorough solution to <a href="https://stackoverflow.com/questions/1082349/page-generation-from-php-class/1082470#1082470">another question</a> that is very closely-related to this question. I'll re-post it here for your convenience:</p> <hr> <p>I would suggest using some of the MVC (Model, View, Controller) frameworks out there like <a href="http://www.kohanaphp.com" rel="nofollow noreferrer">KohanaPHP</a>. It is essentially this. You're working in a strictly Object-Oriented environment. A simple page in Kohana, build entirely from a class would look like this:</p> <pre><code>class Home_Controller extends Controller { public function index() { echo "Hello World"; } } </code></pre> <p>You would then access that page by visiting youur url, the class name, and the method name:</p> <p><a href="http://www.mysite.com/home/" rel="nofollow noreferrer">http://www.mysite.com/home/</a> (<em>index() can be called after home/, but it's implicit</em>)</p> <p>When you start wanting to bring in database-activity, you'll start working with another Class called a <a href="http://docs.kohanaphp.com/general/models" rel="nofollow noreferrer">Model</a>. This will contain methods to interact with your database, like the following:</p> <pre><code>class Users_Model extends Model { public function count_users() { return $this-&gt;db-&gt;count_records('users'); } } </code></pre> <p>Note here that I didn't write my own query. Kohana comes with an intuitive <a href="http://docs.kohanaphp.com/libraries/database/builder" rel="nofollow noreferrer">Query Builder</a>.</p> <p>That method would be called from within your <a href="http://docs.kohanaphp.com/general/controllers" rel="nofollow noreferrer">Controller</a>, the first class that we mentioned at the beginning of this solution. That would look like this:</p> <pre><code>class Home_Controller extends Controller { public function index() { $usersModel = new Users_Model; $userCount = $usersModel-&gt;count_users(); echo "We have " . $userCount . " users!"; } } </code></pre> <p>Eventually, you'll want more complicated layouts, which will involve HTML/CSS/Javascript. At this point, you would introduce the "<a href="http://docs.kohanaphp.com/general/views" rel="nofollow noreferrer">Views</a>," which are just presentation layers. Rather than calling <code>echo</code> or <code>print</code> from within the Controller, you would load up a view (an HTML page, essentially) and pass it some variables:</p> <pre><code>class Home_Controller extends Controller { public function index() { $myView = new View("index"); $usersModel = new Users_Model; $userCount = $usersModel-&gt;count_users(); $myView-&gt;userCount = $userCount; $myView-&gt;render(TRUE); } } </code></pre> <p>Which would load the following "View"</p> <pre><code>&lt;p&gt;We have &lt;?php print $userCount; ?&gt; users!&lt;/p&gt; </code></pre> <p>That should be enough to get you started. Using the MVC-style is really clean, and very fun to work with.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload