Wordpress:获取数据并排序

时间:2014-01-25 19:32:49

标签: php mysql wordpress

表名:wp_ninja_forms_subs

Data in table

id  (int)     = 1 
user_id (int)  = 1
form_id (int)  = 1
data (longtext)    = `a:12:{i:0;a:2:{s:8:"field_id";i:16;s:10:"user_value";s:4:"test";}i:1;a:2:{s:8:"field_id";i:6;s:10:"user_value";s:4:"test";}i:2;a:2:{s:8:"field_id";i:15;s:10:"user_value";s:14:"(808) 080-8080";}i:3;a:2:{s:8:"field_id";i:8;s:10:"user_value";s:14:"asdf@gmail.com";}i:4;a:2:{s:8:"field_id";i:10;s:10:"user_value";s:6:"sfasdf";}i:5;a:2:{s:8:"field_id";i:3;s:10:"user_value";s:12:"ksfasdlkfadf";}i:6;a:2:{s:8:"field_id";i:11;s:10:"user_value";s:0:"";}i:7;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:5:"Admin";}i:8;a:2:{s:8:"field_id";i:13;s:10:"user_value";s:8:"Forgtown";}i:9;a:2:{s:8:"field_id";i:14;s:10:"user_value";s:8:"sadfasdf";}i:10;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:1:"7";}i:11;a:2:{s:8:"field_id";i:5;s:10:"user_value";s:0:"";}}`

date_updated = 2014-01-25 11:02:01

查询

$subs_results = $wpdb->get_results("SELECT * FROM wp_ninja_forms_subs WHERE form_id = 1 ORDER BY date_updated DESC")

如何打印“数据”字段值?

此外,我想通过“数据(电话)”进行数据排序。有可能吗?

2 个答案:

答案 0 :(得分:0)

默认情况下$wpdb->get_results()会返回一个对象数组,因此您可能需要这样:

foreach ($subs_results as $sub)  {
    echo $sub->data;
}

http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

请注意,它会在wp_ninja_forms_subs表中为每个记录打印此字段的值。

答案 1 :(得分:0)

以下代码会将此字符串转换为数组并打印“data”字段值...

foreach ($subs_results as $sub)
{
    $data = unserialize($sub->data);
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}