Professional CodeIgniter, Thomas Myer
Chapter 8: Last-Minute Upgrades
239
The
getAllPages()
function extracts all pages from the Pages database table.
function getAllPages(){
$data = array();
$Q = $this-
>
db-
>
get(`pages');
if ($Q-
>
num_rows()
>
0){
foreach ($Q-
>
result_array() as $row){
$data[] = $row;
}
}
$Q-
>
free_result();
return $data;
}
The
addPage()
function allows you to add a record to the Pages database table.
function addPage(){
$data = array(
`name' =
>
$_POST[`name'],
`keywords' =
>
$_POST[`keywords'],
`description' =
>
$_POST[`description'],
`status' =
>
$_POST[`status'],
`path' =
>
$_POST[`path'],
`content' =
>
$_POST[`content']
);
$this-
>
db-
>
insert(`pages', $data);
}
Please note that in this instance, as in any CodeIgniter situation dealing with POST data, you can use
$this-
>
input-
>
post()
instead of
$_POST
. The main difference? The CodeIgniter function will
return FALSE (Boolean) if the item does not exist or contains no data.
The
updatePage()
function lets you update a record in the Pages database table.
function updatePage(){
$data = array(
`name' =
>
$_POST[`name'],
`keywords' =
>
$_POST[`keywords'],
`description' =
>
$_POST[`description'],
`status' =
>
$_POST[`status'],
`path' =
>
$_POST[`path'],
`content' =
>
$_POST[`content']
);
$this-
>
db-
>
where(`id', $_POST[`id']);
$this-
>
db-
>
update(`pages', $data);
}
c08.indd 239
c08.indd 239
6/10/08 5:37:25 PM
6/10/08 5:37:25 PM