2017-02-11 60 views
1

我試圖從我找到的使用簡碼功能的wordpress表檢索數據。它工作並檢索所有列的數據。當我嘗試指定我想要的列時,它仍然給我一切。最終,我試圖做的是檢索我想要的列的數據,並使用CSS(而不是表格格式)來設置數據的樣式。我是新來的PHP,所以我想知道是否有人可以讓我知道,如果這甚至可能使用短代碼。如果有可能的話,任何幫助將不勝感激。謝謝你在前進,蘭迪使用簡碼從數據庫檢索數據

這裏是我使用的代碼:

if (!defined('WP_FD_ABSPATH')) 

define('WP_FD_ABSPATH', plugin_dir_path(__FILE__)); 

function wp_first_shortcode($atts){ 
global $wpdb; 
$a = shortcode_atts(array(
'table_name' => ", 
'columns' => ", 
), $atts); 
$showcolumns=array(); 
if($a['columns']!=""){ 
$column=$a['columns']; 
$showcolumns=explode(',',$a['columns']); 
} 
else{ 
$column='*'; 
$get_columns=$wpdb->get_results("describe ".$a['table_name'].""); 
foreach($get_columns as $fields) $showcolumns[]=$fields->Field; 
} 

$columncount=count($showcolumns); 

$result_query=$wpdb->get_results("select $column from ".$a['table_name'].""); 
echo '<table><tr>'; 
for($i=0;$i<$columncount;$i++) echo '<th>'.$showcolumns[$i].'</th>'; 
echo '</tr>'; 
foreach($result_query as $result_row){ 
echo '<tr>'; 
for($i=0;$i<$columncount;$i++) echo '<td>'.$result_row->$showcolumns[$i].'</td>'; 
echo '</tr>'; 
} 
echo '</table>'; 
} 
add_shortcode('fetchdata', 'wp_first_shortcode'); 
function table_value_function() { 
query_value(array('orderby' => 'date', 'order' => 'DESC' , 'showvalues' => "")); 
wp_reset_query(); 
return $return_string; 
} 
?> 

這裏是我使用簡碼:

[fetchdata table_name='my_wp_table' columns ='Display_Name' 'First_Name' 'Last_Name' 'Address_1' 'Address_2' 'City' 'State' 'Phone'] 
+0

我仍然想弄清楚如何只使用這個簡碼檢索我想顯示的列:[fetchdata table_name ='my_wp_table'columns ='Display_Name''First_Name''Last_Name''Address_1'' Address_2''City''State''Phone']目前我獲得了表格中每列的所有值。任何幫助,將不勝感激。預先感謝您,蘭迪 –

回答

0

好吧,我想你只是想擺脫的表格,因爲你想用CSS來設計它。請按照下面的代碼:

echo '<div class="something">'; 
for($i=0;$i<$columncount;$i++) echo '<h1>'.$showcolumns[$i].'</h1>'; 

foreach($result_query as $result_row){ 
echo '<div class="something_2">'; 
for($i=0;$i<$columncount;$i++) echo '<p>'.$result_row->$showcolumns[$i].'</p>'; 
echo '</div>'; 
} 
echo '</div>'; 
} 

希望,它有幫助。

+0

完美!我留下了一個積極的投票,但由於我是一個新手它沒有顯示。非常感謝你,我刪除了顯示列標題的回顯,因爲我將應用標籤。我沒有得到的是如何檢索我想要使用我的短代碼的列:[fetchdata table_name ='my_wp_table'columns ='Display_Name''First_Name''Last_Name''Address_1''Address_2''City''State ''電話'] –

+0

如果它回答你的問題,你可以接受答案。我稍後會爲你更新答案。 –

+0

好吧,我點了勾號,現在是綠色的。謝謝! –