Filter result of while loop

Hi, i’m trying only to show ticket_status = ‘Aperto’ inside a while loop. So far i’ve managed to do it using WHERE inside the database query and it is working but what if i don’t use it and try to select all ticket_status = ‘Aperto’ inside the while with a if else statement?

<?php
               $query_last_tickets = mysqli_prepare($conn, "SELECT ticket_id, ticket_subject, ticket_body, ticket_time_stamp, ticket_status FROM user_tickets WHERE ticket_user_id = ? AND ticket_status = 'Aperto' ");
               mysqli_stmt_bind_param($query_last_tickets, 'i', $user_id);
               mysqli_stmt_execute($query_last_tickets);
               mysqli_stmt_bind_result($query_last_tickets, $ticket_id, $ticketsubject, $ticketbody, $ticketdate, $ticketstatus);
               mysqli_stmt_store_result($query_last_tickets);
               $ticketmsg = mysqli_stmt_num_rows($query_last_tickets);

               ?>

                  <div class="col-lg-6 col-xs-12 col-sm-12">
                            <div class="portlet light bordered">
                                <div class="portlet-title tabbable-line">
                                    <div class="caption">
                                        <i class="icon-bubbles font-dark hide"></i>
                                        <span class="caption-subject font-dark bold uppercase">Ultime segnalazioni guasti</span>
                                    </div>
                                    <ul class="nav nav-tabs">
                                        <li class="active">
                                            <a href="#portlet_comments_1" data-toggle="tab"> Aperte </a>
                                        </li>
                                        <li>
                                            <a href="#portlet_comments_2" data-toggle="tab"> In Lavorazione </a>
                                        </li>
                                    </ul>
                                </div>
                                <div class="portlet-body">
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="portlet_comments_1">
                                            <!-- BEGIN: Comments -->
                                            <div class="mt-comments">
                                                
                                             <?php
                                    
                                          
     if ($ticketmsg > 0) {
                                                   
                                          while (mysqli_stmt_fetch($query_last_tickets)) {


                                                      $new_ticket_date = date('d/m/Y', strtotime($ticketdate));
                
                                                      
                                                      echo '

                                                      <div class="mt-comment">
                                                          <div class="mt-comment-img">
                                                              <img src="../assets/pages/media/users/avatar1.jpg" /> </div>
                                                          <div class="mt-comment-body">
                                                              <div class="mt-comment-info">
                                                                  <span class="mt-comment-author">'.$ticketsubject.'</span>
                                                                  <span class="mt-comment-date">Aperto il '.$new_ticket_date.'</span>
                                                              </div>
                                                              <div class="mt-comment-text">'.$ticketbody.'</div>
                                                              <div class="mt-comment-details">
                                                                  <span class="mt-comment-status mt-comment-status-approved">'.$ticketstatus.'</span>
                                                                  <ul class="mt-comment-actions">
                                                                      <li>
                                                                          <a href="#">Visualizza</a>
                                                                      </li>
                                                                      
                                                                  </ul>
                                                              </div>
                                                          </div>
                                                      </div>

                                                      ';
   
                                 } 

                                 }  else{
                                    echo "no result";
                                 }

if i try something like this, it doesn’t work. It does show the else echo as well :frowning:

<?php
               $query_last_tickets = mysqli_prepare($conn, "SELECT ticket_id, ticket_subject, ticket_body, ticket_time_stamp, ticket_status FROM user_tickets WHERE ticket_user_id = ? ");
               mysqli_stmt_bind_param($query_last_tickets, 'i', $user_id);
               mysqli_stmt_execute($query_last_tickets);
               mysqli_stmt_bind_result($query_last_tickets, $ticket_id, $ticketsubject, $ticketbody, $ticketdate, $ticketstatus);
               mysqli_stmt_store_result($query_last_tickets);
               $ticketmsg = mysqli_stmt_num_rows($query_last_tickets);

               ?>

                  <div class="col-lg-6 col-xs-12 col-sm-12">
                            <div class="portlet light bordered">
                                <div class="portlet-title tabbable-line">
                                    <div class="caption">
                                        <i class="icon-bubbles font-dark hide"></i>
                                        <span class="caption-subject font-dark bold uppercase">Ultime segnalazioni guasti</span>
                                    </div>
                                    <ul class="nav nav-tabs">
                                        <li class="active">
                                            <a href="#portlet_comments_1" data-toggle="tab"> Aperte </a>
                                        </li>
                                        <li>
                                            <a href="#portlet_comments_2" data-toggle="tab"> In Lavorazione </a>
                                        </li>
                                    </ul>
                                </div>
                                <div class="portlet-body">
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="portlet_comments_1">
                                            <!-- BEGIN: Comments -->
                                            <div class="mt-comments">
                                                
                                             <?php
                                    
                                          
     if ($ticketmsg > 0) {
                                                   
                                          while (mysqli_stmt_fetch($query_last_tickets)) {

if($ticketstatus == 'Aperto'){

                                                      $new_ticket_date = date('d/m/Y', strtotime($ticketdate));
                
                                                      
                                                      echo '

                                                      <div class="mt-comment">
                                                          <div class="mt-comment-img">
                                                              <img src="../assets/pages/media/users/avatar1.jpg" /> </div>
                                                          <div class="mt-comment-body">
                                                              <div class="mt-comment-info">
                                                                  <span class="mt-comment-author">'.$ticketsubject.'</span>
                                                                  <span class="mt-comment-date">Aperto il '.$new_ticket_date.'</span>
                                                              </div>
                                                              <div class="mt-comment-text">'.$ticketbody.'</div>
                                                              <div class="mt-comment-details">
                                                                  <span class="mt-comment-status mt-comment-status-approved">'.$ticketstatus.'</span>
                                                                  <ul class="mt-comment-actions">
                                                                      <li>
                                                                          <a href="#">Visualizza</a>
                                                                      </li>
                                                                      
                                                                  </ul>
                                                              </div>
                                                          </div>
                                                      </div>

                                                      ';
   
                                 } else{
                                    echo "no result";
                                 }

                                 }  

}

You’re passing a status to your query, so only those that meet that criteria will show…

That being said, why are you using php when using the database approach is a much better method to use?

Hi Dave, thanks for your answer. I thought there would be another method to filter query results without using mysql, so i don’t have to run a database query twice if i need to filter two different results like ticket_status = ‘Open’ and ticket_status = ‘Closed’
But if you say “why are you using php when using the database approach is a much better method to use” then i’ll use two separate queries with different WHERE filter :wink:

You could do it that way, but you’ll need to change your original query to not include status, at which time you changed code will work.

The reason I say to let the database do the work is that the more records you have to process, the longer a “generic” approach is going to take. Just dealing with records that meet your criteria will be less hassle in the long run.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.