view_products()
below displays only one data from my table product
which contains 20 data. Depending on where you place return $output;
either inside while loop
which displays the first data or outside the while loop
which displays the last data.
<?php echo view_products(); ?>
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password@edadmin";
$dbname = "estore";
$dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//Test if connection occurred,
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")");
}
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($products = mysqli_fetch_assoc($result)) {
$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<a href=" . $products['ProductImage'] . " data-lightbox=\"portfolio\" data-title=\"App 1\" class=\"link-preview\" title=\"Preview\"><i class=\"ion ion-eye\"></i></a>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p><a href=\"#\">" . $products['ProductName'] . " </a></p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
return $output;
}
} else {
return "No product yet";
} // return $output;
}
The reason is that you are resetting the content of $output
in the first line of your loop $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
So if you put the return at the end of the loop, in the first loop it will return the first record and exit the function, if you put it at the end of the function, in each loop $output
will be cleared and the content of that loop will only be written in $output
so at the end of the function you will only have the content of the last loop in $output
What you can to is to set $output
to an empty string and then just append everything in your loop. Also set the value of $output
in the else
block and then at the end return $output
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
$output = "";
while ($products = mysqli_fetch_assoc($result)) {
$output .= "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<a href=" . $products['ProductImage'] . " data-lightbox=\"portfolio\" data-title=\"App 1\" class=\"link-preview\" title=\"Preview\"><i class=\"ion ion-eye\"></i></a>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p><a href=\"#\">" . $products['ProductName'] . " </a></p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
}
} else {
$output = "No product yet";
} // return $output;
return $output;
}
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.