我想在wordpress页面上显示自定义表格中的查询:
table name: wp_formmaker_submits
要显示的字段:element_value(每个数据有6行)
这是查询结果:
2014-07-2211:13:00SpeedMacLindseyNewYork2014-07-2211:22:00FedExJackChloeLondon2014-07-2211:42:00DHLSteveDannoHawaii
它应该看起来像
> 2014-07-22 | 11:13:00 | Speed | Mac | Lindsey | New York
> 2014-07-22 | 11:22:00 | FedEx | Jack | Chloe | London
> 2014-07-22 | 11:42:00 | DHL | Steve | Danno | Hawaii
我需要<tr>
和<td>
结构的帮助,因此html表格看起来会正确。
这是我的代码:
global $wpdb;
$result = $wpdb->get_results("SELECT element_value FROM wp_formmaker_submits WHERE DATE(date) = CURDATE()");
echo "<table border='1'>";
echo "<tr><td>Date</td><td>Time</td><td>Carrier</td><td>Driver</td><td>Co Driver</td><td>Destination</td></tr>";
foreach($result as $wp_formmaker_submits){
echo "<td>".$wp_formmaker_submits->element_value."</td>";
}
echo "</table>";
printr:
Array ( [0] => stdClass Object ( [id] => 7 [form_id] => 10 [element_label] => 1 [element_value] => 2014-07-22 [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [1] => stdClass Object ( [id] => 8 [form_id] => 10 [element_label] => 2 [element_value] => 11:13:00 [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [2] => stdClass Object ( [id] => 9 [form_id] => 10 [element_label] => 3 [element_value] => Speed [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [3] => stdClass Object ( [id] => 10 [form_id] => 10 [element_label] => 4 [element_value] => Mac [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [4] => stdClass Object ( [id] => 11 [form_id] => 10 [element_label] => 5 [element_value] => Lindsey [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [5] => stdClass Object ( [id] => 12 [form_id] => 10 [element_label] => 6 [element_value] => New York [group_id] => 2 [date] => 2014-07-22 04:14:01 [ip] => ::1 ) [6] => stdClass Object ( [id] => 13 [form_id] => 10 [element_label] => 1 [element_value] => 2014-07-22 [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [7] => stdClass Object ( [id] => 14 [form_id] => 10 [element_label] => 2 [element_value] => 11:22:00 [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [8] => stdClass Object ( [id] => 15 [form_id] => 10 [element_label] => 3 [element_value] => FedEx [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [9] => stdClass Object ( [id] => 16 [form_id] => 10 [element_label] => 4 [element_value] => Jack [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [10] => stdClass Object ( [id] => 17 [form_id] => 10 [element_label] => 5 [element_value] => Chloe [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [11] => stdClass Object ( [id] => 18 [form_id] => 10 [element_label] => 6 [element_value] => Hawaii [group_id] => 3 [date] => 2014-07-22 04:22:50 [ip] => ::1 ) [12] => stdClass Object ( [id] => 19 [form_id] => 10 [element_label] => 1 [element_value] => 2014-07-22 [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) [13] => stdClass Object ( [id] => 20 [form_id] => 10 [element_label] => 2 [element_value] => 11:42:00 [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) [14] => stdClass Object ( [id] => 21 [form_id] => 10 [element_label] => 3 [element_value] => DHL [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) [15] => stdClass Object ( [id] => 22 [form_id] => 10 [element_label] => 4 [element_value] => Steve [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) [16] => stdClass Object ( [id] => 23 [form_id] => 10 [element_label] => 5 [element_value] => Danno [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) [17] => stdClass Object ( [id] => 24 [form_id] => 10 [element_label] => 6 [element_value] => Hawaii [group_id] => 4 [date] => 2014-07-22 04:43:31 [ip] => ::1 ) )
答案 0 :(得分:3)
刚编辑了你的代码。看看是否有效 -
global $wpdb;
$result = $wpdb->get_results("SELECT element_value FROM wp_formmaker_submits WHERE DATE(date) = CURDATE()");
echo "<table border='1'>";
echo "<tr><th>Date</th><th>Time</th><th>Carrier</th><th>Driver</th><th>Co Driver</th><th>Destination</th></tr>";
$count = 0;
foreach($result as $wp_formmaker_submits){
$new = $wp_formmaker_submits->element_label;
if($new == 1){
if( $count > 0 )
echo "</tr>";
echo "<tr>";
}
echo "<td>".$wp_formmaker_submits->element_value."</td>";
$count++;
}
echo "</tr>";
echo "</table>";