PHP从两个表中获取MySQL结果并将它们组合在一起

时间:2016-11-29 18:52:54

标签: php mysql

我有两个带字段的表。

现在我可以在两个查询中获得所需的所有数据。 但是我如何结合结果或者我应该混合它们呢?

这是我的代码:

    $query = "SELECT * FROM product";
    $select_product = $db->query($query);

    while($row = $db->fetch_object($select_product)) {
        $status = $row->status;
        if($status == 'published') {
            $product_id = $row->id; 
            $product_title = $row->title;
            $product_image = $row->image;   

            echo '<div class="item">';
            echo '<div class="title"><a href="/product/' . $product_id . '">' . $product_title . '</a></div>';

            echo '</div>';
        }
    }

    $query = "SELECT * FROM recipe";
    $select_recipe = $db->query($query);

    while($row = $db->fetch_object($select_recipe)) {
        $status = $row->status;
        if($status == 'published') {
            $recipe_id = $row->id;  
            $recipe_title = $row->title;
            $recipe_image = $row->image;    

            echo '<div class="item">';
            echo '<div class="title"><a href="/recipe/' . $recipe_id . '">' . $recipe_title . '</a></div>';

            echo '</div>';
        }
    }

正如您从代码中看到的那样,html项目处于循环中并显示产品中的所有数据,并在单独的html中显示配方循环项目,但我的目标是这样做并添加图像,当然:

   <div class="product">
      <div class="product_title">'the_value'</div>
      <div class="product_image">'the_value'</div>
   </div>
   <div class="recipe">
      <div class="recipe_title">'the_value'</div>
      <div class="recipe_image">'the_value'</div>
   </div>
   <div class="product">
      <div class="product_title">'the_value'</div>
      <div class="product_image">'the_value'</div>
   </div>
   <div class="recipe">
      <div class="recipe_title">'the_value'</div>
      <div class="recipe_image">'the_value'</div>
   </div>
   ... and so on ...

到目前为止,我已尝试在单个查询中使用UNION子句获取数据,

$query = "SELECT * FROM product UNION SELECT * FROM recipe";

但我收到错误:

Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given

感谢。

EDIT2: 以下是表格:

表名:食谱

字段:id,title,image

表名:产品

字段:id,title,image

我想要的只是显示混合的数据。现在我显示如下:食谱/食谱/食谱/产品/产品/产品

并且目标显示如下:

配方/产品/配方/产物

4 个答案:

答案 0 :(得分:0)

我没有足够的代表发表评论,或者我愿意,但你应该看到question

$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);

$new = array();
for ($i=0; $i<count($arr1); $i++) {
    $new[] = $arr1[$i];
   $new[] = $arr2[$i];
}
var_dump($new);

答案 1 :(得分:0)

我仍然认为这实际上是Merge every other array php的副本。但是,这是根据您的情况专门定制的逻辑。

首先,这是你的两个问题:

$query = "SELECT * FROM recipe";
$select_recipe = $db->query($query);

$query = "SELECT * FROM product";
$select_product = $db->query($query);

$recipes = $select_recipe->fetch_all();
$products = $select_product->fetch_all();

您的两个查询将导致您获得数据,就像您已完成此操作一样:

$recipes = array(
    array(
        "id" => 1,
        "title" => "Apple Pie",
        "image" => "applepie.jpg",
        "status" => "published"
    ),
    array(
        "id" => 2,
        "title" => "Pizza",
        "image" => "pizza.jpg",
        "status" => "published"
    ),
    array(
        "id" => 3,
        "title" => "Chicken Soup",
        "image" => "soup.jpg",
        "status" => "published"
    ),
);

$products = array(
    array(
        "id" => 1,
        "title" => "Apples",
        "image" => "apples.jpg",
        "status" => "published"
    ),
    array(
        "id" => 2,
        "title" => "Cheese",
        "image" => "cheese.jpg",
        "status" => "published"
    ),
    array(
        "id" => 3,
        "title" => "Chicken",
        "image" => "chicken.jpg",
        "status" => "published"
    ),
);

现在我们需要为每一行添加适当的路径前缀:

foreach($recipes as &$recipesRow)
{
    $recipesRow["path"] = "/recipe/";
}

foreach($products as &$productsRow)
{
    $productsRow["path"] = "/product/";
}

以下是链接问题中其中一个答案的确切功能:

function array_merge_alternating($array1, $array2)
{
  $mergedArray = array();

  while( count($array1) > 0 || count($array2) > 0 )
  {
    if ( count($array1) > 0 )
      $mergedArray[] = array_shift($array1);
    if ( count($array2) > 0 )
      $mergedArray[] = array_shift($array2);
  }
  return $mergedArray;
}

以下是您使用数据调用该函数的方法,并根据您的初始代码使用结果:

$mergedArray = array_merge_alternating($recipes, $products);

foreach($mergedArray as $row)
{
    $status = $row["status"];
        if($status == 'published') {
            $item_id = $row["id"];  
            $item_title = $row["title"];
            $item_image = $row["image"]; 
            $item_path = $row["path"]; 

            echo '<div class="item">';
            echo '<div class="title"><a href="' . $item_path . $item_id . '">' . $item_title . '</a></div>';

            echo '</div>';
        }
}

Live Example Here

答案 2 :(得分:0)

考虑使用从数据库提取构建的多维关联数组,然后在所有项目中运行for循环。另请注意, status 过滤器也可以在查询中编写。

$query = "SELECT * FROM product where status = 'published'";
$select_product = $db->query($query);

$products = []; 
while($row = $db->fetch_object($select_product)) {
    $products[]['id'] = $row->id;            
    $products[]['title'] = $row->title;
    $products[]['image'] = $row->image;   
}

query = "SELECT * FROM recipe where status = 'published'";
$select_recipe = $db->query($query);

$recipes = [];
while($row = $db->fetch_object($select_recipe)) {
    $recipes[]['id'] = $row->id;            
    $recipes[]['title'] = $row->title;
    $recipes[]['image'] = $row->image;   
} 

for($i = 0; $i < min(count($products), count($recipes)); $i++){
    echo '<div class="item">';
    echo '<div class="title"><a href="/product/' . $products[$i]['id'] . '">' . $products[$i]['title'] . '</a></div>';
    echo '<div class="image"><a href="/product/' . $products[$i]['id'] . '">' . $products[$i]['image'] . '</a></div>';
    echo '</div>';

    echo '<div class="item">';
    echo '<div class="title"><a href="/recipe/' . $recipes[$i]['id'] . '">' . $recipes[$i]['title'] . '</a></div>';
    echo '<div class="image"><a href="/recipe/' . $recipes[$i]['id'] . '">' . $recipes[$i]['image'] . '</a></div>';
    echo '</div>';
}

注意:由于食谱和产品的长度可能不同,因此上述for循环至min()和{{1}的最低$products次计数},所以每个$recipes维护一对两者。否则,未定义索引将产生错误。对于剩余的项目,在列表上运行另一个循环,然后返回更多的计数。

<div class="item">

答案 3 :(得分:-1)

您通常会在JOIN语句中执行此操作。既然你没有提供列名,这在黑暗中有点刺,但它可能看起来像这样:

SELECT a.`id`, a.`title`, b.`image` FROM `product` AS a LEFT JOIN `recipe` AS b ON a.`id` = b.`id`;

要做到这一点,你需要做几件事。您需要在表id的{​​{1}}上放置一个关系索引,以便它的id与来自recipe的相应id动态匹配。

此外,您应该在运行查询时仅显式请求所需的列。不要查询product,因为它可能是一个非常繁重的查询,也可能会披露您不希望为该特定情况提供的信息。