【问题标题】:Need some logic with PHP to loop through some mysql db results需要一些 PHP 逻辑来循环一些 mysql 数据库结果
【发布时间】:2014-11-02 10:20:38
【问题描述】:

我通过 mysql 查询返回了以下结果:

tag_id   article_id (asc)     name         tag_slug
    56        69487           Exploration  exploration
    10        69488           Events       events
    32        69488           Military     military
    28        69489           Arts         arts
     3        69489           Religion     churches
    36        69490           Forestry     forestry
     8        69490           Industry     industry
    40        69490           Man-Made     man-made
    42        69490           Politics     politics

我需要遍历结果并创建一个字符串,其中包含与每组文章 ID 相关联的 tag_slugs。这部分代码不需要 name 和 tag_id 列。

例如...

69487 会有一个字符串:''

69488 将有一个字符串:''

69489 会有一个字符串:''

69490 将有一个字符串:''

...一个名为 tags 的列将在数据库中使用这些字符串和相应的 article_id 进行更新。

我在下面的尝试有点工作,但总是忽略最后一次数据库更新。我确信必须有更好更合乎逻辑的方法,但现在我做不到。

$previous_article_id = 0;

while( $row = $result->fetch_assoc() )
{
    if ( $row['article_id '] != $previous_article_id  && $previous_article_id  != 0 )
    {
        $sql = "
             UPDATE
                    ".ARTICLE_TABLE."
             SET
                    tags = '".$tags."'
             WHERE
                    id = ".$previous_article_id."
         ";

         $db->query($sql) OR sql_error($db->error.$sql);

         $tags = '';
    }

    if ( $row['article_id '] == $previous_article_id  || $previous_article_id  == 0 )
    {
        $tags .= '<'.$row['tag_slug'].'>';
    }

    $previous_article_id  = $row['article_id '];
}

是的,我知道它应该是 PDO,而且代码有点疯狂,但我在朋友兄弟网站上工作,所以没有太多权限来更改它。

【问题讨论】:

  • 你的意思是tags 是一个单独的字段,它的值应该是&lt;forestry&gt;&lt;industry&gt;&lt;man-made&gt;&lt;politics&gt;article_id 69490?
  • 您确定您的代码正在打印 id 69488 的 标签吗?因为它看起来不是,或者我错过了一些东西。
  • @Jenson M John 是的,tags 是同一张表中的另一列
  • @Malcolm Kindermans 你是对的,只打印了事件标签。应该是事件和军事。代码未按预期运行。

标签: php mysql optimization logic


【解决方案1】:

首先,我要感谢您如何解释您的问题以及您所做的尝试。您的代码格式也很好。我经常在这里看到一些垃圾帖子。所以我的赞美。

我应该将整个内容更改为以下内容:

$article_tags = array();
while( $row = $result->fetch_assoc() )
{
    $article_tags[$row['article_id']][] = $row['tag_slug'];
}

foreach( $article_tags as $article_id => $tag_array )
{
    $tags = '';
    foreach( $tag_array as $tag )
    {
        $tags .= '<' . $tag . '>';
    }

    $sql = "
             UPDATE
                    ".ARTICLE_TABLE."
             SET
                    tags = '".$tags."'
             WHERE
                    id = ".$article_id."
         ";

    $db->query($sql) OR sql_error($db->error.$sql);
}

【讨论】:

  • 这是完美的。我知道必须有更好的方法。我不是一个程序员,因为包含更多的 foreach 循环和 foreach 循环,所有混合在一起的数组往往让我在晚上太晚了。感谢您的帮助,以及亲切的 cmets :)
【解决方案2】:

也许使用 PHP 更简洁的方法是创建一个关联数组,将您的 article_ids 作为键,将包含相关标签的字符串作为值。 例如。

 $myArray[$row['article_id ']].='<'.$row['tag_slug'].'>';

然后,最后您只需在其上使用 foreach 循环进行迭代并将数据插入数据库中。

【讨论】:

    【解决方案3】:

    在我看来,您可以更轻松地使用 GROUP_CONCAT 函数从数据库中获取此结果

      SELECT  article_id, CONCAT('<',GROUP_CONCAT(tag_slug separator '><'),'>')
      from table GROUP BY article_id
      ORDER BY article_id DESC
    

    如果您的 tag_slug 上有重复项,您可以在 GROUP_CONCAT 函数中使用 DISTINCT 运算符

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多