Skip to content

Instantly share code, notes, and snippets.

@lanior
Created December 17, 2010 21:55
Show Gist options
  • Save lanior/745772 to your computer and use it in GitHub Desktop.
Save lanior/745772 to your computer and use it in GitHub Desktop.
body {
padding: 10px;
}
Ext.onReady(function(){
Ext.QuickTips.init();
var
store = new Ext.data.JsonStore({
autoLoad: true,
remoteSort: true,
sortInfo: {
field: 'time',
direction: 'DESC'
},
url: "news.php?action=list",
root: 'news',
idProperty: 'id',
totalProperty: 'count',
fields: ['content', {name: 'time', type: 'date', dateFormat: 'timestamp'}]
}),
combo = new Ext.form.ComboBox({
id: "perpage",
name : 'perpage',
width: 40,
store: new Ext.data.ArrayStore({
fields: ['id'],
data : [
['10'],
['20'],
['30']
]
}),
mode : 'local',
value: '10',
listWidth : 40,
triggerAction : 'all',
displayField : 'id',
valueField : 'id',
editable : false,
forceSelection: true
}),
bbar = new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true,
items: [
"-",
"Per page: ",
combo
],
displayMsg: 'Displaying articles {0} - {1} of {2}',
emptyMsg: "No articles found"
}),
grid = new Ext.grid.GridPanel({
title: 'Articles',
store: store,
renderTo: "grid",
columns: [
{ id: 'content', header: "Content", width: 180, dataIndex: 'content', sortable: true },
{ header: "Date", width: 200, dataIndex: 'time', sortable: true }
],
autoExpandColumn: 'content',
width: 600,
height: 500,
loadMask: true,
columnLines: true,
bbar: bbar
});
combo.on('select', function(combo, record) {
bbar.pageSize = parseInt(record.get('id'), 10);
bbar.doLoad(bbar.cursor);
}, this);
var form = new Ext.FormPanel({
labelWidth: 75,
url: 'news.php?action=add',
frame: true,
title: 'Add new article',
bodyStyle:'padding:5px 5px 0',
width: 600,
waitMsgTarget: true,
hideLabels: true,
defaultType: 'textfield',
items: [{
xtype: 'htmleditor',
id: 'content',
height: 200,
width: '100%'
}
],
renderTo: "form",
buttons: [{
text: 'Add',
handler: function() {
form.getForm().submit({url:'news.php?action=add', waitMsg: 'Saving Data...'});
}
}]
});
});
<?php
error_reporting(E_ALL);
function get_param($name, $default = '')
{
if (isset($_REQUEST[$name]))
{
$result = $_REQUEST[$name];
return get_magic_quotes_gpc() ? stripslashes($result) : $result;
}
return $default;
}
try
{
$db = new PDO('sqlite:news.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex)
{
die('DB error');
}
try
{
$db->exec('CREATE TABLE news (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time INTEGER,
content TEXT)
');
}
catch (PDOException $ex)
{
}
$action = get_param('action');
if ($action == 'load')
{
}
else if ($action == 'add')
{
$content = get_param('content');
if (!empty($content))
{
$q = $db->prepare("INSERT INTO news (time, content) VALUES(?, ?)");
$q->bindValue(1, time(), PDO::PARAM_INT);
$q->bindValue(2, $content, PDO::PARAM_STR);
$q->execute();
}
exit;
}
else if ($action == 'list')
{
$dir = get_param('dir') == 'DESC' ? 'DESC' : 'ASC';
$limit = intval(get_param('limit'));
if ($limit < 10) $limit = 10;
if ($limit > 30) $limit = 30;
$start = intval(get_param('start'));
if ($start < 0) $start = 0;
$sort = get_param('sort');
if (!in_array($sort, array('content', 'time')))
{
$sort = 'content';
}
$q = $db->prepare("SELECT id, time, content FROM news ORDER BY {$sort} {$dir} LIMIT {$start}, {$limit}");
$q->execute();
$result = array();
die(json_encode(array(
'news' => $q->fetchAll(PDO::FETCH_ASSOC),
'count' => $db->query('SELECT COUNT(*) FROM news')->fetch(PDO::FETCH_COLUMN, 1)
)));
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>News Page</title>
<link rel="stylesheet" type="text/css" href="resources/css/ext-all.css" >
<link rel="stylesheet" type="text/css" href="news.css" >
<script type="text/javascript" src="ext-base.js"></script>
<script type="text/javascript" src="ext-all-debug.js"></script>
<script type="text/javascript" src="news.js"></script>
</head>
<body>
<div id="grid"></div>
<div id="form"></div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment