Inserting data from a database help

Discussion in 'TemplateCodes' started by skekum, Aug 25, 2009.

  1. skekum Customer

    Hi Guys,

    I have populated a mysql database with some data and want to display it on the home page.

    Using standard mySQL & php, I can retrieve the 3 rows of data, and echo the array to the page, but it somehow breaks the building of the page.

    This is the code i've placed onto the home.tpl where I want the data to appear:
    PHP:
    $host="localhost";
    $user="***";
    $password="***";
    $database="***";

    $connection mysql_connect($host,$user,$password)
        or die(
    "Couldn't select database");

    // connect to db
    $db mysql_select_db($database,$connection)
        or die(
    "Couldn't select the database");
        

    // Get the last 3 rows of data from the db
    $query "SELECT * FROM whatson ORDER BY id DESC LIMIT 3";
    $result mysql_query($query)
        or die(
    "Couldn't select anything from the db but don't get upset");

    // Count the number of rows retrieved.
    $nrows mysql_num_rows($result);

    // Start a loop to echo out the results in a table.

    for ($i=0;$i<$nrows;$i++)
    {
        
    $row mysql_fetch_array($result);
        
    extract ($row);
        echo 
    "<table id='whatsontable'>";
        echo 
    "<tr><td class='articletable_title'><a href='$link' target='_blank'>$title</a></td></tr>";
        echo 
    "<tr><td>$body…</td></tr>";
        echo 
    "</table><br />";
    }
    As seen in the attached picture, the data is written correctly but the menus on the right hand column are not completed leaving empty boxes. I suspect my {php} tags on the home.tpl page are breaking the code?

    [IMG]

    I'm new to smarty and have been through the manual and the forums, but can't seem to get a handle on what I need to do to get this to work.

    Any help would be appreciated.

    Currently as a work around, I'm "hard coding" the data onto the page, but i'm trying to automate it.

    Glenn
  2. Eric Barnes Guest

    The proper way to do this would be to include that code in your index.php file and then assign the results to smarty. Maybe take a look at includes/init.php where it assigns pages out.

    Also check out Smarty : Smarty Crash Course
  3. skekum Customer

    Thanks Eric,

    I'll hit the books over the coming week and see how i go.

    Glenn

Share This Page