gpt4 book ai didi

PHP - 如何将数组中的数据显示到树或表中?

转载 作者:搜寻专家 更新时间:2023-10-31 20:52:12 24 4
gpt4 key购买 nike

目前,我从 SELECT 中检索数据并将其转换为 PHP assoc 数组。因此,我得到了这个数组。

[0] => array
(
[Gattung] => 'Gattung_A'
[Untergattung] => null
[Sektion] => null
[Untersektion] => null
[Serie] => null
[Unterserie] => null
[Art] => 'Art_A'
[Unterart] => null
[Varietaet] => null
[SubVarietaet] => null
[Form] => 'Form_A'
[Unterform] => null
[SpezialForm] => null
[Hybride] => null
[Blendling] => null
[Sorte] => null
)
[1] => array
(
[Gattung] => 'Gattung_A'
[Untergattung] => null
[Sektion] => null
[Untersektion] => null
[Serie] => null
[Unterserie] => null
[Art] => 'Art_B'
[Unterart] => null
[Varietaet] => 'Variant_G'
[SubVarietaet] => null
[Form] => 'Form_B'
[Unterform] => null
[SpezialForm] => null
[Hybride] => null
[Blendling] => null
[Sorte] => null
)
[2] => array
(
[Gattung] => 'Gattung_B'
[Untergattung] => null
[Sektion] => null
[Untersektion] => null
[Serie] => null
[Unterserie] => null
[Art] => 'Art_C'
[Unterart] => null
[Varietaet] => 'Variant_A'
[SubVarietaet] => null
[Form] => null
[Unterform] => null
[SpezialForm] => null
[Hybride] => null
[Blendling] => null
[Sorte] => null
)
.........
Nearly ~300k records

到目前为止,我已经使用 array_unique 将我的数组固定为基于唯一值的一维数组。这是我的结果:

(
[0] => 'Gattung|Gattung_A'
[1] => 'Art|Art_A'
[3] => 'Form|Form_A'
[5] => 'Art|ArtB'
[7] => 'Varietaet|Variant_G'
[9] => 'Form|Form_G'
[11] => 'Gattung|Gattung_B'
[13] => 'Art|Art_C'
[15] => 'Varietaet|Variant_A'
}

但是,我的结果没有很好地分组。我需要将其显示在页面上,因此类似于构建分层系统。

问题:@Melsi 我希望我的第二次编辑能够净化我的问题。

How to display the data into a tree form (See Edit 2) or a table (See Edit 1)?

谢谢。

编辑 1:

我正在做的是在几个表中创建类似于 FULLTEXT 搜索的东西。这是连接所有表的查询:(所有 FK 都有每个表的 id 的索引)

SELECT 
tGa.Gattung AS Gattung,
tUG.Untergattung AS Untergattung,
tSe.Sektion AS Sektion,
tUS.Untersektion AS Untersektion,
tSer.Serie AS Serie,
tUser.Unterserie AS Unterserie,
tA.Art AS Art,
tUa.Unterart AS Unterart,
tV.Varietaet AS Varietaet,
tSV.SubVarietaet AS SubVarietaet,
tF.Form AS Form,
tUF.Unterform AS Unterform,
tSF.SpezialForm AS SpezialForm,
tH.Hybride AS Hybride,
tBL.Blendling AS Blendling,
tSo.Sorte AS Sorte
FROM
botanischername AS tBot
LEFT JOIN ( gattung AS tGa ) ON ( tBot.ID_Ga = tGa.ID_Ga )
LEFT JOIN ( untergattung AS tUg ) ON ( tBot.ID_UG = tUg.ID_UG )
LEFT JOIN ( sektion AS tSe ) ON ( tSe.ID_Se = tBot.ID_Se )
LEFT JOIN ( untersektion AS tUS ) ON ( tUS.ID_US = tBot.ID_US )
LEFT JOIN ( serie AS tSer ) ON ( tSer.ID_Ser = tBot.ID_Ser )
LEFT JOIN ( unterserie AS tUSer ) ON ( tUser.ID_USer = tBot.ID_USer )
LEFT JOIN ( art AS tA ) ON ( tA.ID_A = tBot.ID_A )
LEFT JOIN ( unterart AS tUa ) ON ( tUa.ID_UA = tBot.ID_UA )
LEFT JOIN ( varietaet AS tV ) ON ( tV.ID_V = tBot.ID_V )
LEFT JOIN ( subvarietaet AS tSV ) ON ( tSV.ID_SV = tBot.ID_SV)
LEFT JOIN ( form AS tF ) ON ( tF.ID_F = tBot.ID_F )
LEFT JOIN ( unterform AS tUF ) ON ( tUF.ID_UF = tBot.ID_UF )
LEFT JOIN ( spezialform AS tSF ) ON ( tSF.ID_SF = tBot.ID_SF )
LEFT JOIN ( hybride AS tH ) ON ( tH.ID_H = tBot.ID_H )
LEFT JOIN ( blendling AS tBL ) ON ( tBL.ID_BL = tBot.ID_BL )
LEFT JOIN ( sorte AS tSo ) ON ( tSo.ID_So = tBot.ID_So )
LEFT JOIN ( status AS tST
CROSS JOIN pflanze AS tPfl) ON
( tST.ID_ST = tBot.ID_ST AND tPfl.ID = tBot.ID )

WHERE
tGa.Gattung LIKE #1_Word# OR
tUG.Untergattung LIKE #2_Word# OR
tSe.Sektion LIKE #3_Word# OR
tUS.Untersektion LIKE #4_Word# OR
tSer.Serie LIKE #5_Word# OR
tUser.Unterserie LIKE #6_Word# OR
tA.Art LIKE #7_Word# OR
tUa.Unterart LIKE #8_Word# OR
tV.Varietaet LIKE #9_Word# OR
tSV.SubVarietaet LIKE #10_Word# OR
tF.Form LIKE #11_Word# oR
tUF.Unterform LIKE #12_Word# OR
tSF.SpezialForm LIKE #13_Word# oR
tH.Hybride LIKE #14_Word# oR
tBL.Blendling LIKE #15_Word# OR
tSo.Sorte LIKE #16_Word#

我知道使用 LIKE %xxx% OR 是一个繁重的过程。如果你想用另一种方式建议我,那很好。

我的预期结果是这样的:

________________________________________________________________________________
| | | |______________________________Detail_____________|
| Num | Gattung | Art | Form | Varietaet | ...... | the other cols|
|------|-------------|-------|-----------|------------|--------|---------------|
| 1. | Gattung A | Art A | Form A | | | |
| | | <same | Form B | | | |
| | | data or Art A> | | | | |
| | | Art B | Form B | Variant G | | |
| 2. | Gattung B | Art C | | Variant A | | |
-------------------------------------------------------------------------------
Back 1 2 3 Next

编辑 2:简明问题。

或者以树的形式,

                   ----- Art A ------ Form A
| |
| -- Form B
1. Gattung A ----|---- Art B ------ Form B ---- Variant G

2. Gattung B --------- Art C ------ Variant A

... And so on.

最佳答案

@exodream

谢谢你的评论。

这是我对你的问题的新回答,它避免了空行,如果你需要避免空列或需要其他东西让我知道。

<?php 

//make the labels
$labels=array('Gattung','Untergattung','Sektion','Untersektion','Serie',
'Unterserie','Art','Unterart','Varietaet','SubVarietaet','Form','Unterform',
'SpezialForm','Hybride','Blendling','Sorte' );

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());

//select database
mysql_query("drop database if exists `test_db`;")or die(mysql_error());
mysql_query("create database `test_db`;")or die(mysql_error());
mysql_select_db("test_db") or die(mysql_error());

//create table
mysql_query(" drop table if exists `test_table`;")or die(mysql_error());


mysql_query("
CREATE TABLE `test_table` (
`Gattung` char(30) NULL ,
`Untergattung` char(30) NULL ,
`Sektion` char(30) NULL ,
`Untersektion` char(30) NULL ,
`Serie` char(30) NULL ,
`Unterserie` char(30) NULL ,
`Art` char(30) NULL ,
`Unterart` char(30) NULL ,
`Varietaet` char(30) NULL ,
`SubVarietaet` char(30) NULL ,
`Form` char(30) NULL ,
`Unterform` char(30) NULL ,
`SpezialForm` char(30) NULL ,
`Hybride` char(30) NULL ,
`Blendling` char(30) NULL ,
`Sorte` char(30) NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;") or die(mysql_error());



mysql_query("INSERT INTO `test_table` VALUES
('Gattung_A',null,null,null,null,null,'Art_A',null,null,null,'Form_A',null,null,null,null,null ) ")
or die(mysql_error());

mysql_query("INSERT INTO `test_table` VALUES
('Gattung_A',null,null,null,null,null,'Art_A',null,null,null,'Form_A',null,null,null ,null,null ) ")
or die(mysql_error());

mysql_query("INSERT INTO `test_table` VALUES
('Gattung_B',null,null,null,null,null,'Art_C',null,'Variant_A',null,null,null,null,null,null,null ) ")
or die(mysql_error());

mysql_query("INSERT INTO `test_table` VALUES
(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ) ")
or die(mysql_error());


mysql_query("INSERT INTO `test_table` VALUES
('Gattung_Q',null,null,null,null,null,'Art_C',null,'Variant_A',null,'Form_W',null,null,null,null,null ) ")
or die(mysql_error());

mysql_query("INSERT INTO `test_table` VALUES
('Gattung_Q',null,'test','test','test','test','Art_S','test','Variant_Y','test','Form_H','test','test','test','test','last' ) ")
or die(mysql_error());


//get result
$result = mysql_query("SELECT * FROM `test_table` ;")
or die(mysql_error());



normal_display();
non_empty_row_display();
non_empty_column_display();

function non_empty_row_display()
{
//get data
global $result;
$final_array=array();

global $result;
mysql_data_seek ($result , 0);

print '<b><br><h2>TABLE: 2 NON EMPTY VALUES OF EACH ROW</h2></b>';
print '<table border=7 bordercolor=orange >';
$index=0;
$color=array('#66CCFF','#00FF00');
//for every row of the table
while($row = mysql_fetch_row($result))
{
$current_row='';
//for every coloumn
foreach ($row as $value)
//if value is not null
if($value!=null)//then keep it
$current_row .= $value.' | ';

if($current_row!='')
echo '<tr bgcolor=',$color[$index%2],'><th>ROW ',$index++,'</th><td>',$current_row,'</td></tr>',
'<tr><td colspan=2 height=5 bgcolor=pink></td></tr>';
}//while
echo '</table>';

}//function


function normal_display()
{
print '<b><h2>TABLE:1 Normal display</h2></b>';
print '<table border=1><tr>';
global $labels;
foreach($labels as $value)
echo '<th>',$value,'</th>';
print '</tr>';
global $result;
while($row = mysql_fetch_row($result))
{
print '<tr>';
foreach($row as $value)
echo '<td>',$value,'&nbsp;</td>';
print '</tr>';
}//while
print '</table>';
}//function



function non_empty_column_display()
{
//get data
global $result;
mysql_data_seek ($result , 0);

//for every row of the table
$final_column=array();
while($row = mysql_fetch_row($result))
//for every coloumn
foreach ($row as $key => $value)
//if value is not null
if($value!=null)
//if $final_column[$key] exists append value to it, otherwise assign value
$final_column[$key]=(isset($final_column[$key]))?$final_column[$key].=$value.'<br>':$value.' <br> ';


echo '<br><br><h2>Array:1 </h2>The coloumn with index 6 is placed before the one with index 2 in this array
because its first non empty value is fetched earlier. Check for your self:<br>';
echo '<pre>',strip_tags(print_r($final_column,true)),'</pre>';


//for each mysql table coloumn name
global $labels;
foreach($labels as $key => $value)
//if it exists in the result
if(array_key_exists($key,$final_column))
//keep it
$label_header=(isset($label_header))?$label_header.='<th>'.$value.'</th>':'<th>'.$value.'</th>';


ksort($final_column);
echo '<br><br><h2>Array:2 </h2>We can order this. Check for your self:<br>';
echo '<pre>',strip_tags(print_r($final_column,true)),'</pre>';

echo '<br><b><h2>TABLE:3 </h2>THIS IS A TABLE WITH NON EMPTY COLOUMNS<b>';
echo '<table border=1>',$label_header,'<tr>';
foreach($final_column as $value)
echo '<td valign=top>',$value,'</td>';
echo '</tr></table>';



}//function

?>

我已经修复了第 147 和 161 行(对于前面的示例),这是一个警告,我们必须在连接之前检查变量是否存在,在您的示例中它将是:

147:

$final_column[$key]=(isset($final_column[$key]))?$final_column[$key].=$value.'<br>':$value.' <br> ';

161:

$label_header=(isset($label_header))?$label_header.='<th>'.$value.'</th>':'<th>'.$value.'</th>';

关于PHP - 如何将数组中的数据显示到树或表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6286273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com