0

I'm currently working on a website project with a blog portal, and I have created an admin panel for its management. However, I'm having trouble with a particular condition in my code. The website is supposed to change the related posts based on the selected theme in the URL. For example, if the URL is projeto_01_Estudo/blog/esportes, the posts should be related to sports. Unfortunately, this functionality is not working as intended.

Another things in my code it's working, for example the select from form is working, is already change the url but don't change the post.

And the PHP show me an error:

Notice: Undefined index: nome in C:\xampp\htdocs\FULL STACK\CURSO DESENVOLVIMENTO WEB COMPLETO\PROJETOS\projeto_01_Estudo\pages\blog.php on line 76 Visualizando todos os Posts

Notice: Undefined index: nome in C:\xampp\htdocs\FULL STACK\CURSO DESENVOLVIMENTO WEB COMPLETO\PROJETOS\projeto_01_Estudo\pages\blog.php on line 82

So, I can't resolve that :(

<?php 

    $url = explode('/',$_GET['url']);
    if(!isset($url[2]))
    
    {
        if (isset($url) && is_array($url) && isset($url[1])) {
            $categorias = MySql::conectar()->prepare("SELECT * FROM `tb_site.categorias` WHERE slug = ?");
            $categorias->execute(array(@$url[1]));
            $categorias = $categorias->fetch();   
        }

   

?>

<section class="header-noticias">
    <div class="center">
    <h2><i class="fa fa-bell"></i></h2>
    <h2>Acompanhe nossos últimos posts de <b>blog</b></h2>
    </div><!--center-->
</section><!--header-noticias-->

<section class="container-portal">
    <div class="center">
        <div class="sidebar">
            <div class="content-sidebar">
            
                <h3> <i class="fa fa-search"></i> Realizar uma busca</h3>
                <form action="">
                    <input type="text" name="parametro" placeholder="O que deseja procurar" required>
                  <i class="fa fa-search"></i>  <input type="submit" name="buscar"  value="pesquisar">  </input>
                </form>        
            </div><!--content-sidebar-->
            <div class="content-sidebar">
            
                    <h3> <i class="fa fa-list-ul"></i> Selecione a categoria:</h3>
                    <form>
                        <select name="categoria">
                        <option value="" disabled selected="">Todas as categorias</option>
                            <?php
                                $categorias = MySql::conectar()->prepare("SELECT * FROM `tb_site.categorias` ORDER BY order_id ASC");
                                $categorias->execute();
                                $categorias = $categorias->fetchAll();
                                foreach ($categorias as $key => $value) {
                            ?>
                            <option <?php if($value['slug'] == @$url[1]) echo 'selected' ?> value="<?php echo $value['slug']?>">
                            <?php echo $value['nome']; ?></option>
                            <?php  }  ?>
                        </select>
                    </form>        
            </div><!--content-sidebar-->    
            <div class="content-sidebar">
            
                    <h3> <i class="fa fa-user"></i> Sobre o autor:</h3>
                    <div class="autor-box-portal">
                        <div class="box-img-autor"></div><!--box-img-autor-->
                        <div class="texto-autor-portal text-center">
                            <?php
                                $infoSite = MySql::conectar()->prepare("SELECT * FROM `tb_site.config`");
                                $infoSite->execute();
                                $infoSite = $infoSite->fetch();
                            ?>
                            <h3 ><?php echo $infoSite['nome_autor'];?></h3>
                             <p><?php echo substr($infoSite['descricao'],0,'150').'...'?></p>  
                        </div><!--texto-autor-portal-->
                    </div><!--autor-box-portal-->
            </div><!--content-sidebar-->    
        </div><!--sidebar-->
            <div class="conteudo-portal">
                <div class="header-conteudo-portal">    
                    <?php 

                       
                        if($categorias['nome'] == ''){
                            echo '<h2>Visualizando todos os Posts</h2>';
                        }else{
                            echo '<h2>Visualizando Posts em <span>'.$categorias['nome'].'</span></h2>';
                        }
                        $query = "SELECT * FROM `tb_site.noticias` ";
                        if($categorias['nome'] != ''){
                            $categoria['id'] = (int)$categoria['id'];
                            $query.="WHERE categoria_id = $categoria[id]";
                        }
                        
                        $sql = MySql::conectar()->prepare($query);
                        $sql->execute();
                        $noticias = $sql->fetchAll();
                        

                        var_dump($query);
                        foreach ($categorias as $categorias) {
                            var_dump($categorias['nome']);
                        }
                        

                    ?>
                </div><!--header-->

                <?php
                    foreach($noticias as $key=>$value){
                        /*$sql = MySql::conectar()->prepare("SELECT * FROM `tb_site.noticias` WHERE titulo LIKE '%$parametro%' OR conteudo LIKE '%$parametro%'");*/
                        $sql = MySql::conectar()->prepare("SELECT `slug` FROM `tb_site.categorias` WHERE id = ?");
                        $sql->execute(array($value['categoria_id']));
                        $categoriaNome = $sql->fetch()['slug'];
                ?>
                    <div class="box-single-conteudo">
                        <h2><?php echo date('d/m/Y',strtotime($value['data']));?> - <?php echo $value['titulo']; ?></h2>
                        <p><?php echo substr(strip_tags($value['conteudo']),0,400).'...';?></p>
                        <a href="<?php echo INCLUDE_PATH;?>blog/<?php echo $categoriaNome?>/<?php echo $value['slug'];?>">Leia mais</a>
                    </div><!--box-single-conteudo-->
                   
                <?php } ?>

                <div class="paginator"  >
                    <a class="active-page" href="">1</a>
                    <a href="">2</a>
                    <a href="">3</a>
                    <a href="">4</a>
                </div><!--paginator-->
            </div><!--conteudo-portal-->
        
        <div class="clear"></div><!--clear-->
    </div><!--center-->

</section><!--container-portal-->

<?php
}else{ 
    include('blog_single.php');
 }?>

I made var_dump to see, what's returning, the result is that:

string(33) "SELECT * FROM `tb_site.noticias` "
string(17) "Marketing Digital"
string(8) "Esportes"
string(5) "Geral"

But when I pass the parameter on the url didn't work.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • `$_GET['url']` would get a URL query parameter named `url`, eg `index.php?url=some_value`. Is that the correct thing to be using? Are you sure you didn't mean to use `$_SERVER['REQUEST_URI']`? – Phil May 09 '23 at 23:38
  • 1
    `foreach ($categorias as $categorias)` -- you used the same variable name here, which ruins the old value of `$categorias`. – Tim Roberts May 09 '23 at 23:39
  • Just a general rule-of-thumb... don't re-use variable names – Phil May 09 '23 at 23:46
  • I was looking for my array and i find this: – Michael Patrick May 11 '23 at 16:57
  • rray(3) { [0]=> array(8) { ["id"]=> string(1) "1" [0]=> string(1) "1" ["nome"]=> string(8) "Esportes" [1]=> string(8) "Esportes" ["slug"]=> string(8) "esportes" [2]=> string(8) "esportes" ["order_id"]=> string(1) "1" [3]=> string(1) "1" } [1]=> array(8) { ["id"]=> string(1) "2" [0]=> string(1) "2" ["nome"]=> string(17) – Michael Patrick May 11 '23 at 16:58
  • Digital" [1]=> string(17) "Marketing Digital" ["slug"]=> string(17) "marketing-digital" [2]=> string(17) "marketing-digital" ["order_id"]=> string(1) "2" [3]=> string(1) "2" } [2]=> array(8) { ["id"]=> string(1) "3" [0]=> string(1) "3" ["nome"]=> string(5) "Geral" [1]=> string(5) "Geral" ["slug"]=> string(5) "geral" [2]=> string(5) "geral" ["order_id"]=> string(1) "3" [3]=> string(1) "3" } } – Michael Patrick May 11 '23 at 16:58

0 Answers0