1

I am currently working on a basic blog with php and mysql, it turns out that I am working on my blog posts and I would like the posts to show the categories or topics selected for that publication but it is only shown as code: a:2:{i:0;s:2:"37";i:1;s:2:"36";}.

For example, when creating a post about Water, say in the reading that it belongs to the category "Nature", but when placing the <?php echo $ post ['topic']?> the code is displayed: a:2:{i:0;s:2:"37";i:1;s:2:"36";}.

In the table of "posts" there is topic_id which is where the data of the selected categories are stored and are shown as {i: 0; s: 2:" 37 "; i: 1; s: 2:" 36 "; } but I would like the selected categories to be displayed in my blog posts.

code: posts.php

<?php 

include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');

$table = 'posts';
$categorias = selectAll('categorias');

$errors = array();
$id = '';
$title = '';
$body = '';
$topic_id = '';

$posts = selectAll($table);


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'] = '¡Anime 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'] = '¡Anime eliminado correctamente!';
    $_SESSION['type'] = 'success';
    header('location: ../../admin/posts/index.php');
    exit();
}

if (isset($_POST['update-post'])){
    $_POST['topic_id']=serialize($_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'] = '¡Anime 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'];
}
} 
?>                      

code: topics.php

<?php 

include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');

$table = 'categorias';

$errors = array();
$id = '';
$name = '';

$categorias = selectAll($table);


if (isset($_POST['add-topic'])) {
    $errors = validateCategoria($_POST);
    
    if (count($errors) === 0){
        unset($_POST['add-topic']);
        $topic_id = create($table, $_POST);
        $_SESSION['message'] = '¡Genero creado correctamente!';
        $_SESSION['type'] = 'success';
        header('location: ../../admin/topics/index.php');
        exit();
    } else {
        $name = $_POST['name'];
    }
}

if (isset($_GET['id'])){
    $id = $_GET['id'];
    $categoria = selectOne($table, ['id' => $id]);
}

if (isset($_GET['del_id'])){
    $id = $_GET['del_id'];    
    $count = delete($table, $id);
    $_SESSION['message'] = '¡Genero eliminado correctamente!';
    $_SESSION['type'] = 'success';
    header('location: ../../admin/topics/index.php');
    exit();
}

if (isset($_POST['update-topic'])){
    $errors = validateCategoria($_POST); 
    
    if (count($errors) === 0){
        $id = $_POST['id'];
        unset($_POST['update-topic'], $_POST['id']);
        $topic_id = update($table, $id, $_POST);
        $_SESSION['message'] = '¡Genero actualizado correctamente!';
        $_SESSION['type'] = 'success';
        header('location: ../../admin/topics/index.php');
        exit();        
    } else {
        $id = $_POST['id'];
        $name = $_POST['name'];
}
} 
?>                

            

code: posts.php

<?php include($_SERVER['DOCUMENT_ROOT'].'/app/controllers/posts.php');

if (isset($_GET['id'])){
    $post = selectOne('posts', ['id' => $_GET['id']]);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

  <!-- Custom Styles -->
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="/css/box.css">
  <link rel="stylesheet" href="/css/navbar.css">
  <link rel="stylesheet" href="/admin/css/fonts.css">
</head>


<body>
 <?php include('app/includes/header.php'); ?>

    <?php echo $post['topic_id']?>



</body>

</html>

As you can see in this image, this is shown with In the posts.php file but I would like the category names to appear

imagen

imagen

MrVictoxDB
  • 21
  • 2
  • 1
    I guess ["Is storing a delimited list in a database column really that bad?"](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) could be worth a read for you... – sticky bit Sep 20 '20 at 19:25

0 Answers0