Hello, I am currently doing a project and I need to create a system of categories/tags per publication. I got it to work using the Serialize method but I would like the selected categories to be displayed in my blog posts, because when I log in <p class="cap-title"><?php echo $post['topic_id']?></p>
I get the following on the web: a:2:{i:0;s:2:"37 ";i:1;s:2:"36";}
I was told to use Unserialize but I don't quite understand where to add it.
Is using this method to create the categories reliable? Please help!.
code: topics.php
if (isset($_POST['add-post'])) {
$_POST['topic_id']=serialize($_POST['topic_id']);
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = "../../images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result){
$_POST['image'] = $image_name;
} else {
array_push($errors, "¡Algo fallo al subir la imagen!");
}
} else {
array_push($errors, "¡Necesitas subir una imagen!");
}
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Post creado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id']; // array('test','test2')
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
$id = $post['id'];
$title = $post['title'];
$body = $post['body'];
$topic = $post['topic_id'];
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Post eliminado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$_POST['topic_id']=serialize(array_values($_POST['topic_id']));
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Post actualizado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id'];
}
}
?>
images:
The answer to that specific question is:
In short, when you apply
unserialize()
you get a fix:To display the categories, ideally you would have an associative array
id_categoria => 'nombre_categoria'
and just loop through:The answer to this is purely based on opinions and here is mine: It is as reliable as you can and know how to handle it, for example: you should consider how to maintain consistency in the catalog of categories, if one is deleted, you have to verify that it is not being used in some publication.
There will be those who tell you that you must create a separate table to establish the relationships and, probably, it is the most appropriate to always program based on good practices.