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

报告相同问题?