2013-01-03 174 views
0

我有一個表,其值從dat_eb_registrants爲行(例如$row[1])和值從一水平陣列,從dat_eb_field_values萃取,我想拆分那些,所以我可以一切命令到表我想如何。拆分多達陣列成行

得到的數據如何把我的表:

$count = 0; 
while ($row = mysql_fetch_row($result)) 
{ 
echo "<tr>"; 
$myArray[] ="<tr><td>" . $row[9] . "</td><td>"; echo $myArray[$count]; 
$count++; 
echo "</tr>"; 
} 

如何將所有的數據從數據庫(是的,我知道這是舊的)提取:

SELECT dr.id, dr.first_name, dr.last_name, dr.email, dr.comment, dr.amount, dr.published, dr.transaction_id, dr.register_date, GROUP_CONCAT(df.field_value SEPARATOR '</td><td>') 
    FROM dat_eb_registrants dr 
    LEFT JOIN dat_eb_field_values df 
    ON dr.id=df.registrant_id 
    WHERE `event_id` >= 20 AND `event_id` <= 25 
    GROUP BY dr.id 
    ORDER BY '".$sort."', '".$ascdsc."' 

現在,我想將第一個表(例如$row[1])和(例如$row[2])的一些行垂直放置在數組中間。我怎樣才能做到這一點?

因爲數組填寫一次我的表,並使用$rows,你可以簡單地告訴要顯示的行,其中...

這就是我想要的東西(代碼可能不正確):

while ($row = mysql_fetch_row($result)) { 
    echo "<tr>"; 
echo "<td>"'.$row_table_1[1]'"</td>"; 
echo "<td>"'.$row_table_1[2]'"</td>"; 
echo "<td>"'.$row_table_2[35]'"</td>"; 
echo "<td>"'.$row_table_2[45]'"</td>"; 
echo "<td>"'.$row_table_1[5]'"</td>"; 
echo "<td>"'.$row_table_2[6]'"</td>"; 
echo "</tr>"; 
} 

我以前做的:

echo "<td>"; $result24 = mysql_query("SELECT field_id, field_value FROM dat_eb_field_values WHERE (field_id = 88) AND (registrant_id = $row[0])"); $r24 = mysql_fetch_row($result24); echo $r24[1]; echo "</td>"; 
echo "<td>"; $result25 = mysql_query("SELECT field_id, field_value FROM dat_eb_field_values WHERE (field_id = 57) AND (registrant_id = $row[0])"); $r25 = mysql_fetch_row($result25); echo $r25[1]; echo "</td>"; 
echo "<td>" . $row[5] . "</td>"; 
echo "<td>" . $row[6] . "</td>"; 

我想做的事:

echo "<td>" . $rowfromsecondtable[1] . "</td>"; 
echo "<td>" . $rowfromsecondtable[2] . "</td>"; 
echo "<td>" . $row[5] . "</td>"; 
echo "<td>" . $row[6] . "</td>"; 

(使用我上面給的代碼)

dat_eb_registrants預覽:

| id | first_name | last_name | email    | 
------------------------------------------------------------------------ 
    | 1 |  Mike  |  Doe  | [email protected] | 
    | 2 |  John  |  Smith  | [email protected] | 

dat_eb_field_values預覽:

field 1 = fav.sport 
field 2 = fav. color 
field 3 = fav. food 

    | registrant_id | field_id | field_value | 
---------------------------------------------------------- 
    | 1    |  1  |  tennis  | 
    | 1    |  2  |  green  | 
    | 1    |  3  |  spagetti  | 
    | 2    |  1  |  hockey  | 
    | 2    |  2  |  red   | 
    | 2    |  3  |  fish   | 

我需要:

first_name | id | fav.sport | last_name | fav.food | 
--------------------------------------------------------------------- 
Mike  | 1 |  Tennis |  Doe  | spagetti | 
John  | 2 |  Hockey |  Smith  | fish  | 
+0

努力去理解這個問題。你能提供一個更具體的例子嗎? – eggyal

+0

當然,請參閱更改 – Laurent

+0

並再次:)該'mysql'擴展名被oudated,不維護,並將被標記爲不贊成使用PHP5.5。使用'PDO_MYSQL'或'MySQLi'。 http://php.net/en/mysql-connect – KingCrunch

回答

0

也許我失去了一些東西,但爲什麼不直接在SQL執行此任務。這基本上是樞軸點。MySQL沒有一個支點,但你可以使用聚合函數與CASE聲明:

select r.first_name, 
    r.id, 
    r.last_name, 
    max(case when f.field_id =1 then f.field_value else null end) As FavSport, 
    max(case when f.field_id =2 then f.field_value else null end) As FavColor, 
    max(case when f.field_id =3 then f.field_value else null end) As FavFood 
from dat_eb_registrants r 
left join dat_eb_field_values f 
    on r.id = f.registrant_id 
group by r.first_name, r.id, r.last_name 
order by r.id 

SQL Fiddle with Demo

查詢的結果是輸出要:

| FIRST_NAME | ID | LAST_NAME | FAVSPORT | FAVCOLOR | FAVFOOD | 
---------------------------------------------------------------- 
|  Mike | 1 |  Doe | tennis | green | spagetti | 
|  John | 2 |  Smith | hockey |  red |  fish | 

或者您可以使用在dat_eb_field_values表中的多個聯接:

select r.first_name, 
    r.id, 
    r.last_name, 
    fSport.field_value FavSport, 
    fColor.field_value FavColor, 
    fFood.field_value FavFood 
from dat_eb_registrants r 
left join dat_eb_field_values fSport 
    on r.id = fSport.registrant_id 
    and fSport.field_id = 1 
left join dat_eb_field_values fColor 
    on r.id = fColor.registrant_id 
    and fColor.field_id = 2 
left join dat_eb_field_values fFood 
    on r.id = fFood.registrant_id 
    and fFood.field_id = 3 
order by r.id 

請參閱SQL Fiddle with Demo。它產生相同的結果。

0

這確實不是回答你的問題,而是使用$ row [9],$ row [10]等等,你可以使用表的列名作爲行索引。

$行[ 'FIRST_NAME'] $行[ '姓氏']

等。它會使你的代碼更具可讀性。

您可能還想先獲取所有數據,然後生成您的html代碼。它看起來更乾淨。

e.g

$result24 = mysql_query(sometihng here); 
$r24 = mysql_fetch_row($result24); 

$result25 = mysql_query(sometihng else here); 
$r25 = mysql_fetch_row($result25); 

//php code for table starts here 
echo '<table>';