简体   繁体   English

将来自mysql行的数据显示为php中的列

[英]Display data from mysql row as a column in php

I have a question that I've been struggling with for hours! 我有一个问题,我已经奋斗了几个小时! I'm trying to create a website to display results from a charity scavenger hunt. 我正在尝试创建一个网站来显示慈善寻宝活动的结果。

I have a table in a mysql database containing my data; 我在mysql数据库中有一个表,其中包含我的数据; the first column contains the name of each person and each subsequent column shows how many of each object they've collected (I've included a pic to explain better anyway). 第一列包含每个人的名字,随后的每一列显示他们收集的每个对象有多少(无论如何,我都附有一张图片以作更好的解释)。

Table 1: 表格1:
http://i.imgur.com/U69IqiY.jpg

I'm trying to use PHP to display this data in a table but I can only represent it the way I've posted in the first picture. 我正在尝试使用PHP在表中显示此数据,但我只能用第一张图片中的方式表示它。 I'd like to select a single person's data and display it in a vertical table with two columns, the left column showing the name of each object and the right column showing how many object they've collected (I've put a pic of this in too). 我想选择一个人的数据并将其显示在垂直表格中,该表格有两列,左列显示每个对象的名称,右列显示他们收集了多少个对象(这个也)。

Table 2: 表2:
http://i.imgur.com/gverhcw.jpg

This has been driving me mad for hours so any help would be greatly appreciated! 这使我生气了几个小时,所以任何帮助将不胜感激!

Basically, as long as you know the name = Fred you do this: 基本上,只要您知道名称= Fred,您就可以这样做:

select * from your_table where name = 'Fred'

Then loop through the single result set displaying each of the items. 然后遍历显示每个项目的单个结果集。

You'd best show some more code, how you are connecting to the db and so on 您最好显示更多代码,以及如何连接数据库等。

I can't get to imgur from work but you'd want to do something like this: 我无法下班上去imgur,但您想做这样的事情:

(assuming you bound your results to these variables, of course) (当然,假设您将结果绑定到这些变量)

echo "<table>"
while($resultsFromQuery->fetch()){
echo "<tr><td colspan=2>".$name."</td></tr>";
echo "<tr><td>item 1</td><td>".$item1."</td></tr>";
echo "<tr><td>item 2</td><td>".$item2."</td></tr>";
...
echo "<tr><td>item n</td><td>".$itemn."</td></tr>";
}
echo "</table>

I am making assumptions, and please remember to always take a backup of your database before trying new code. 我正在做假设,请记住在尝试新代码之前始终备份数据库。 Speaking from experience. 从经验上讲。

If you are using PHP and MySQLi, try the following, if you would like to show a table with all of them, you have that code down, I am assuming you are going to a different page to show the single table fora person. 如果您使用的是PHP和MySQLi,请尝试以下操作,如果您想显示所有表,则将这些代码记下来,我假设您将转到另一个页面以显示一个人的单个表。

Here is what I would do, when you click on a name, have it go to a page like person.php?person=Fred 这是我要做的,当您单击一个名称时,将其转到类似于person.php?person=Fred的页面person.php?person=Fred

here is the code I would use for that page: 这是我将用于该页面的代码:

//Your DB connection details go here.
$sql = "SELECT * FROM table WHERE name = '$_GET[person]'";
$sql_con = mysqli_query($con, $sql);
$data = mysqli_fetch_array($sql_con);
echo '<h2>Data for '.$data['name'].'</h2><br />';
echo '<table><tr><th>Items</th><th>Count</th></tr><tr>'; // SET UP YOUR TABLE
echo '<td>Item 1</td><td>'.$data['item1'].'</td></tr><tr>';
echo '<td>Item 2</td><td>'.$data['item2'].'</td></tr>'; // CONTINUE FOR EVERY ITEM YOU HAVE
echo '</table>';

That should do it, if that is not what you were looking for, let me know. 应该这样做,如果那不是您想要的,请告诉我。

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

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