SHARE
TWEET

Untitled

a guest Mar 20th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /* ****** обновление сразу нескольких строк за 1 запрос *********** */
  3. if( !empty($_POST['data']) ){
  4.     foreach ($_POST['data'] as $v) {
  5.         $post_title .= $wpdb->prepare("WHEN %d THEN %s ",(int)$v['timel_id'],sanitize_text_field($v['timel_title']));
  6.         $post_content .= $wpdb->prepare("WHEN %d THEN %s ",(int)$v['timel_id'],sanitize_text_field($v['timel_content']));
  7.         $post_date .= $wpdb->prepare("WHEN %d THEN %s ",(int)$v['timel_id'],sanitize_text_field($v['timel_date']));
  8.         $post_id .= (int)$v['timel_id'].",";
  9.     }
  10.     $post_id = substr($post_id, 0,-1);
  11.     // echo $post_title;
  12.     $update_query = "UPDATE $wpdb->posts SET
  13.                 post_title = CASE id {$post_title} END,
  14.                 post_content = CASE id {$post_content} END,
  15.                 post_excerpt = CASE id {$post_date} END WHERE id IN({$post_id})";
  16.     // echo $update_query."<hr>";
  17.    $wpdb->query($update_query);
  18. /*
  19.   UPDATE wp8k_posts SET
  20.     post_title = CASE id WHEN 10374 THEN 'alex title 1' WHEN 10375 THEN 'alex title 2' END,
  21.     post_content = CASE id WHEN 10374 THEN 'a desc 1' WHEN 10375 THEN 'a desc 2' END,
  22.     post_excerpt = CASE id WHEN 10374 THEN '21 Mar 2017' WHEN 10375 THEN '20 Mar 2017' END
  23.         WHERE id IN(10374,10375)
  24. */
  25. }
  26.  
  27. /* ****** обновление сразу нескольких строк за 1 запрос *********** */
  28.  
  29. /* ****** добавление сразу нескольких строк за 1 запрос *********** */
  30. if( !empty($_POST['new_data']) ){
  31.     $user = wp_get_current_user();
  32.     $member_id = $user->ID;
  33.     foreach ($_POST['new_data'] as $item) {
  34.         $val .= $wpdb->prepare("(%s,%s,%s,%d,%s),",sanitize_text_field($item['timel_title']),sanitize_text_field($item['timel_content']),sanitize_text_field($item['timel_date']),(int)$member_id,"alex_timeline");
  35.     }
  36.     $val = substr($val, 0,-1);
  37.     $insert_query = "INSERT INTO $wpdb->posts (post_title,post_content,post_excerpt,post_parent,post_type) VALUES {$val}";
  38.     echo $insert_query;
  39.     $wpdb->query($insert_query);
  40. }
  41. /*
  42. INSERT INTO wp8k_posts (post_title,post_content,post_excerpt,post_parent,post_type)
  43.     VALUES ('alex title 3','desc 3','15 Mar 2017',14,'alex_timeline'),
  44.         ('a title 4','desc 4','01 Mar 2017',14,'alex_timeline'),
  45.         ('a title 5','desc 5','04 Mar 2017',14,'alex_timeline')
  46. */
  47. /* ****** добавление сразу нескольких строк за 1 запрос *********** */
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top