About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

You are here: Home / Documentation / Database Programming in Sitellite

Database Programming in Sitellite

A Simple Example

<?php

echo template_simple (
    '<h1>You have selected story the story named "{title}"</h1>',
    db_single (
        'select * from sitellite_news where id = ?',
        $parameters['id']
    )
);

?>

As you can see, there is no error handling here yet. However, this example illustrates the very concise nature of the Sitellite Database API. Let's insert error handling into it and break it up into multiple lines.

<?php

$result = db_single (
    'select * from sitellite_news where id = ?',
    $parameters['id']
);
if (! $result) {
    echo db_error ();
    return;
}

echo template_simple (
    '<h1>You have selected story the story named "{title}"</h1>',
    $result
);

?>

This example outlines a few features of the Sitellite Database API, namely:

  • Parameters are passed to the query using substitutions. These substitutions are database-agnostic, which helps increase your code portability, which encourages you to avoid inlining variables into your queries. This increases the security of your applications by eliminating the chance of SQL Injection hacks.
  • You never have to call "global $db;" or "$db = new Database ();" before working with the database in Sitellite. Database access is one of the most common operations of any web application, so why not make it as succinct as possible?
  • The use of global-level functions which match the naming standard of PHP itself increases the readability of your code in two ways:
    1. By eliminating stylistic differences within the same piece of code.
    2. By eliminating unnecessary symbols (ie. "$db->fetch ()" vs. "db_fetch ()").
    We are proud to say that Sitellite features the only database abstraction layer to brave such a step backwards.

A More Interesting Example

Let's look at another example:

<?php

echo template_simple (
    '<ul>{loop obj}
        <li>
            <a href="{site/prefix}/index/tutorials-story-action/story.{loop/id}">
                {loop/title}
            </a>
        </li>
    {end loop}</ul>',
    db_fetch_array (
        'select id, title
        from sitellite_news
        order by date desc
        limit 10'
    )
);

?>

Save this to a file, say inc/app/example/boxes/headlines/index.php. Then call it in your browser via http://www.yourWebSite.com/index/example-headlines-action. You can see from the example that it will display the ten most recent news headlines.

Next, log into Sitellite and create a new sidebar by clicking on the "+" icon at the top of the left or right columns in the example site. Leave the body blank but enter an ID (say "example_headlines") and a Title (say "Latest Articles"), but click on the "Properties" tab and in the "Alias of (a box name)" field enter "example/headlines". Next, click on the "State" tab and set the Status to "Approved" and the Access Level to "Public" (I'm assuming you're not learning on a LIVE web site, by the way ;)). Click the "Create" button at the bottom of the form.

You will now see your headline action inserted into the sidebar of your web site, seamlessly integrated into the existing web site.



Page 1: Rationale
Page 2: A Simple Example
Page 3: Model-View-Controller (MVC) Mistakes

All Tutorials

Members

Note: You can use your SitelliteForge.com account here and vice versa.

Username

Password

Forgot your password?

Not a member? Click here to register

Sitellite 5 Beta


Copyright © 2008, SIMIAN systems Inc.
All rights reserved. Privacy policy
Some of the icons on this site were created by the Gnome Project.