0

I am trying to fetch members ratings from the database using ajax. I passed a function to the JSON, even though it returned a value in the function but it doesn't execute the for loop condition.

Here is my code, the loop failed to execute. What am I doing wrong?

    function mrate($irate) {
        $class = "fa-star star-filled";
        for ($i = 0; $i < 5; $i++) {
            if ($irate <= $i) {
                $class = "fa-star-o empty";
            }
            return '<i class="fa ' . $class . '"></i>';
        }
    }

    $perPage = 2;
    if (isset($_GET["page"]) && isset($_GET["page"])) {
        $page = $_GET["page"];
        $pid = $mysqli->real_string_escape($_GET["pid"]);
    } else {
        $page = 1;
        $pid = $mysqli->real_string_escape($_SESSION['pid']);
    };

    $startFrom = ($page - 1) * $perPage;
    $sqlQuery = "SELECT id, name,
        review, rating, added_date
        FROM review_rating
        where product_id = '$pid'
        ORDER BY id ASC LIMIT $startFrom, $perPage";
    $result = mysqli_query($mysqli, $sqlQuery);
    $paginationHtml = '';
    while ($row = mysqli_fetch_assoc($result)) {

        $img = '<img class="rounded-circle" width="50" src="' . $set['installUrl'] . 'assets/img/login.png" alt="' . $row["name"] . '"/>';
        $irate = $row['rating'];

        $paginationHtml .= '<div  class="product-review pb-4 mb-4 border-bottom">';
        $paginationHtml .= '<div class="d-flex mb-3">';
        $paginationHtml .= '<div class="media media-ie-fix align-items-center mr-4 pr-2">' . $img;
        $paginationHtml .= '<div class="media-body pl-3"><h6 class="font-size-sm mb-0">' . $row["name"] . '</h6>';
        $paginationHtml .= '<span class="font-size-ms text-muted">' . $row['added_date'] . '</span></div></div>';
        $paginationHtml .= '<div><div class="star-rating">' . mrate($irate) . '</div></div>';
        $paginationHtml .= '</div>';
        $paginationHtml .= '<p class="font-size-md mb-2">' . $row['review'] . '</p>';
        $paginationHtml .= '</div>';
    }
    $jsonData = array(
        "html" => $paginationHtml,
    );
    echo json_encode($jsonData);
5
  • 2
    Your for loop won't iterate more than once because of the return statement. Commented Mar 10, 2020 at 15:34
  • @RajdeepPaul, how could I return the value in the function without using retun? Commented Mar 10, 2020 at 15:39
  • Can you please explain what you are trying to do in mrate function, especially in that for loop? Commented Mar 10, 2020 at 15:42
  • @RajdeepPaul, I am trying to print out the number of rating per members. The star-icon appears 5 times. Out of the 5 stars, get number of rating per member which brings out the looping. Commented Mar 10, 2020 at 15:46
  • I have given an answer below to address your issue. Commented Mar 10, 2020 at 16:21

2 Answers 2

2

Replace your function mrate($irate) with this and try. You needed to concatenate the stars code to display it more than once.

function mrate($irate){
    $stars = '';
    for($i=0; $i<5; $i++){
        if($irate <= $i){
            $class = "fa-star-o empty";
        }else{
            $class = "fa-star star-filled";
        }
        $stars .= '<i class="fa '.$class.'"></i>';
    }
    return $stars;
}
Sign up to request clarification or add additional context in comments.

3 Comments

You could factor out the $stars .= ... part so that you don't repeat it in both branches of the if..else...
@NigelRen Thank you for the reminder, edited my answer.
Thanks to everyone who contributed. I appreciate you.
0

Assuming there's no fractional rating, you can do the following - Display all 5 stars but solid ones will represent the rating.

function mrate($irate){
    $class = '';
    for($i = 0; $i < 5; $i++){
        if ($irate <= $i) {
            $class .= '<i class="fa fa-star"></i>';
        } else {
            $class .= '<i class="fa fa-star-o"></i>';
        }
    }
    return $class;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.