Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Created December 16, 2015 10:31
Show Gist options
  • Save VictorFursa/2049c569b7052d99485b to your computer and use it in GitHub Desktop.
Save VictorFursa/2049c569b7052d99485b to your computer and use it in GitHub Desktop.
<?php
include_once("connectDatabases.php");
if(isset($_POST['add_categories'])) {
$parent_id = $_POST['parent_id'] ;
$name = $_POST['add_categories'];
mysql_query("INSERT INTO categories (parent_id, name) VALUES('$parent_id','$name')");
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
function get_child($id, $array){
$childrens = array();
foreach($array as $key){
if($key['parent_id'] == $id){
$arr = array(
"name" => $key["name"],
"childrens" => get_child($key['id'], $array)
);
$childrens[] = $arr;
}
}
return $childrens;
}
$result = mysql_query("SELECT * FROM categories ");
$array = array();
while ($categories = mysql_fetch_array($result)) {
$array[] = $categories;
} ?>
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<title>Добовление товаров</title>
</head>
<body>
<h1 align="center">Работа с Mysql <br> (")(o_O)(")</h1>
<hr>
<form method="POST" action="magazin.php">
<h1>1</h1>
<select name="appliances">
<?php
$result = mysql_query("SELECT * FROM appliances ");
while ($res = mysql_fetch_array($result)){
echo "<option "." value='".$res['id']."'>".$res['categoria']."</option>";
}?>
</select>
<em>Добавить товар в категорию:<input type="text" name="product"> <br><br>
<input type="submit" value="Просмотреть" name="submit"/>
<input type="submit" value="Добавить" name="add" />
<hr><br>
<div>
<input type="submit" value="Добавить в категорию" name="add_cat"/>
</div>
<br><hr>
</form>
<br>
<?php
if (isset ($_POST['submit'])) {
if (isset($_POST['appliances'])) {
$query = mysql_query("SELECT * FROM appliances JOIN product ON product.appliances_id = appliances.id");
while ($row = mysql_fetch_array($query)) {
if ($row['appliances_id'] == $_POST['appliances']) {
echo $row['product_name']."<br>";
}
}
}
}
if(isset($_POST['add'])) {
if (isset($_POST['product'])) {
$product = $_POST['product'];
if (isset($_POST['appliances'])) {
$id = $_POST['appliances'];
mysql_query("INSERT INTO product(appliances_id,product_name) VALUES('$id','$product') ");
echo " в категорию успешнно добавлен товар: <b>$product";
}
}
}
echo "<h1> рекурсия </h1>";
$childrens= array();
$childrens[] = get_child(0,$array);
echo "<pre>";
print_r($childrens);
echo "</pre>";
?>
<div>
<h1>2</h1>
<label for="add_categories">Добовление новой категории </label>
<input id="add_categories" type="text" name="add_categories">
</div>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment