如何在我的代码中删除这个重复的代码,同时仍然得到我需要的东西

This first code below is the duplicate in my code :

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

I want to remove it while I still can get the data that I want. How can I be able to do it?

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


    echo '<button class="call_modal" style="cursor:pointer; width: 150px; height: 60px;">' .$row['title']. '</button>';
    echo "<a href='update.php?id=$id'>edit</a>";
    echo '&nbsp; &nbsp;';
    echo "<a href='delete.php?id=$id'>delete</a>";


}
?>

<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note">


<?php
   echo '<br><br>';
       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';
    echo '<br><br><br><br><br>';
   echo '<form action="upload.php" method="post" enctype="multipart/form-data">

        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
        </form>'
?>


<img src="x.png" class="close" style="line-height: 12px;
     margin-top: 1px;
     margin-right: 2px;
     position:absolute;
     top:0;
     right:0;
     width: 15px;
     height:15px;">
</div>
</div>

<?php
}?>

check this out if this is true:

<?php $con=mysqli_connect("localhost","root","","task");
 $results = mysqli_query("SELECT * FROM note");
 $rows = mysqli_fetch_all($results, MYSQLI_ASSOC);
 foreach ($rows as $row) {
     $id = $row['id'];


         echo '<button class="call_modal" style="cursor:pointer; width: 150px; height: 60px;">' .$row['title']. '</button>';
         echo "<a href='update.php?id=$id'>edit</a>";
         echo '&nbsp; &nbsp;';
         echo "<a href='delete.php?id=$id'>delete</a>";

}
?>


<?php foreach ($rows as $row) { ?>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note">


<?php
   echo '<br><br>';
       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';
    echo '<br><br><br><br><br>';
   echo '<form action="upload.php" method="post" enctype="multipart/form-data">

        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
        </form>'
?>


<img src="x.png" class="close" style="line-height: 12px;
     margin-top: 1px;
     margin-right: 2px;
     position:absolute;
     top:0;
     right:0;
     width: 15px;
     height:15px;">
</div>
</div>

<?php
}?>

If your goal is to not execute the same query twice you have to re-use the result. There are several ways to do that:

Reset the result

Example:

$results = mysqli_query("SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) {
    // do something
}

mysqli_data_seek($results, 0);
while ($row = mysqli_fetch_array($results)) {
    // do something else
}

Why this is necessary: mysqli_fetch_array updates the internal pointer in $results that tells it what the next row is. When the first loop finishes, that internal pointer is set to beyond the last row, to indicate there are no more rows available. When your second loop starts, that pointer is still set there so it will not have any results to loop through. Using mysqli_data_seek you can manually set the pointer back to the beginning.

Fetch the results once

Example:

$results = mysqli_query("SELECT * FROM note");
$rows = mysqli_fetch_all($results, MYSQLI_ASSOC);
foreach ($rows as $row) {
    // do something
}

foreach ($rows as $row) {
    // do something else
}

Using mysqli_fetch_all will retrieve all rows as an array. You can then loop over that array as many times as you want. I'm explicitly asking for MYSQLI_ASSOC results, since you're using $row['id'] and $row['note']. mysqli_fetch_all() defaults to using MYSQLI_NUM results, which would not allow you to do that.

AFAIK, this is the most common approach, but it has a downside: you're retrieving all results into memory. If your query has a lot of results or if your application is already using a lot of memory, this may be a problem.

Since you said you're new to foreach, here's a short explanation of how it is different from other looping methods:

foreach ($rows as $index => $row) {
}

// is the same as:
for ($index = 0; $index < count($rows); $index++) {
    $row = $rows[$index];
}

// or:
$index = 0;
while ($index < count($rows)) {
    $row = $rows[$index];
}

// and if you're not interested in it, you don't need to ask for the $index:
foreach ($rows as $row) {
}

The main difference is that foreach also works easily on non-numeric arrays. For example:

foreach ($row as $column => $value) {
    echo $column . ': ' . $value . '<br>';
    // id: 123
    // title: Note title
    // note: Lorem ipsum ...
}