2017-04-25 57 views
-1

我使用滑塊獲取MYSQL記錄我使用while函數我只需要第一個項目中的活動類。我只需要循環第一個元素中的活動類來解決這個問題。我使用此代碼但無法更改該項目。如何只設置第一個項目活躍在while循環在php

mycode的

<?php 
        $qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`"); 
        ?> 
        <?php while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
         <div class="item <?php if($i==0) { ?> active<?php } ?>"> 
         <ul class="thumbnails"> 
          <li class="col-xs-12 col-lg-12 col-md-12 col-sm-12"> 
           <div class="fff"> 
            <div class="thumbnail"> 
             <a href="#"><img src="administrator/slide_name/large/slide_1.jpg" alt=""></a> 
            </div> 
            <div class="col-lg-12 well"> 

             <div class="form-group"> 
              <h3><?php print_r($row->post_title); ?></h3> 
             </div> 
             <div class="form-group"> 
              <h4><?php print_r($row->post_url); ?></h4> 
              <h5></h5> 
             </div> 
             <div class="form-group"> 
              <div class="row"> 
               <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                <center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center> 
               </div> 
               <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                <center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center> 
               </div> 
              </div> 
             </div> 
             <div class="form-group"> 
              <p>Location:</p> 
              <p><?php print_r($row->post_maplocation); ?></p> 
             </div> 
             <div class="form-group"> 
              <p>Description: </p> 
              <p><?php print_r($row->post_desc); ?></p> 
             </div> 
             <div class="row pull-right"> 
              <a class="btn btn-mini" href="#">» Read More</a> 
              <a class="btn btn-mini" href="#">» Share it</a> 
             </div> 
            </div> 
           </div> 

          </li> 
         </ul> 
        </div> 
        <?php } ?> 
+0

out side $ i = 0;在while循環$ i ++其他代碼是好的 –

+0

你能寫代碼如何修復 –

+0

請等待jst做 –

回答

1

更換

<?php while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
<div class="item <?php if($i==0) { ?> active<?php } ?>"> 

隨着

<?php 
    $i=0; 
    while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
    <div class="item <?php if($i==0) { 
    $i=1; 
    echo 'active'; } ?>"> 
+0

其工作謝謝。 –

+0

太棒了!快樂的編碼! @AravinthE :) – Akshay

0

定義$ I = 0並使其$ I ++ 試試下面的代碼

<?php 
         $qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`"); 
         ?> 
         <?php 
     $i = 0; 
    while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
          <div class="item <?php if($i==0) { ?> active<?php } ?>"> 
          <ul class="thumbnails"> 
           <li class="col-xs-12 col-lg-12 col-md-12 col-sm-12"> 
            <div class="fff"> 
             <div class="thumbnail"> 
              <a href="#"><img src="administrator/slide_name/large/slide_1.jpg" alt=""></a> 
             </div> 
             <div class="col-lg-12 well"> 

              <div class="form-group"> 
               <h3><?php print_r($row->post_title); ?></h3> 
              </div> 
              <div class="form-group"> 
               <h4><?php print_r($row->post_url); ?></h4> 
               <h5></h5> 
              </div> 
              <div class="form-group"> 
               <div class="row"> 
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                 <center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center> 
                </div> 
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                 <center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center> 
                </div> 
               </div> 
              </div> 
              <div class="form-group"> 
               <p>Location:</p> 
               <p><?php print_r($row->post_maplocation); ?></p> 
              </div> 
              <div class="form-group"> 
               <p>Description: </p> 
               <p><?php print_r($row->post_desc); ?></p> 
              </div> 
              <div class="row pull-right"> 
               <a class="btn btn-mini" href="#">» Read More</a> 
               <a class="btn btn-mini" href="#">» Share it</a> 
              </div> 
             </div> 
            </div> 

           </li> 
          </ul> 
         </div> 
         <?php 
          $i++; 
         } ?> 
0
<?php $i = 0 ; 
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`"); 
?> 
<?php while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
<div class="item <?php if($i==0) { ?> active<?php } ?>"> 
Your other code 
php $i++; } ?> 
1

試試這個。

$qry = MYSQL_QUERY("SELECT * FROM 'post' WHERE 'post_type'='events' AND 'post_event_start_date' <= CURDATE() ORDER BY 'post_event_start_date'"); 
$first_item = true;?> 
        <?php while($row = MYSQL_FETCH_OBJECT($qry)){ ?> 
         <div class="item <?php echo $first_item? 'active': ''; ?>"><?php $first_item = false; ?> 
         <ul class="thumbnails"> 
          <li class="col-xs-12 col-lg-12 col-md-12 col-sm-12"> 
           <div class="fff"> 
            <div class="thumbnail"> 
             <a href="#"><img src="administrator/slide_name/large/slide_1.jpg" alt=""></a> 
            </div> 
            <div class="col-lg-12 well"> 

             <div class="form-group"> 
              <h3><?php print_r($row->post_title); ?></h3> 
             </div> 
             <div class="form-group"> 
              <h4><?php print_r($row->post_url); ?></h4> 
              <h5></h5> 
             </div> 
             <div class="form-group"> 
              <div class="row"> 
               <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                <center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center> 
               </div> 
               <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> 
                <center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center> 
               </div> 
              </div> 
             </div> 
             <div class="form-group"> 
              <p>Location:</p> 
              <p><?php print_r($row->post_maplocation); ?></p> 
             </div> 
             <div class="form-group"> 
              <p>Description: </p> 
              <p><?php print_r($row->post_desc); ?></p> 
             </div> 
             <div class="row pull-right"> 
              <a class="btn btn-mini" href="#">» Read More</a> 
              <a class="btn btn-mini" href="#">» Share it</a> 
             </div> 
            </div> 
           </div> 

          </li> 
         </ul> 
        </div> 
        <?php } ?>`