简体   繁体   English

在每个 id php 的单独行上显示一个 id 的所有记录

[英]Display all records of an id on separate rows for each id php

i have this table resulted from some inner joins我有这个表是由一些内部连接产生的

id_outfit   | name | article
----------------------------------------
   56        | one  | shoe.png
   56        | one  | dress.png
   56        | one  | bag.png
   54        | two  | shoe1.png
   54        | two  | dress2.png

Column 'article' contains images. “文章”列包含图像。

I try to display in my app all the articles from an id_outfit on a single row, but I don't know how to do that.我尝试在我的应用程序中显示来自 id_outfit 的所有文章,但我不知道该怎么做。

For example I try to display all articles that have id 56 on a row,all articles that have id 54 on the next row and so on.例如,我尝试在一行上显示所有 id 为 56 的文章,在下一行显示所有 id 为 54 的文章,依此类推。

I tried to do this with GROUP BY but for each id it displays only the first image(article).我尝试使用 GROUP BY 执行此操作,但对于每个 id,它仅显示第一张图片(文章)。

Any suggestion is very helpful for me, thank you !任何建议对我都非常有帮助,谢谢!

Here is a way do it using the PDO syntax:这是使用 PDO 语法的方法:

<?php
try{
    $db = new PDO("mysql:host=localhost;dbname=yourdb", $user, $pass);
}
catch (Exception $e){
    //Handle your connection errors
}

//Request the things you need
$req = $db->query("SELECT id_outfit, article FROM your_table ORDER BY id_outfit DESC");

$tempId = null;

//start to loop through your data
while ($data = $req->fetch()){

  //if the outfit id is different than the last time we went through the loop:
  if($data["id_outfit"] !== $tempId){
    //close the previous row unless this is the first time through the loop
    echo $tempId === null ? "" : "</div>";
    //start a new row
    echo "<div class='row'>";
  }

  //now we display the image
  echo "<img src='" . $data["article"] . "' alt='youralt' >";
  //store the id we had for the next loop to check
  $tempId = $data["id_outfit"];
}

//close the last row
echo "</div>";

?>

This will output a div of class "row" for each different ID in your DB, contain all the images corresponding to that ID.这将 output 一个 class “行”的div用于您的 DB 中的每个不同 ID,包含与该 ID 对应的所有图像。

Then you can style those div using css to display it as a row, most likely using a flexbox:然后,您可以使用 css 设置这些 div 的样式以将其显示为一行,很可能使用 flexbox:

.row{
  display: flex;
}

(you will need more css than that to make it actually pretty obviously.) (你需要更多的 css 才能让它变得非常明显。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM