dongxia9519 2014-09-29 11:43
浏览 363
已采纳

使用条件语句显示动态内容

I need a solution for a php based website. I have a testimonial in homepage page in which the content should be dynamically displayed from the back-end. and also i need a conditional statement for it. If the testimonial is approved from back-end, then display the content in homepage, if not , do not show.

Images

Html mainpage Codes

     <aside id="home-marketing-testimonials">
           <span class="section_title"><h6 style="color:#fff200; font-weight:bold;">Our Students Say it Best</h6></span>
           <div class="testimonials" style="text-align:justify;">
              <div class="testimonial">
                 <blockquote style="color:#fff;">
                   <?php
           $nsql = mysql_query("select * from testimonials order by id desc limit 0,1");
           while($nrow = mysql_fetch_array($nsql))
           {
                ?>

               <?php echo $nrow[5]; ?></a></strong>
                  <?php 
                  $nrowlen = strlen($nrow[1]);
                  if($nrowlen > 220)
                  {
                        echo substr($nrow[1],0,220)."...";
                  }
                  else
                  {
                        echo "<div style='font-size:12px;color:yellow; margin-top:10px;'>".$nrow[1]."</div>";
                  }
                  ?>
                 </blockquote>
                   <?php
           }
           ?>
                 <strong class="client_identity" style="color:#fff; float:left;"><a class="test_author" href="students-testimonials.php"></a></strong>
              </div>


           </div>

        </aside>

Backend testimonial code:

<div class="content_display">
        <div class="widgetcontent bordered">
                <div class="row-fluid">
                <!--<div align="right"><a href="add_pages.php" style="margin-bottom: 10px; color:#fff;" class="btn btn-primary">Add New</a></div>-->
        <table class="table table-bordered"  width="100%">
                <colgroup>
                    <col class="con0" />
                    <col class="con1" />
                    <col class="con0" />
                    <col class="con1" />
                    <col class="con0" />
                    <col class="con1" />
                </colgroup>
                <thead>

                    <tr>
                        <th width="10%">S.No</th>
                        <th width="20%">Name</th>
                        <th width="20%">Student ID</th>
                        <th width="20%">Email</th>
                        <th width="10%">Status</th>
                        <th width="20%">Options</th>
                    </tr>
                </thead>
                <tbody>
                 <?php 
                $i=1;
                $sql=mysql_query("select * from testimonials order by id desc");

                while($res=mysql_fetch_array($sql))
                {
                ?>
                    <tr>
                        <td ><?php echo $i; ?></td>
                        <td><?php echo $res['name']; ?></td>
                        <td><?php echo $res['stuid']; ?></td>
                        <td><?php echo $res['email']; ?></td>
                        <td><?php 
                        if($res['cstatus']=="0")
                        {
                            echo "Disapproved";
                        }
                        else
                        {
                            echo "Approved";
                        }
                        ?>
                        </td>
                        <td>
                        <a href="view_testimonial.php?id=<?php echo $res['id']; ?>">View</a>&nbsp;&nbsp;
                        <?php 
                        if($res['cstatus']=="0")
                        {
                            echo '<a href="approve.php?id='.$res['id'].'">Approve</a>';
                        }
                        else
                        {
                            echo '<a href="disapprove.php?id='.$res['id'].'">Disapprove</a>';
                        }
                        ?>
                        &nbsp;&nbsp;<a href="#" onClick="ConfirmChoice(<?php echo $res['id']; ?>);">Delete</a></td>
                    </tr>
                   <?php
               $i++; }?></tbody></table></div></div></div>
  • 写回答

1条回答 默认 最新

  • donglang7236 2014-09-29 11:46
    关注

    This is the front end code with the mysql line changed, the loop started from the correct place, and the if/else statement corrected...

    <aside id="home-marketing-testimonials">
           <span class="section_title"><h6 style="color:#fff200; font-weight:bold;">Our Students Say it Best</h6></span>
           <div class="testimonials" style="text-align:justify;">
           <?php
               $nsql = mysql_query("select * from testimonials where cstatus=1 order by id desc limit 3");
               while($nrow = mysql_fetch_assoc($nsql)) {
                   //start testimonial div and blockquote
                   echo "<div class='testimonial'><blockquote style='color:#fff;'>";
    
                   //shorten quote if too long
                   $nrowlen = strlen($nrow['comments']);
                   if ($nrowlen > 220) $nrow['comments']=substr($nrow['comments'],0,220)."...";
    
                   //insert quote and end blockquote
                   echo "<div style='font-size:12px;color:yellow; margin-top:10px;'>".$nrow['comments']."</div></blockquote>";
    
                   //student identity (start and end)
                   echo "<strong class='client_identity' style='color:#fff; float:left;'><a class='test_author' href='students-testimonials.php'>".$nrow['name']."</a></strong>";
    
                   //end testimonial div
                   echo "</div>";
               }
           ?>               
           </div>
    </aside>
    

    I also used mysql_fetch_assoc instead of mysql_fetch_array so that I could use the column names ($nrow['name']) instead of the column id numbers ($nrow[1])

    Also notice I'm using LIMIT 3 here. It looks like on your site you only pull three quotes. If this can ever be a different number, just change the number 3.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了