Does not show category

Asked

Viewed 46 times

-1

My blog is showing the post normally, minus the category part. I don’t know what else to do to fix this problem. Could someone please give me a little push?

$resultado = mysql_query($consulta, $conn) or die(mysqli_error());
$linhas = mysql_fetch_assoc($resultado);

Below shows part of the code:

                          <?php do { ?>
                            <div class="post post-row">
                                <a class="post-img" href="blog-post.html"><img src="Admin/app/webroot/files/post/imagem1/<?php echo $linhas['imagem1_dir']; ?>/<?php echo $linhas['imagem1']; ?>"></a>
                                <div class="post-body">
                                    <div class="post-meta">
                                        <a class="post-category" href="category.html"><?php echo $linhas['categoria']; ?></a>
                                        <span class="post-date"><?php echo utf8_encode($linhas['data']); ?></span>
                                    </div>
                                    <h3 class="post-title"><a href="blog-post.html"><?php echo utf8_encode($linhas['titulo_principal']); ?></a></h3>
                                    <p><?php echo utf8_encode($linhas['texto_inicial']); ?></p>
                                </div>

                            </div>
                            <?php } while ($linhas = mysql_fetch_assoc($resultado)); ?>
                        </div>

But only this part does not show, that it is precisely the category:

<a class="post-category" href="category.html"><?php echo $linhas['categoria']; ?></a>

I have two tables in the database, a call Posts that has categoria_id. Already the table Categories, has id and category. Someone who can help me? Thank you!

  • Please show the query sql in the database.

  • I think this is the part: $query = "SELECT * FROM posts ORDER BY id DESC"; Sorry if I’m wrong, I’m still studying mysql. When do I put 'categoria_id' (where you only have category) <?php echo $lines['category']; ? > appears the right ids, but the category name does not show.

  • If you want to display data from the two tables then you will have to do an INNER JOIN between the two tables through the category id field common to both tables.

1 answer

1


You should understand mysql and how to make queries relating tables. In your table of posts you must have a field with the category index for you to achieve this relationship. For example, in the table of posts and categories you would have an id_category field. Then you would have sql:

SELECT posts.*,categorias.categoria FROM posts
INNER JOIN categorias ON (posts.id_categoria = categorias.id_categoria) ORDER BY id DESC
  • Got it. Thanks so much for your help.

  • Arrange. If the answer has solved your problem the ideal is for you to mark as a solution to help the community.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.