doulun1666 2012-07-31 11:47
浏览 55
已采纳

在PHP + mysql和HTML表格中需要一些不合逻辑的逻辑帮助

New to this board but loving it so far :)

I am currently helping a friend of mine redo her personal website and I am running my head against a wall on the logic for a table.

The table is suppossed to show the latest news in a table consisting of 4 columens/cells/TDs pr row (nothing more) - looing something like this: http://screencast.com/t/Rh39oh6ms0OL

So basically I have a SQL query that selects the date, thumbnail and headline from the news table (ordered by id desc so I get the newest first).

I have made some logic (as you can see in the code below) but it is only resulting in the first row of the table showing. Anyone out there who can get me back on the right track? It will be very much appreciated.

<table>
  <?php
  //PHP code used for building HTML table showing news-resumes
  //Written by:  Jesper Flindt
  //Date:       2012.07.30
  //Version:   1.0

  //Getting data from MySQL database
  $query  = "select date, thumbnail, headline from news order by id desc";
  $result = mysql_query($query) or die("Connection Error:" . mysql_error());

  //Setting variables
  $i   = 0;
  $p  = 0;

  //Starting row in table
  print('<tr>');

  //Looping through the resultset
  while($row = mysql_fetch_assoc($result))
  {
    //Limit row to 4 cells (TDs)
    if($i < 4)
    {
      //Display the date of the news as well as its thumbnail
      print('<td><div style="padding-left:8px;"><b>Tilf&oslash;jet d. '.$row['date'].'</b></div><div class="news_thumbframe"><img src="./upload/'.$row['thumbnail'].'" alt=""/></div></td>');
      $i++;
      $p++;
    }
    //If row is 4 cells wide, start new row
    else if($i % 4 == 0)
    {
      print('</tr><tr>');
      $i++;
      $p++;
    }
    //Every second row should display the news headline as well as a "Read More" link ("L&aelig;s mere" in danish)
    else if($i % 4 != 0 || $p % 4 == 0)
    {
      print('<td width="215" valign="top"><br /><div style="padding-left:8px;"><h2>'.$row['headline'].'</h2><a href="#">L&aelig;s mere</a></div></td>');
      $i++;
      $p++;
    }
    else
    {
      $i = 0;
      $p = 0;
    }
  }
  ?>
</table>
  • 写回答

3条回答 默认 最新

  • douluozhan4370 2012-07-31 12:09
    关注

    may be looks better

    $result = mysql_query($query) or die("Connection Error:" . mysql_error());
    
    $items = array();
    
    while($row = mysql_fetch_assoc($result)){
       $items[] = array('date'=>$row['data'],
                        'thumbnail'=>$row['thumbnail'],
                        'headline'=>$row['headline'],);
    }
    
    $pairs = array_chunk($items,4);
    
    foreach ($pairs as $pair){
    
      print '<tr>';
    
      foreach ($pair as $row){
        print('<td><div style="padding-left:8px;"><b>Tilf&oslash;jet d. '.$row['date'].'</b></div><div class="news_thumbframe"><img src="./upload/'.$row['thumbnail'].'" alt=""/></div></td>');
      }
    
      print '<tr/><tr>';
    
      foreach ($pair as $row){
        print('<td width="215" valign="top"><br /><div style="padding-left:8px;"><h2>'.$row['headline'].'</h2><a href="#">L&aelig;s mere</a></div></td>');
    
      }  
      print '<tr/>'; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值