为什么我的函数不能在php循环中工作?

时间:2016-02-15 11:46:36

标签: php

我有审核功能,我需要在PHP while循环中运行。但是如果我在while循环中调用该函数,它会以某种方式无效。

但如果我手动将代码(评级功能)放入其中,那么它就可以正常工作。你能告诉我为什么吗 ?

这是功能代码:

function rating($star) {
    echo "<div class='my_all_star'>";
    $total = "";
    for ($i=1; $i <= $star; $i++) { 
        $total .= "<img src='../images/star.png'/> ";
    }
    for ($i=1; $i <= (5 - $star); $i++) { 
        $total .= "<img src='../images/star_null.png'/> ";
    }
    if($star > 1 ){
        $reviews = " Reviews";
    }else{ 
        $reviews = " Review";
    }
    $total .= ($star) . $reviews;
    return $total;
    echo "</div>";
}

更新:While循环:

<?php
$grq =  mysqli_query($conn, "SELECT tbl_reviews.*, tbl_users.FName, tbl_users.LName FROM tbl_reviews LEFT JOIN tbl_users ON tbl_reviews.reviewerID = tbl_users.UserID WHERE tbl_reviews.ProductID = '$pid' ");

while($allreviews = mysqli_fetch_array($grq)){

$review_text = inputvalid($allreviews['ReviewText']);
$review_date = inputvalid($allreviews['ReviewDate']);
$st_rating = (int) $allreviews['StarRating'];
$fname = inputvalid($allreviews['FName']);
$lname = inputvalid($allreviews['LName']);

      ?>
      <div class="single_main_reviews">
        <div class="col-sm-2 text-center rev_author_info">
          <img src="../images/avater-1.png" alt="" />
          <p>Reviewed by</p>
          <a href=""><?php echo $fname ." ". $lname; ?></a>

        </div>
        <div class="col-sm-10 rev_author_content">
          <div class="rev_author_content_head">
            <div class="">
              <?php 
               echo "<div class='my_all_star'>";
$total = "";
$star =  $st_rating;
for ($i=1; $i <= $star; $i++) { 
    $total .= "<img src='../images/star.png'/> ";
}
for ($i=1; $i <= (5 - $star); $i++) { 
    $total .= "<img src='../images/star_null.png'/> ";
}
if($star > 1 ){
    $reviews = " Reviews";
}else{ 
    $reviews = " Review";
}
$total .= ($star) . $reviews;
echo  $total;
echo "</div>"; 

               ?>
            </div>
            <div class="pull-right"><span class="review-date"><?php echo $review_date; ?></span></div>
          </div>
            <div class="clear"></div>
            <p><?php echo $review_text; ?></p>
        </div>
      </div>
<?php 
}

?>

1 个答案:

答案 0 :(得分:0)

不要同时回显和返回数据。

function rating($star) {
    $total =  "<div class='my_all_star'>";

    for ($i=1; $i <= $star; $i++) { 
        $total .= "<img src='../images/star.png'/> ";
    }
    for ($i=1; $i <= (5 - $star); $i++) { 
        $total .= "<img src='../images/star_null.png'/> ";
    }
    if($star > 1 ){
        $reviews = " Reviews";
    }else{ 
        $reviews = " Review";
    }
    $total .= ($star) . $reviews;

    $total .="</div>";
    return $total;
}