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

时间:2014-09-29 11:43:51

标签: php html phpmyadmin

我需要基于php website的解决方案。我在主页页面上有一个推荐,其中的内容应该从后端动态显示。而且我还需要一个条件声明。如果推荐是从后端批准的,那么在主页中显示内容,如果没有,则不显示。

Images

Html主页代码

     <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>

后端推荐代码:

<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 个答案:

答案 0 :(得分:1)

这是更改mysql行的前端代码,循环从正确的位置开始,if / else语句已更正...

<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>

我还使用了mysql_fetch_assoc而不是mysql_fetch_array,这样我就可以使用列名($ nrow [&#39; name&#39;])而不是列id号($ nrow [1])

另请注意我在这里使用LIMIT 3。在您的网站上看起来只有三个引号。如果这可能是不同的数字,只需更改数字3。