简体   繁体   中英

While loop inside if else statement not working

I have this code snippet that is failing to work, don't know what am doing wrong, am getting content from MYSQL database and using if else statement with while loop to echo the contents.

<?php 

                if ($row_item['cat_item_id'] == ''){
                        echo '<div class="col-sm-6 col-md-3"">

                        </p>
                        <p>
                          No Item To Show
                        </p>
                      </div>
                  </div>';
                    }

                else  {                       
                  while ($row_item = mysql_fetch_assoc($item)){
                      echo
                  '
                <div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
                      <div class="thumbnail">
                        <img src="myaccount/user_data/'.$row_item['file_name'].'" />
                      </div>
                      <div class="caption">
                        <h3>'.$row_item['item_name'].'</h3>
                                             <p>
                          <a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
                           View Item
                          </a> 
                          <a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
                          Contact Owner
                          </a>
                        </p>
                      </div>
                  </div>';
                  }

                }

                    ?>

Help me figure out what am not doing right.

You are checking if

if ($row_item['cat_item_id'] == ''){

before you actually call

while ($row_item = mysql_fetch_assoc($item)){

To get $row_item


Try changing it to something like this:

while ($row_item = mysql_fetch_assoc($item)){
    if ($row_item['cat_item_id'] == ''){

        echo '<div class="col-sm-6 col-md-3"">
              </p>
              <p>No Item To Show</p></div></div>';

    }else  {

        echo
                  '
                <div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
                      <div class="thumbnail">
                        <img src="myaccount/user_data/'.$row_item['file_name'].'" />
                      </div>
                      <div class="caption">
                        <h3>'.$row_item['item_name'].'</h3>
                                             <p>
                          <a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
                           View Item
                          </a> 
                          <a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
                          Contact Owner
                          </a>
                        </p>
                      </div>
                  </div>';
    }
}

You have to call while ($row_item = mysql_fetch_assoc($item)) first place.

Otherwise the $row_items doesn't get initialized properly.

So you put all inside

 while ($row_item = mysql_fetch_assoc($item))

And check for the if-else conditions inside the while:

if ($row_item['cat_item_id'] == '') // this goes inside the while. Otherwise $row_item is not initialized properly

Try mysql_fetch_array

while ($row_item = mysql_fetch_array($item)){
                  echo
              '
            <div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
                  <div class="thumbnail">
                    <img src="myaccount/user_data/'.$row_item['file_name'].'" />
                  </div>
                  <div class="caption">
                    <h3>'.$row_item['item_name'].'</h3>
                                         <p>
                      <a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
                       View Item
                      </a> 
                      <a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
                      Contact Owner
                      </a>
                    </p>
                  </div>
              </div>';
              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM