显示的表在PHP中反复列 [英] Displaying a table in PHP with repeated columns

查看:91
本文介绍了显示的表在PHP中反复列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个MySQL数据库中的以下数据:



<预类=郎无prettyprint-覆盖> AUTONUM ID名称MetaValue
1玫瑰饮
2 1玫瑰大好人
3 1玫瑰亚军
4 2加里 - 普莱尔
5 2加里·滑稽

我在PHP中的工作,但现在我也遇到了这个问题,使用C#,Java等语言几次。



现在我在过去和现在的目标是在下面的格式来显示数据:

 <表> 
<&THEAD GT;
<第i个姓名和LT; /第i
<第i MetaValue1< /第i
< / THEAD>
<&TBODY GT;
< TR>
< TD>玫瑰和LT; / TD>
< TD>
< UL>
<立GT;饮酒< /李>
<立GT;大好人< /李>
<立GT;亚军和LT; /李>
< / UL>
< / TD>
< / TR>
< TR>
< TD>加里< / TD>
< TD>
< UL>
<立GT;播放器和LT; /李>
<立GT;&滑稽LT; /李>
< / TD>
< / TR>
< / TBODY>

< /表>



我已经通过创建一个代表我的SQL表中的课前解决这个问题。
然后,我创建了一个词典持有雇员和类。

 词典<字符串,MyTable的>迈德特=新<字符串,MyTable的>(); 
表MyMetaData =新表();
MyMetaData SomeMetaData = getMetaValueList(); //想象这是否
MyData.add(雇员,SomeMetaData)的网络服务;



我跳过一些步骤,但我希望你明白我的意思。我可能只需要怎样称呼这种类型的问题的关键字。什么是实现这一目标的最佳方法是什么?


解决方案

我不喜欢的原因之一SQL解决方案。你问数据库引擎的加入字符串,然后你用PHP来拆呢。这就像重复的工作。另一个缺点是,如果 metavalue 包含分隔符字符。



我的代码只是演示了如何您可以轻松地组数据在PHP二维数组,它是由编码器来决定哪种方法来从数据库中检索遗留任何* mysql_或PDO的数据。

  //得到的结果
$结果= mysql_query(选择AUTONUM,ID,姓名,metavalue FROM表) ;

//的每一行
环,而($行= mysql_fetch_assoc($结果)){
$表[$行[名称] [] = $行['metavalue'];
}

//打印出来
的print_r($表);

有没有必要对整个结果集暂时存入一个阵列。您可以通过要分组哪些领域你的数据迭代直行通过第一类数据,并跟踪状态。



不过,有两个原因,我更喜欢这种风格它是个人偏好(这不是必须的,必须做的解决方案):




  1. 一般情况下,每个人都希望分离的逻辑和演示分开。在阵列中存储的数据可以很容易地传递给你的模板引擎。我更喜欢使用PHP模板引擎很容易实现,它以光速运行。


  2. 我宁愿换轻微的性能速度和内存使用情况的可读性和可维护性。该算法是短期和简单易懂。如果性能是你的首要任务,你总是可以使用缓存如存储在文件中,memcached的存储或存储在数据库中计算出的结果。




诚然,我们是分组从不同的侧面数据,但假设 metavalue 不包含分隔符(在你的代码|)。它可以通过选择分离,几乎不可能通过 metavalue 中很容易地固定。无论如何,你的解决方案将在几乎所有情况下,因为 metavalue 的@jvelez表明绝不会包含字符工作完全正常|


I have the following data in a MySQL database:

Autonum  ID  Name  MetaValue
1         1  Rose  Drinker
2         1  Rose  Nice Person
3         1  Rose  Runner
4         2  Gary  Player
5         2  Gary  Funny

I am working in PHP now but I have encountered this problem several times using C#, Java and other languages.

Now my goal in the past and present has been to display the data in the following format:

<table>
    <thead>
    <th>Name</th>
    <th>MetaValue1</th>
    </thead>
    <tbody>
           <tr>      
               <td>Rose</td>
                <td>
                    <ul>
                        <li>Drinker</li> 
                        <li>Nice Person</li>
                        <li>Runner</li>
                     </ul>
                </td>
            </tr>
            <tr>
                <td>Gary</td>
                <td>
                    <ul>
                         <li>Player</li>
                         <li>Funny</li>
                 </td>
                </tr>
        </tbody>

</table> 

I have tackled this problem before by creating a class that represents my SQL table. Then I created a Dictionary to hold EmployeeId and the class.

Dictionary<string,MyTable> MyData = new <string,MyTable>();
Table MyMetaData = new Table();
MyMetaData SomeMetaData=getMetaValueList();//imagine a web service that does that
MyData.add(EmployeeId,SomeMetaData);

I am skipping steps but I hope you get my point. I probably just need keywords of what to call this type of problem. What is the preferred way to accomplish this?

解决方案

I do not like SQL solution for one reason. You ask database engine to join string and then you use PHP to split it. It is like repeated work. Another down side is that what if metavalue contains seperator character.

My code just demonstrates how you can easily group data with 2 dimensional array in PHP and it is up to coder to decide which method to retrieve data from database either legacy mysql_* or PDO.

// get result
$result = mysql_query("SELECT autonum,id,name,metavalue FROM table");

// loop through each row
while ($row = mysql_fetch_assoc($result)) {
    $table[$row['name']][] = $row['metavalue'];
}

// print it out
print_r($table);

There is no need to temporarily store the whole resultset into an array. You can iterate straight ahead by first sort data by what field you want to group your data and keep track of state.

However, there are two reasons why I prefer this style and it is personal preference (it is not the must-must do solution):

  1. Usually, everyone want to separate logic and presentation apart. Storing data in array can be easily passed to your template engine. I prefer using PHP template engine for it is easy to implement and it runs at light speed.

  2. I would rather trade slight performance speed and memory usage for readability and maintainability. The algorithm is short and simple to understand. If performance is your top priority, you can always use cache such as storing in file, storing in memcached, or storing calculated result in database.

True, we are grouping data from different side, but you assume that the metavalue does not contain separator (which in your code is |). It can be easily fixed by choosing separator that almost impossible to be used by metavalue. Anyway, your solution will works perfectly fine in almost every case because metavalue that @jvelez has shown will never contain character |.

这篇关于显示的表在PHP中反复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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