php - How can I remove this duplicate code in my code while still getting what I is needed

800

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 >' .$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 >
<div ></div>
<div >
<div >


<?php
  echo '<br><br>';
    echo '<div >'.$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" 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 >' .$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 >
<div ></div>
<div >
<div >


<?php
  echo '<br><br>';
    echo '<div >'.$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" style="line-height: 12px;
   margin-top: 1px;
   margin-right: 2px;
   position:absolute;
   top:0;
   right:0;
   width: 15px;
   height:15px;">
</div>
</div>

<?php
}?>
108

Answer

Solution:

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. Usingmysqli_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
}

Usingmysqli_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 forMYSQLI_ASSOC results, since you're using$row['id'] and$row['note'].mysqli_fetch_all() defaults to usingMYSQLI_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 toforeach, 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 thatforeach 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 ...
}

People are also looking for solutions to the problem: php - Pagination Not Working With filter. Click page 2(with filter) will return to Page 2 with no filter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.