Oto fragment rzeczywistego sprawdzonego kodu,
oczywiście jeśli jest bardzo dużo danych to lepiej użyć
prawdziwej bazy danych, ale w tamtym przypadku nie było
tylko kilkadziesiąt wierszy:
<?php
$labels['this'] = 'dystrybutorzy.php';
$labels['database'] = 'dystrybutorzy.db';
$labels['text'] = 'dystrybutorzy.txt';
$labels['nazwa'] = 'Nazwa Firmy';
$labels['adres'] = 'Adres';
$labels['miasto'] = 'Miasto';
$labels['kod'] = 'Kod pocztowy';
$labels['telefon'] = 'Numer Telefonu';
$labels['delete'] = 'Czy na pewno chcesz to skasować?';
$labels['cancel'] = 'Anuluj';
$labels['submit'] = 'OK';
require("javascript.php");
PostToJavaScript($labels);
if (!isset($_GET['action'])) $_GET['action'] = 'show';
if ($_GET['action']=='show') Show();
if ($_GET['action']=='edit') Edit();
if ($_GET['action']=='update') Update($_GET['id']);
if ($_GET['action']=='delete') Remove($_GET['id']);
if ($_GET['action']=='tekst') Editor();
if ($_GET['action']=='save') Save();
function Show(){
gora();
$plik = file($labels['database']); echo '<h2>Lista dystrybutorów:</h2>';
echo '<div style="height:25px;font-size:12px">'; echo '<a href="'.$labels['this'].'?action=edit">Dodaj nowego </a>'; echo ' '; echo '<a href="'.$labels['this'].'?action=tekst">Edytuj tekst poczatkowy</a>';
foreach ($plik as $row){
echo '<div style="height:25px;font-size:12px">'; echo '<a href="java script:Delete('.$row['id'].')">'; echo '<img src="../layout/delete.png" border="0" align="left" /></a>'; echo '<a href="'.$labels['this'].'?action=edit&id='.$row['id'].'">'; }
}
function InputText($name,$row=''){
echo '<table border="0" width="480px">'; echo '<tr><td width="150px" style="font-size:12px">'; echo '</td><td align="right">'; echo '<input name="'.$name.'" id="'.$name.'" value="'.$row[$name].'" style="width:320px">'; echo '</td></tr></table>'; }
function InputSubmit(){
echo '<table border="0" width="480px">'; echo '<tr><td align="right">'; echo '<input type="button" value="'.$labels['cancel'].'" style="width:80px" onClick="location=''.$labels['this
'].''">'; echo '<input type="submit" value="'.$labels['submit'].'" style="width:80px">'; echo '</td></tr></table>'; }
function Edit(){
gora();
echo '<h3>Edycja wpisu:</h3>'; echo '<form action="'.$labels['this'].'?action=update&id='.$_GET['id'].'" method="post">';
$plik = file($labels['database']); foreach ($plik as $row){
if ($_GET['id']==$array['id']) {
$found = $array;
break;
}
}
InputText('nazwa',$found);
InputText('adres',$found);
InputText('miasto',$found);
InputText('stan',$found);
InputText('kod',$found);
InputText('telefon',$found);
InputSubmit();
dol();
}
function Update($id){
if ($id) Remove($_POST['id']=$_GET['id'],false);
else $_POST['id'] = time();
$plik = fopen($labels['database'],'a'); Location($labels['this']);
}
function Remove($id,$back=true){
$plik = file($labels['database']); foreach ($plik as $row){
if ($id!=$array['id'])
$nowy[] = $row;
}
$plik = fopen($labels['database'],'w'); foreach ($nowy as $row)
if ($back) Location($labels['this']);
}
?>
Kod
<script language="javascript" type="text/javascript">
function Delete(id){
if (confirm(labels_delete))
location = labels_this+'?action=delete&id='+id;
}
</script>
<?php
function Editor(){
gora();
require('../lib/editor/xina.php');
?>
<form action="<?=$labels['this']?>?action=save" method="post">
<textarea id="content" name="content" style="width:720px;height:480px;"><?=$content?></textarea>
<input type="button" value="<?=$labels['cancel']?>" onClick="location=labels_this" style="width:80px">
<input type="submit" value="<?=$labels['submit']?>" style="width:80px" >
<?php
dol();
}
function Save(){
$plik = fopen($labels['text'],'w'); Location($labels['this']);
}
?>