Sveiki, nagrineju knyga php 5 pradmenys ir ten yra pavyzdis duomenu isgavimas is db ir kaip ikelti naujus duomenys i db privalumas tas kad kategorija galima pasirinkti is kitos duomenu bazes, duomenys isgauti isgauna, o kai ikelineji nauja eilute categorijas duoda pasirinkti is kitos bd bet ikleti neikelia. codas:
perziura.php
<?php
$link = mysql_connect("localhost","root","121212");
mysql_select_db("mysql");
//tikrinama ar vartotojas nori istrinti irasa ir triname , jaigu taip
if ($_GET['action'] =='delete')
{
$help_topic_id = (int) $_GET['help_topic_id'];
mysql_query("DELETE FROM help_topic WHERE help_topic_id= $help_topic_id");
}
//Isrenkama 10 pirmu irasu
$result=mysql_query
("
SELECT help_topic.name AS topic_name,
help_category.name AS category_name,
help_topic.example,
help_topic.help_topic_id
FROM help_topic
LEFT JOIN help_category
ON help_category.help_category_id = help_topic.help_topic_id where help_topic.name like '%aaa%'
LIMIT 100
");
//SUDAROMAS MASYVAS
while ($row = mysql_fetch_array($result))
{
$rows[]=$row;
}
//isvedama i html lentele
echo toHTML($rows);
//FUNKCIJA NAUDODAMA PATEIKTA MASYVA SUFORMUOJA HTML LENTELE
function toHTML($rows)
{
$buffer = "<table border ='1'>";
$buffer .="<tr><th>Topic(<a href='add.php'>Naujas[/url])</th><th>Category</th><th>Example</th><th> </th></tr>";
if (count($rows))
{
foreach ($rows as $row)
{
$buffer .=sprintf("<tr><td><a href='perziura.php?help_topic_id=%d'>
%s[/url]</td>", $row['help_topic_id'], $row['topic_name']? $row['topic_name']:"n/a");
$buffer .=sprintf("<td>%s</td>", $row['category_name']?$row['category_name']:"n/a");
$buffer .=sprintf("<td>%s</td>", $row['example']?"Yes":"No");
$buffer .=sprintf("<td><a href='?action=delete&help_topic_id=%d'>
Delete[/url]</tr>", $row['help_topic_id']);
}
}
else
{
$buffer .="<tr><td colspan='4'>irasu nera</td></tr>";
}
$buffer .="</table>";
return $buffer;
}
mysql_close();
?>
add.php
<?php
$link=mysql_connect('localhost','root','121212');
mysql_select_db('mysql');
if ($_POST['action'] == "add")
{
mysql_query(sprintf("insert into help_topic (name, url, help_category_id, description, example)
values ('%s','%d','%s','%s','%s')",$_POST['name'],$_POST['url'],$_POST['help_category_id'], $_POST['description'],$_POST['example']));
header('Location: perziura.php');
}
function categoriesToHtml($categories)
{
foreach ($categories as $category)
{
$buffer .= sprintf("<option value='%d'>%s</option>", $category[0],$category[1]);
}
return $buffer;
}
function getCategories($categories = array(), $help_category_id = 0, $space ="")
{
if ($help_category_id !=0)
$space .= ' ';
$result = mysql_query("select * from help_category where parent_category_id = $help_category_id order by name");
while ($row=mysql_fetch_array($result))
{
$categories[] = array($row['help_category_id'], $space . substr($row['name'], 0, 27));
$categories = getCategories($categories, $row['help_category_id'], $space);
}
return $categories;
}
?>
<form method="POST">
<input type="text" maxlength="64" name="name" size="30"/> - name
<input type="text" name="url" size="30"/> - url
<select name="help_category_id">
<?php echo categoriesToHtml(getCategories())?>
</select> - category
Description:
<textarea rows="5" cols="27" name="description"></textarea>
Example:
<textarea rows="5" cols="27" name="example"></textarea>
<input type="submit" value="Ikelti">
<input type="hidden" name="action" value="add">
</form>
<a href="perziura.php">Atgal[/url]
where help_topic.name like '%aaa%' cia kai ivedu name pvz laaabas, arba aaarbata kad rodytu tik kur trys aaa