将来自mysql行的数据显示为php中的列 [英] Display data from mysql row as a column in php

查看:64
本文介绍了将来自mysql行的数据显示为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.

我在mysql数据库中有一个表,其中包含我的数据;第一列包含每个人的名字,随后的每一列显示他们收集的每个对象有多少(无论如何,我都附有一张图片以作更好的解释).

I have a table in a mysql database containing my data; 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).

表1:

我正在尝试使用PHP在表中显示此数据,但是我只能以我在第一张图片中发布的方式来表示它.我想选择一个人的数据并将其显示在具有两列的垂直表中,左列显示每个对象的名称,右列显示他们收集了多少个对象(也是如此).

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. 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).

表2:

这使我生气了好几个小时,所以任何帮助将不胜感激!

This has been driving me mad for hours so any help would be greatly appreciated!

推荐答案

我正在做假设,请记住在尝试新代码之前始终备份数据库.从经验上讲.

I am making assumptions, and please remember to always take a backup of your database before trying new code. Speaking from experience.

如果您使用的是PHP和MySQLi,请尝试以下操作,如果您想显示所有表,则将这些代码记下来,我假设您将转到另一个页面以显示单个表论坛人.

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.

这就是我要做的,当您单击一个名称时,将其转到person.php?person=Fred

Here is what I would do, when you click on a name, have it go to a page like 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.

这篇关于将来自mysql行的数据显示为php中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆