Get the selected id in a content

Asked

Viewed 30 times

1

I am developing a PHP forum to train myself and then I have the following situation, a content on the index page that has the categories created, so that as a category is selected, all the topics created within it are arranged, but I have a problem to capture the $id, because whenever I take the global variable and play in SELECT returns the same value, which would be the first of the table. I hope I’ve been clear, thanks friends. Code:

Part of the index showing the categories:

**

<div id="content">
            <?php
                include_once('server.php');
                $sql = "SELECT * FROM genero ORDER BY titulo_genero ASC";
                $res = mysqli_query($db,$sql) or die(mysqli_error());
                $cat = "";

                if(mysqli_num_rows($res) > 0){

                    while($row = mysqli_fetch_assoc($res)){
                        $id_view = $row['id'];
                        $titulo = $row['titulo_genero'];    
                        $descricao = $row['desc_genero'];
                        $cat .= "<a href='view.php' class='cat_links'>".$titulo." - <font size='-1'>".$descricao."</a>";
                    }   
                    echo $cat;  
                }else{
                    echo "<p>Nenhum gênero inserido ainda!</p>";
                }
            ?>
        </div>

View topics by category

$sql = "SELECT
            id,
            titulo_genero,
            desc_genero
        FROM
            genero
        WHERE
            id = '$id_view'";


$result = mysqli_query($db,$sql);

if(!$result)
{
    echo 'Não foi possível acessar a categoria desejada.' . mysqli_error($db);
}
else
{
    if(mysqli_num_rows($result) == 0)
    {
        echo 'O genêro não existe.';
    }
    else
    {
        //display category data
        while($row = mysqli_fetch_assoc($result))
        {
            echo '<h2>Topicos em ′' . $row['titulo_genero'] . '′ </h2>';
        }

        //roda a query para os tópicos
        $sql = "SELECT  
                    topic_id,
                    topic_subject,
                    topic_date,
                    topic_cat
                FROM
                    topics
                WHERE
                    topic_cat = '$id_view'";


        $result = mysqli_query($db,$sql);

        if(!$result)
        {
            echo 'Não foi possível listar os tópicos, tente de novo.';
        }
        else
        {
            if(mysqli_num_rows($result) == 0)
            {
                echo 'Nenhum tópico foi criado nessa categoria ainda.';
            }
            else
            {
                //preparando a tabela
                echo '<table border="1">
                      <tr>
                        <th>Tópico</th>
                        <th>Criado em</th>
                      </tr>'; 

                while($row = mysqli_fetch_assoc($result))
                {               
                    echo '<tr>';
                        echo '<td class="input-group">';
                            echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><h3>';
                        echo '</td>';
                        echo '<td class="input-group">';
                            echo date('d-m-Y', strtotime($row['topic_date']));
                        echo '</td>';
                    echo '</tr>';
                }
            }
        }
    }
}
  • In this case, when I call $id_view, I wanted to get the id that the user selected within the content, but I don’t know how to do that. If I just call $id_view I will always access it, which is the first that was inserted in the bank

1 answer

2


a Dato may be passed by SESSION[] the cookie. but in that case you don’t know what to save in If the coockie and the salutation would put the category by the url and pick it up again on the page view

$cat .= "<a href='view.php?categoria=".$id_view."' class='cat_links'>".$titulo." - <font size='-1'>".$descricao."</a>";

and in the view.php page Voce picks up the Dato that is in the url in the following mode

/////////////

if(isset($_GET['categoria']))
{
    $id_view = $_GET['categoria'];
    $sql = "SELECT
            id,
            titulo_genero,
            desc_genero
        FROM
            genero
        WHERE
            id = '$id_view'";
     $result = mysqli_query($db,$sql);

}
////////////

if(!$result)
{
    echo 'Não foi possível acessar a categoria desejada.' . mysqli_error($db);
}
else
{
    if(mysqli_num_rows($result) == 0)
    {
        echo 'O genêro não existe.';
    }
    else
    {
        //display category data
        while($row = mysqli_fetch_assoc($result))
        {
            echo '<h2>Topicos em ′' . $row['titulo_genero'] . '′ </h2>';
        }

        //roda a query para os tópicos
        $sql = "SELECT  
                    topic_id,
                    topic_subject,
                    topic_date,
                    topic_cat
                FROM
                    topics
                WHERE
                    topic_cat = '$id_view'";


        $result = mysqli_query($db,$sql);

        if(!$result)
        {
            echo 'Não foi possível listar os tópicos, tente de novo.';
        }
        else
        {
            if(mysqli_num_rows($result) == 0)
            {
                echo 'Nenhum tópico foi criado nessa categoria ainda.';
            }
            else
            {
                //preparando a tabela
                echo '<table border="1">
                      <tr>
                        <th>Tópico</th>
                        <th>Criado em</th>
                      </tr>'; 

                while($row = mysqli_fetch_assoc($result))
                {               
                    echo '<tr>';
                        echo '<td class="input-group">';
                            echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><h3>';
                        echo '</td>';
                        echo '<td class="input-group">';
                            echo date('d-m-Y', strtotime($row['topic_date']));
                        echo '</td>';
                    echo '</tr>';
                }
            }
        }
    }
}

hope I’ve helped

  • Eddy, thank you so much! That’s simple right, I still have a lot to learn.... It worked perfectly

  • a pleasure to help you, we all have something to learn

Browser other questions tagged

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