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 ' ';
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 ' ';
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
}?>
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:
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:
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 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.