-2

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>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";
            $output .= "</div></div></div>";
            return $output;
        }
    } else {
        return "No product yet";
    } // return $output;
}
D. Schreier
  • 1,700
  • 1
  • 22
  • 34
  • 7
    Do you have a question? – Strawberry Jul 06 '20 at 22:16
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Additionally the procedural interface has less rigorous error checking and reporting, frustrating debugging efforts. – tadman Jul 06 '20 at 22:20
  • Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Jul 06 '20 at 22:20
  • Right and I'm with @Strawberry here. I voted the question as "unclear". I'd of figured that others would have already voted as such, right? A bit of communication goes a long way. – Funk Forty Niner Jul 06 '20 at 23:29

1 Answers1

0

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>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";
            $output .= "</div></div></div>";
        }
    } else {
        $output = "No product yet";
    } // return $output;
    return $output;
}
EhsanT
  • 2,077
  • 3
  • 27
  • 31