Loop to show all news

0

I'm trying to make a backoffice for my website so I can post news! Does anyone know how to loop to show each of the rows in my database? Thanks to those who help!

Note: I'm new to PHP and Mysql         

date_default_timezone_set('Europe/Lisbon');
$date = date("Y-m-d H:i:s");

// Ligação
$conn = mysqli_connect($servername, $username,"",$dbname);


$querypesquisa= "select * from praticar where titulo=titulo and texto=texto 
 and nome=nome";
$querypesquisa_run = mysqli_query($conn,$querypesquisa);
  ?>
 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?php
    if(mysqli_num_rows($querypesquisa_run)>0)
    {
        // Loop vai ter a mesma quantidade que o nº de rows // 
          {
        echo '
    <h1>Titulo</h1>
    <div>Conteudo</div>
    <p></p>
    <footer>MOSTRAR AQUI A VARIÁVEL DE DATA</footer><footer>Nome</footer>'
         ; }
}
else
{
 echo 'Não existem noticias';
}

?>
</body>

    
asked by anonymous 16.05.2018 / 16:18

1 answer

0

First, there in your query, you do not need the name = name, title = title .. For this it is only applicable if you want to look for some specific data in the database, eg:

$nome = "AlmostDone";
$query = "SELECT * FROM praticar WHERE nome = '$nome' ";

In the above example syntax, you are using WHERE to show you only data that the name equals $ name variable (in which we assign the value of AlmostDone), so basically the database reads like this:

"SELECT ALL OF WHERE to practice WHERE name is EQUAL to AlmostDone"

But in your case, do not need to put name = name, just put:

$querypesquisa= "select * from praticar";

That's the equivalent of your search, it will list everything on your table, UNLESS you select a news item equivalent to a category (for example: only list news involving sport), but for this you have another sea of tasks before, and as a beginner, I advise you to read a little more about using MYSQL and PHP .

A good channel for this is the Celke , you have video lessons that will increase your knowledge.

/ p>

But let's go to your problem, there are countless invalid things in your code, from the loop (which is done while while fetching data from the database) and various items out of place (from " ; " outside of your position).

I have set up your code to list news .. I left with because I imagine that the user has to click on it to open the news .. But if you are a beginner, better start with more calm .. Simpler things that do not involve both set of variables and ids from side to side.

Now, there are several ways to delete a record, I'll give you an easier one. But for this you need the bootstrap for this method (I'll show you a modal confirmation).

Follow the code below with the delete button, modal and jquery all right for you .. Be careful, the button is already in operation, register a test news if you want to test its operation:

<?php

date_default_timezone_set('Europe/Lisbon');

$date = date("Y-m-d H:i:s");

// Ligação
$conn = mysqli_connect($servername, $username,"",$dbname);

$querypesquisa= "select * from praticar";

$querypesquisa_run = mysqli_query($conn, $querypesquisa);
  ?>
 <!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <h1>Titulo</h1>
    <div>Conteudo</div>

    <?php
    while($row = mysqli_fetch_assoc($querypesquisa_run)){
    ?>
    <p>
      <?php echo $row['id'] ?>-
        <a href="#">
        <?php echo $row['nome'] ?>
        </a>
   </p>
    <footer><?php echo $row['data'] ?></footer>
<button type="button" class="btn btn-sm btn-danger" href="#"
    data-toggle="modal" data-target='#deleta_<?php echo $row['id'] ?>'>
    <i class="fas fa-trash-alt"></i> Apagar</button>

<!-- Modal para deletar o equipamento !-->
        <div class="container">
        <div class="modal fade" id='deleta_<?php echo $row['id'] ?>' role="dialog">
          <div class="modal-dialog modal-md">
            <div class="modal-content">
              <div class="modal-body">
                <p> Você tem certeza que deseja apagar o <?php echo $row['nome'] ?>?</p>
              </div>
              <div class="modal-footer">
                    <a href="teste2.php?id=<?php echo $row['id'] ?>"
                       class="btn btn-danger" id="delete">
                        <i class="fas fa-eraser"></i> Apagar Registro</a>
                    <button type="button" data-dismiss="modal" class="btn btn-primary">
                        <i class="fas fa-times"></i> Cancelar</button>
              </div>
            </div>

          </div>
        </div>
      </div>
  <?php }; ?>

 </body> 

</html>

Now, notice this line:

<a href="teste2.php?id=<?php echo $row['id'] ?>"..

You need to create another file to exclude (if you want another name, put it in href by replacing teste2.php with another name you want to put on the delete page. do not delete anything! )

test2.php :

<?php
// Ligação
$conn = mysqli_connect($servername, $username,"",$dbname);

$id = $_GET['id'];

$query_volta = "SELECT * FROM praticar";
$query = mysqli_query($conn, $query_volta);
$row_volta = mysqli_fetch_assoc($query);
$id_volta = $row_volta['id'];

  //Cadastro de equipamentos
  $query_deleta = "DELETE FROM praticar WHERE id=$id";
  $resultado_deleta = mysqli_query($conn, $query_deleta);
  $linhas = mysqli_affected_rows();

  if(mysqli_affected_rows() != 0){
    echo "<script type=\"text/javascript\">
            alert(\"Equipamento apagado com sucesso.\")";
    header("Location: index.php");
  }else{
    echo 'Erro ao apagar o registro!';
    header("Location: index.php");
  }
?>
    
16.05.2018 / 17:02