尝试根据SQL表中的数据在帖子中创建HTML表

时间:2018-10-24 14:43:43

标签: php mysql sql wordpress custom-post-type

我正在尝试在我的wordpress安装中通过几个不同的SQL表构建大量的循环。我能够从sql表中的数据创建自定义帖子,但是将这些表本身的数据插入到帖子中是另一回事。有时,foreach循环会重复发生,因此我也遇到了这个问题。

function runposts(){

    global $wpdb;
    global $row;
    $rows = $wpdb->get_results("SELECT * FROM routes_txt");
    foreach ( $rows as $row ) {

      $route_name = $row->route_short_name;

      if (get_page_by_title($route_name, OBJECT, 'routes')) {

      } else {

      $table =  '<h1>'. $route_name .'</h1>';


       $trips = $wpdb->get_results("SELECT * FROM trips_txt WHERE route_id = '$row->route_id'");
       foreach ( $trips as $trip ) {
           $days  = $wpdb->get_results("SELECT * FROM calendar_txt WHERE service_id = '$trip->service_id'");
           foreach ( $days as $day ) {
             $day->start_date;
             $startdate = date("m-d-Y", strtotime($day->start_date));  
             $day->end_date;
             $startweekday = "";
             $endweekday = "";
             if ( $day->monday = 1 ){
                 $startweekday = "Mon";
             } else if ( $day->tuesday = 1 ){
                 $startweekday = "Tues";
             } else if ( $day->wednesday = 1 ){
                 $startweekday = "Wed";
             } else if ( $day->thursday = 1 ){
                 $startweekday = "Thur";
             } else if ( $day->friday = 1 ){
                 $startweekday = "Fri";
             } else if ( $day->saturday = 1 ){
                 $startweekday = "Sat";
             } else if ( $day->Sunday = 1 ){
                 $startweekday = "Sun";
             }    
             if ( $day->monday = 1 ){
                 $endweekday = "Mon";
             }
             if ( $day->tuesday = 1 ){
                 $endweekday = "Tues";
             }
             if ( $day->wednesday = 1 ){
                 $endweekday = "Wed";
             }
             if ( $day->thursday = 1 ){
                 $endweekday = "Thur";
             }
             if ( $day->friday = 1 ){
                 $endweekday = "Fri";
             }
             if ( $day->saturday = 1 ){
                 $endweekday = "Sat";
             }
             if ( $day->Sunday = 1 ){
                 $endweekday = "Sun";
             }    
           };
           $table .= '<h2>Effective '. $startdate .' ';
           $table .= $startweekday;
           $table .= ' - ';
           $table .= $endweekday .'</h2>';
           $table .= '<table><tr>';
           $offdays  = $wpdb->get_results("SELECT * FROM calendar_dates_txt WHERE service_id = '$trip->service_id' AND exception_type = 2");
           foreach ( $offdays as $offday ) {
             $offday->date;
           };
           $stops = $wpdb->get_results("SELECT * FROM stops_txt WHERE stop_id = '$trip->stop_id'");
           foreach ( $stops as $stop ) {
             $table .= '<td><table data-lat="'. $stop->stop_lat .'"  data-lon="'. $stop->stop_lon .'"><thead><tr><th>'.$stop->stop_name.'</th></tr></thead><tbody>';
             $stoptimes = $wpdb->get_results("SELECT * FROM stop_times_txt WHERE stop_id = $stop->stop_id");
             foreach ( $stoptimes as $stoptime ) {
               $table .= '<tr><td>'. $stoptime->arrival_time .'</td></tr>';
             }
             $table .= '</tbody></table></td>';
           }
           $table .= '</tr></table>';
       }



        // Insert the post into the database
        $my_post = array(
          'post_title'    => $row->route_short_name,
          'post_content'  => $table,
          'post_status'   => 'publish',
          'post_type'     => 'routes',
          'post_author'   => 1
        );

        wp_insert_post( $my_post );
    };
};
}

add_action('init', 'runposts');

0 个答案:

没有答案