lundi 31 octobre 2016

This if statement is adding itself to the next row

Image showing row issue

This is adding the previous rows rating query every time a new row is displayed any ideas as to why its doing this? Have I failed to close the loop in some way?

Please help I have limited SQL, IF statement knowledge and it has been ruining my mind for days now

    <?php
                // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 


$sql = "SELECT sailors.id, sailors.habbo, ranks.rank, ranks.abv, rating.ratingabv, rating.rating, position.position FROM (((sailors INNER JOIN ranks ON sailors.rankid = ranks.rankid) INNER JOIN rating ON sailors.ratingid = rating.ratingid) INNER JOIN position ON sailors.pos_id = position.pos_id) WHERE sailors.rankid=30";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
     // output data of each row
     echo"<div class='col-md-12'> <h1>W-5 Chief Warrant Officer Five (CWO5)</h1></div>";
     while($row = $result->fetch_assoc()) {

       $ast_query = $conn->query("select ast_list.ast_tag from (ast_list INNER JOIN ast ON ast.ast_id = ast_list.ast_id) where ast_sailor_id = '" . $row['id'] . "'");
       $str_ast = '';

          $last_promo = $conn->query("select promo.date, sailors.habbo, promo.promo_officer_id, sailors.id FROM (promo INNER JOIN sailors ON sailors.id = promo.promo_officer_id) WHERE promo_sailor_id = '" . $row['id'] . "' ORDER BY promoid DESC LIMIT 1");
          $str_promo = '';

          $last_promo3 = $conn->query("select promo.date, sailors.habbo, promo.promo_officer_id, sailors.id FROM (promo INNER JOIN sailors ON sailors.id = promo.promo_officer_id) WHERE promo_sailor_id = '" . $row['id'] . "' ORDER BY promoid DESC LIMIT 1");
          $str_promo3 = '';

          $last_promodate = $conn->query("select promo.date, sailors.habbo FROM (promo INNER JOIN sailors ON sailors.id = promo.promo_officer_id) WHERE promo_sailor_id = '" . $row['id'] . "' ORDER BY promoid DESC LIMIT 1");
          $str_promo2 = '';

         $rating_query = $conn->query("SELECT sailors.id, sailors.habbo, ranks.rank, ranks.abv, rating.ratingabv, rating.rating, position.positionabv, position.position FROM (((sailors INNER JOIN ranks ON sailors.rankid = ranks.rankid) INNER JOIN rating ON sailors.ratingid = rating.ratingid) INNER JOIN position ON sailors.pos_id = position.pos_id) WHERE sailors.id = '" . $row['id'] . "'");
       $str_rating = '';

          while($promo_array = $last_promo->fetch_assoc()){
              $str_promo .= $promo_array['habbo'];
          }
          while($promo_array2 = $last_promo3->fetch_assoc()){
                   $str_promo3 .= $promo_array2['id'];
       }

       while($promo_array2 = $last_promodate->fetch_assoc()){
                    $str_promo2 .= $promo_array2['date'];

       }


       while($ast_array = $ast_query->fetch_assoc()){
         $str_ast .= $ast_array['ast_tag'];
       }
         if ($ast_query->num_rows > 0) {
         $text  = "(" . $str_ast . ")";
} else { 
    $text = "";
} 

       while($rating_array = $rating_query->fetch_assoc()){

         $str_position .= $rating_array['positionabv'];

       }


         if ($row['position'] == "None"){

         $rating  = "". $row["ratingabv"]. "";
} else { 

    $rating = "/" . $str_position . "";
} 


                    echo " 
                    <div class='col-md-4'><div class='panel panel-default'>
                    <div class='panel-heading'>
                        <h4 class='text-white'><center><b>". $row["habbo"]. "</b></center></h4>
                    </div>

                    <div class='panel-body'>
                    <center>
                    <a href='sailor.php?id=". $row["id"] ."'>
                        <img class='img-responsive'  src='http://ift.tt/2eNO2IK". $row["habbo"]. "&direction=2' alt=''>
                        </a></center><br>
                        <center><p><b>USN: " . $row["abv"] . "" . $rating . "




                         " . $text . "</b>

                        <center><small>Last promoted by <a href='sailor.php?id=". $str_promo3 ."'>" . $str_promo . "</a> <br>  " . $str_promo2 . "</small></center>
                         <p></center>

                    </div>
                </div></div> ";

     }
} else {
     echo "";
}

$conn->close();
?>

Aucun commentaire:

Enregistrer un commentaire