使用数组值将行插入数据库

时间:2013-04-15 00:49:02

标签: php database wordpress

我无法在下面的函数中获取最后一个数组以插入表中。我猜测语法问题,但我找不到它并需要帮助。该函数在Wordpress安装时插件激活时插入数据库值:

function my_plugin_install_function() {
global $wpdb;
$table_name = $wpdb->prefix . "revslider_sliders";
$wpdb->insert( $table_name, array( 
           'title' => 'test',
           'alias' => 'test2',
           'params' => array (
                'title' => 'Homepage Slider',
                'alias' => 'homepage-slider',
                'shortcode' => '[rev_slider homepage-slider]',
                'slider_type' => 'fixed',
                'width' => 875,
                'height' => 350,
                'responsitive_w1' => 'NULL',
                'responsitive_sw1' => 'NULL', 
                'responsitive_w2' => 'NULL', 
                'responsitive_sw2' => 'NULL',
                'responsitive_w3' => 'NULL',
                'responsitive_sw3' => 'NULL',
                'responsitive_w4' => 'NULL',
                'responsitive_sw4' => 'NULL',
                'responsitive_w5' => 'NULL',
                'responsitive_sw5' => 'NULL',
                'responsitive_w6' => 'NULL',
                'responsitive_sw6' => 'NULL',
                'delay' => 8000,
                'touchenabled' => 'on',
                'stop_on_hover' => 'on',
                'shuffle' => 'off',
                'load_googlefont' => 'true',
                'google_font' => 'PT+Sans',
                'stop_slider' => 'off', 
                'stop_after_loops' => 0, 
                'stop_at_slide' => 2, 
                'position' => 'center', 
                'margin_top' => 0, 
                'margin_bottom' => 0,
                'margin_left' => 0, 
                'margin_right' => 0, 
                'shadow_type' => 3, 
                'show_timerbar' => 'false', 
                'timebar_position' => 'top',
                'background_color' => '#fff', 
                'padding' => 5, 
                'show_background_image' => 'true', 
                'background_image' => 'NULL', 
                'navigation_type' => 'bullet', 
                'navigation_arrows' => 'verticalcentered', 
                'navigation_style' => 'navbar-old', 
                'nav_offset_hor' => 0, 
                'nav_offset_vert' => 20, 
                'navigaion_always_on' => 'false', 
                'hide_thumbs' => 200, 
                'thumb_width' => 100, 
                'thumb_height' => 50, 
                'thumb_amount' => 5, 
                'hide_slider_under' => 0, 
                'hide_defined_layers_under' => 0, 
                'hide_all_layers_under' => 0,
                'jquery_noconflict' => 'on',
                'js_to_body' => 'false', 
                'output_type' => 'none',
                )
         ), array('%s', '%s', '')
   );

2 个答案:

答案 0 :(得分:0)

出现一个疯狂的猜测:

'params' => array (//...
    'output_type' => 'none',
)

,

之后取消最后一个逗号('none'

线成为

'output_type' => 'none'

我不熟悉WP函数,但这是常见的语法错误。这肯定会显示PHP错误。

答案 1 :(得分:0)

文档http://codex.wordpress.org/Class_Reference/wpdb说:

  

可能的格式值:%s为字符串; %d为十进制数;和%f为   浮。

您无法将Array()添加到数据库。这对我和函数$ wpdb-> insert()没有意义,因为第三个参数的array('%s', '%s', '')第三个值:''需要是以下之一:%s,%d或%f

我有两个建议

方法1

您可以更改数据库结构(示例列):

title, alias, params_title, params_alias, params_shortcode, ... , params_output_type

并像这样单独保存params:

$wpdb->insert( $table_name, array( 
           'title' => 'test',
           'alias' => 'test2',
           'params_title' => 'Homepage Slider',
           'params_alias' => 'homepage-slider',
           'params_shortcode' => '[rev_slider homepage-slider]', //(...)
            ), array('%s', '%s', '%s', '%s', '%s') // Add as many %s or %d as needed
           );

方法2

或保存params array();像这样的字符串(参见那些“”-characters)"array (/* ... */)"

$wpdb->insert( $table_name, array( 
           'title' => 'test',
           'alias' => 'test2',
           'params' => "array (
                'title' => 'Homepage Slider',
                'alias' => 'homepage-slider',
                'shortcode' => '[rev_slider homepage-slider]',
                /* ... */
           )"
         ), array('%s', '%s', '%s')
   );

在PHP代码中,你可以从数据库中获取这些内容

eval("\$params = \"{$row['params']}\";");

eval()将$ params-variable设置为:

$params = array (
                'title' => 'Homepage Slider',
                'alias' => 'homepage-slider',
                'shortcode' => '[rev_slider homepage-slider]',
                /* ... */
                );

知道了吗:)?