/** * White information from XML files to database */ public function writeXMLInDB() { if ($this->isDerectory()) { $dirTree = $this->getXMLPaths($this->_dir_path); foreach ($dirTree as $value) { $xml_content = simplexml_load_file($value); foreach ($xml_content as $key => $value) { $is_exist = \models\Animal::getAnimals(array('name' => $value->name)); if ($is_exist) { //update $animal_id = key($is_exist); $animal = new \models\Animal($animal_id); $animal->setCategory($value->category); $animal->setSubcategory($value->subcategory); $animal->setName($value->name); $animal->setDescription($value->description); $animal->setEat($value->eat); $animal->save(); } else { //new animal $animal = new \models\Animal(); $animal->setCategory($value->category); $animal->setSubcategory($value->subcategory); $animal->setName($value->name); $animal->setDescription($value->description); $animal->setEat($value->eat); $animal->save(); } } } } }
<?php require_once '../config.php'; require_once ROOT_PATH . '/models/Database.php'; require_once ROOT_PATH . '/models/Animal.php'; if (!empty($_POST['searched_text'])) { //filter and validate searched data $searched_text = \models\Database::validateData($_POST['searched_text'], 'string|specialchars|strip_tags'); $params = array('search_name' => $searched_text); $searched_animals = \models\Animal::getAnimals($params, true); $search_result = ''; if (!empty($searched_animals)) { foreach ($searched_animals as $key => $data) { $search_result .= '<tr>'; $search_result .= '<td>' . $searched_animals[$key]['id'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['category'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['subcategory'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['name'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['description'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['eat'] . '</td>'; $search_result .= '<td>' . $searched_animals[$key]['date_created'] . '</td>'; $search_result .= '</tr>'; } } else { $search_result = '<div class="alert alert-danger">Search results not found.</div>'; } echo json_encode($search_result); } else { throw new Exception('Empty string given for seach'); }
<?php $all_animals = \models\Animal::getAnimals(array(), true); if (!empty($all_animals)) { ?> <div class="panel panel-default"> <div class="panel-heading"><h3>All current animals from database</h3></div> <div class="panel-body"> <p>Table show all of current inserted animals data in database</p> </div> <table class="table"> <thead> <tr> <th>#</th> <th>Category</th> <th>Subcategory</th> <th>Name</th> <th>Description</th> <th>Eat</th> <th>date_created</th> </tr> </thead> <?php foreach ($all_animals as $key => $data) { ?> <tr> <td><?php echo $all_animals[$key]['id']; ?> </td> <td><?php