你如何组合2个PHP迭代?

时间:2012-11-04 17:05:18

标签: php iteration

有人可以帮我组合这两个迭代,以允许两个数组项目显示在我的网页上吗?我要做的是在我的网页中显示videoURL和相应的GroupName。

首先

try
{
 $sql = 'SELECT URL from videoclip';
 $result = $pdo->query($sql);

}
catch (PDOException $e)

{
 $error ='Error fetching videos:'.$e->getMessage();
 include'error.html.php';
 exit();
}

while ($row = $result->fetch())
{
  $URLS[] = $row['URL'];
}

include 'index.html.php';

第二

try
{
  $sql = 'SELECT GroupName from videoclip';
  $result = $pdo->query($sql);
}
catch(PDOException $e)
{
 $error ='unable to fecth data:'.$e->getMessage();
 include'error.html.php';
 exit();
}

while ($row = $result->fetch())
{
  $GroupNames[] = $row['GroupName'];
}
include 'index.html.php';

1 个答案:

答案 0 :(得分:2)

只需合并您的查询,然后构建一个多维数组以传递给index.html.php

try
{
  $sql = "SELECT URL, GroupName FROM videoclip";
  $result = $pdo->query($sql);
}
catch(PDOException $e)
{
  $error = 'unable to fetch data: '.$e->getMessage();
  include'error.html.php';
  exit();
}
$URLS = array();
while ($row = $result->fetch())
{
  $URLS[] = array('URL' => $row['URL'], 'GroupName' => $row['GroupName'] );
}

include 'index.html.php';