If else not showing results correctly

2019-08-21 10:27发布

I'm trying to do a if else depending on the rows that it get back but its not working right i dont know what i'm doing wrong if anyone can give me some insite on what i'm doing wrong would be so much of a help thank you in advance

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 5");
$query3=mysqli_query($dbconnect,"SELECT companyname,client_id,feedback,status from review WHERE status=3");
$rows_get = mysqli_num_rows($query);

if ($rows_get >0) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query3);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >1) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >2) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
} 
}else if ($rows_get >3) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >4) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query);
   $row5 = mysqli_fetch_assoc($query);
} 
}else {
   $row1 = mysqli_fetch_assoc($query3);
   $row2 = mysqli_fetch_assoc($query3);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}

this is the php/html side where the post get pulled to this is where i'm running in to issues

    <blockquote class="blockquote" >
      <p class="mb-0">"<?php echo $row1['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row1['client_id'];  ?> @ <?php echo $row1['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote-reverse">
      <p class="mb-0">"<?php echo $row2['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row2['client_id'];  ?> @ <?php echo $row2['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote">
      <p class="mb-0">"<?php echo $row3['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row3['client_id'];  ?> @ <?php echo $row['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote-reverse">
      <p class="mb-0">"<?php echo $row4['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row4['client_id'];  ?> @ <?php echo $row4['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote">
      <p class="mb-0">"<?php echo $row5['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row5['client_id'];  ?> @ <?php echo $row5['companyname'];  ?></footer>
    </blockquote>

  </div>
</div>

标签: php mysql
2条回答
甜甜的少女心
2楼-- · 2019-08-21 11:05

This is could be a solution for your problem, even if it's a bit tricky:

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 5");
$query3=mysqli_query($dbconnect,"SELECT companyname,client_id,feedback,status from review WHERE status=3");
$rows_get = mysqli_num_rows($query);

$nq=0;

while($row=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
    if ($rows_get == 1 && $nq < 1) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 2 && $nq < 2) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 3 && $nq < 3) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 4 && $nq < 4) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 5 && $nq < 5) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else {
       break;
    }
}

$nq=0;

while($row3=mysqli_fetch_array($query3,MYSQLI_ASSOC))
{
    if ($rows_get == 0 && $nq < 5) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 1 && $nq < 4) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 2 && $nq < 3) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 3 && $nq < 2) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 4 && $nq < 1) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else {
       break;
    }
}
查看更多
孤傲高冷的网名
3楼-- · 2019-08-21 11:12

You can use a while loop instead of using if else, while($row[]=mysqli_fetch_assoc($query)); while($row[]=mysqli_fetch_assoc($query3)); these two will get all your data in an array $row. no need to check conditions as this will get data only if data exists and append with the previous.

查看更多
登录 后发表回答