简体   繁体   中英

How to display each row data in the database?

I'm having a hard time displaying the title and note on each row of my database. I want to display one row (with the title and note) after each from a form in a page heading to the displaying of row datas page.

This is my code below:

// Let's say that we have 4 rows of datas. In my code, I can only display the first row, because it keeps having the first row's data. This is because the form is in the first php file. Then after I submit the form, it's directed to this file, and it keeps getting the first row.

<?php $con=mysqli_connect("localhost","root","","task");?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>

<?php
$id=$row['id'];
 echo '&nbsp; &nbsp; &nbsp; &nbsp;';
    echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
?>
<?php
}?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="note" data-id="<?= $row['id'] ?>">
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">

<?php
   echo '<br><br>';

       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';
?>
</div>
</div>
<?php
}?>
<?php
<?php
function test_input($data) { 
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
} ?>

In your loop you're using mysqli_fetch_array , which returns an array with each element in that array containing the field value.

What you want is mysqli_fetch_assoc instead, this will return a hash which you can then use the way you're using it now.

Another thing is that you don't need to have 2 loops in there querying the database. And please indent your code, it makes it really hard for you and everyone else to read it.

Here is the updated/cleaned up version of your code. This has been tested, you can find a sample code with instructions to run on my Github here .

<?php
$con = mysqli_connect("localhost", "root", "", "task");
$results = mysqli_query($con, "SELECT * FROM note");

while ($row = mysqli_fetch_assoc($results)) {
  $id = $row['id'];
  echo '&nbsp; &nbsp; &nbsp; &nbsp;';
  echo '<button class="call_modal" data-id="' . $id . '" style="cursor:pointer;">'. $row['title'] . '</button>';
  ?>
  <div class="note" data-id="<?= $row['id'] ?>">
    <div class="modal">
      <div class="modal_close close"></div>
        <div class="modal_main">
          <?php
          echo '<br /><br />';
          echo '<div class="padding">' . $row['title'];
          echo '<br /><br /><br /><br />';
          echo $row['note'];
          echo '</div>'
          ?>
        </div>
      </div>
    </div>
  </div>
<?php
}
?>

<?php
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

The code below is not tested but it should be correct and give you a better idea of what you are doing right/wrong. I hope it helps..

<?php 
$con=mysqli_connect("localhost","root","","task");
$results = mysqli_query($con, "SELECT * FROM note");



while ($row = mysqli_fetch_array($results)) { //starting your data row loop


$id=$row['id'];// you are creating a variable here but not using it two lines down. 
 echo '&nbsp; &nbsp; &nbsp; &nbsp;';
 //YOUR OLD CODE echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
 echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $id . '</button>';// use the variable "$id" that you created here

// You dont need the next two lines, you already did a query and have the data loaded in the "$results" array
/*  $results = mysqli_query($con, "SELECT * FROM note"); 
 while ($row = mysqli_fetch_array($results)) */

?> // close the php tag if you are going to switch to html instead of "echoing html"
/* OLD CODE <div class="note" data-id="<?= $row['id'] ?>"> you were missing the "php" in the php tags */
<div class="note" data-id="<?php echo $id; ?>"> // corrected code
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">

<?php //switching back to php so create your open tag again...
   echo '<br><br>';

       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';// you dont NEED the '' before .$row....  unless you want it but its just essentially a blank string
?>
</div>
</div>
<?php
} // ending your "for each data row" here 
?>

<?php
//  PS you're not using this function anywhere unless its in ommited code?
function test_input($data) { 
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
} ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM