Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Build record table list from db with smarty and pear

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
danny36
Smarty Rookie


Joined: 14 Jul 2004
Posts: 27
Location: Italy - Arezzo (Tuscany)

PostPosted: Thu Jul 15, 2004 10:57 am    Post subject: Build record table list from db with smarty and pear Reply with quote

Hi all,
this is my script example for create a template it can show a table with list of record from database of movie directors.

This script use smarty for template engine, pear::db for abstraction database layer for connect to mysql database and pear::pager_sliding for generate records page.

Sorry for my bad english but I'm italian php web application developer and this is my first tutorial in this english forum.

Start tutorial:

1. create one database called "mydb" in your mysql

2. run this sql script for generate "registi" table in "mydb" database

Code:

USE mydb;


#
# Table structure for table 'registi'
#

DROP TABLE IF EXISTS `registi`;
CREATE TABLE `registi` (
  `registi_id` tinyint(3) unsigned NOT NULL auto_increment,
  `registi_nome` varchar(30) default NULL,
  `registi_cognome` varchar(30) default NULL,
  PRIMARY KEY  (`registi_id`),
  UNIQUE KEY `registi_id` (`registi_id`)
) TYPE=MyISAM;



#
# Dumping data for table 'registi'
#

INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("1", "terry", "gilliam");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("2", "joel", "coen");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("3", "david", "fincher");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("4", "frank", "darabont");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("5", "tim", "burton");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("6", "quentin", "tarantino");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("7", "stanley", "kubrick");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("8", "ridley", "scott");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("9", "michael", "moore");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("10", "danny", "boyle");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("11", "vincent", "cassel");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("12", "ron", "howard");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("13", "steven", "spielberg");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("14", "luc", "besson");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("15", "andy e larry", "wachowski");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("16", "peter", "jackson");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("17", "guy", "ritchie");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("18", "oliver", "stone");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("19", "robert", "zemeckis");
INSERT INTO `registi` (`registi_id`, `registi_nome`, `registi_cognome`) VALUES("20", "sam", "raimi");


3. create one .php file called "registi.php" and put this code

Code:

<?php
// FILE NAME: registi.php

require_once 'DB.php'; // require PEAR::DB classes
require_once 'smarty/Smarty.class.php'; //require SMARTY template engine classes
require_once 'Pager/Sliding.php'; // require PEAR::PAGER SLIDING classes

$smarty = new Smarty();
$smarty->force_compile = true;
$smarty->compile_check = true;
$smarty->debugging = false;

//START  Section for connect to mysql database with PEAR::DB classes
$db_user = 'root';      //mysql username
$db_pass = '';          //mysql password
$db_host = 'localhost'; //mysql hostname
$db_name = 'mydb';      //mysql my database name
$db_engine = 'mysql';   //database engine type must "mysql" for mysql database

$datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
$db = DB::connect($datasource);

if(DB::isError($db)) {
die($db->getMessage());
}
//END Section for connect to mysql database with PEAR::DB classes


$Query_Str_registi="SELECT * FROM registi ORDER BY registi_nome ASC";
$result_registi = $db->getAll($Query_Str_registi,DB_FETCHMODE_ASSOC);
if ($result_registi === false) die("failed");

$params_registi = array(
            'perPage'    => 4,
            'delta'      => 2,
            'append'     => true,
            'expanded'   => true,
            //'linkClass'  => 'testo',
            'urlVar'     => 'pageID_registi',
            'itemData'   => $result_registi
            
            );
         
$pager_registi = &new Pager_Sliding($params_registi);
$data_registi  = $pager_registi->getPageData();
$links_registi = $pager_registi->getLinks();

$smarty->assign('links_registi', $pager_registi->links);
$smarty->assign ("registi", $data_registi);

$smarty->display('registi.tpl');

?>


4. create one .tpl file called "registi.tpl" and put this code



Code:

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body>
<TABLE width="100%" border="0" cellspacing="5" cellpadding="0">
  <TR align="center" valign="top">
    <TD>
      <TABLE width="20%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
        <TR bgcolor="#666666">
          <TD colspan="3">Directors</TD>
        </TR>
        <TR bgcolor="#FFFFFF">
          <TD bgcolor="#CCCCCC">ID</TD>
          <TD bgcolor="#CCCCCC">Name</TD>
          <TD bgcolor="#CCCCCC">Surname</TD>
        </TR>
        {foreach from=$registi item=row}
        <TR bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
          <TD>{$row.registi_id}</TD>
          <TD>{$row.registi_nome}</</TD>
          <TD>{$row.registi_cognome}</TD>
        </TR>
       {/foreach}
      
    </TABLE>
     {$links_registi}
</body>
</html>
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP