【问题标题】:Need some help for illogical logic in PHP + mysql and HTML tables需要一些帮助来解决 PHP + mysql 和 HTML 表中的不合逻辑逻辑
【发布时间】:2012-07-29 04:17:55
【问题描述】:

这个板的新手,但到目前为止喜欢它:)

我目前正在帮助我的一个朋友重做她的个人网站,而我正在为一张桌子的逻辑撞墙。

该表格应该在由 4 个列/单元格/TDs pr 行组成的表格中显示最新消息(仅此而已) - 如下所示: http://screencast.com/t/Rh39oh6ms0OL

所以基本上我有一个 SQL 查询,可以从新闻表中选择日期、缩略图和标题(按 id desc 排序,所以我首先得到最新的)。

我已经做了一些逻辑(如您在下面的代码中所见),但它只会导致表格的第一行显示。 有谁能让我回到正确的轨道上吗?将不胜感激。

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

【问题讨论】:

  • 不应再使用 MySQL 函数。而是使用 MySQLi 或 PDO 来访问您的数据库。
  • 您是否尝试在第一次执行循环时将 image1 n date1 放在 row1、col1(html 表)和 header1 在 row2、col1 处??
  • @Lusitanian 虽然我完全同意,但我认为解决基本逻辑问题更为重要。尽管不鼓励这种编码风格,但应该能够产生正确的结果。
  • @phant0m 因此我的评论是评论而不是答案

标签: php mysql sql if-statement while-loop


【解决方案1】:

你可以修改代码,这样你就可以每4组同时生成[缩略图,日期]和[标题]

PHP:

  //Setting variables
  $i   = 1;
  $img_date = '';  
  $headline =''; 

  //Looping through the resultset
  while($row = mysql_fetch_assoc($result))
  {
     //Building both  content for 4 columns
     $img_date .= '<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>';

    $headline .='<td width="215" valign="top"><br /><div style="padding-left:8px;"><h2>'.$row['headline'].'</h2><a href="#">L&aelig;s mere</a></div></td>';


     if($i == 4) // Time build row
     {  
        // Building both thumbnail and headline row 
        echo "<tr id='thumb_date'>$img_date</tr>";
        echo "<tr id='headline'>$headline </tr>";

         //resetting  
        $i=1; 
        $img_date = '';  
        $headline ='';
      }
      else
        i++;

   }//end while

【讨论】:

  • 不错的输入 @Makesh 但是 voodoo417 的解决方案成功了。
【解决方案2】:

对于每个结果(即一个 整个 新闻条目,而不是一列),您只执行一个 if 子句,因为您使用 if/elseif/elseif/else 构造。它们是相互排斥的。

含义:

这会完全跳过数据库结果的第四行:

else if($i % 4 == 0)
{
  print('</tr><tr>');
  $i++;
  $p++;
}

这会捕获所有不是 4 的倍数且大于 4 的行:

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

因为你的其他条件已经耗尽了所有的可能性。

  • 第一个子句将执行 4 次。
  • 那么第二个子句会执行一次,因为$i == 4
  • 现在第三个子句将针对$i == 5, 6, 7 执行
  • 第二个子句将针对$i == 8 执行
  • 第三个子句将针对$i == 9, 10, 11 执行

等等等等

为什么有两个计数器变量?据我所知,您总是在相同的位置增加它们,并在相同的位置将它们设置为零。有什么区别?

更新

澄清您在评论中提到的内容:

else if($i % 4 != 0 || $p % 4 == 0)

|| 表示逻辑或。它将首先检查第一个条件,如果不正确,它将检查第二个条件。如果其中任何一个为真,则认为整个条件为真,子句将执行。

但是,$i % 4 != 0 仅在 $i % 4 == 0 为真时为假。这意味着,前面的else if 子句已执行。支票甚至永远不会到达$p 部分。

【讨论】:

  • 好的,谢谢...我会重新尝试一下,但请注意,在第二个子句中它是 else if($i % 4 != 0 || $p % 4 == 0) - 该子句使用 $i 作为第一部分,使用 $p 作为第二部分,而不是 $i 用于两者
  • @JFlindt:第二部分|| $p % 5 == 0 永远不会为真,也永远不会被执行。
  • @JFlindt 我更新了我的问题以更好地解释为什么它不重要。看看最后吧。
  • @phant0m 感谢您的意见......自从我不得不做一些编程以来已经很久了,所以我有点生疏了。
【解决方案3】:

可能看起来更好

$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/>'; 
}

【讨论】:

  • @voodoo417 - 感谢您的帮助 - 这个解决方案对我有用。现在来分析您的代码示例,以便下次需要时在脑海中掌握正确的工具。
猜你喜欢
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2023-03-08
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多