• Stop being a LURKER - join our dealer community and get involved. Sign up and start a conversation.

Reply to thread

Well... It debugged the code for me! LOL


This is just a test but I don't think it would take very much at all to have this thing to build out a full blown inventory plugin for basically any WordPress site.



[CODE=php]<?php

/*

Plugin Name: Test Vehicle Inventory Parser 3.0

Description: Parses a CSV file and stores the data in a MySQL database

Version: 1.0

Author: Rick Buffkin and ChatGPT

*/

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );


register_activation_hook( __FILE__, 'vehicle_inventory_parser_activate' );

add_action( 'vehicle_inventory_parser_cron_job', 'vehicle_inventory_parser_run' );

add_action( 'admin_menu', 'vehicle_inventory_parser_menu' );

add_action( 'admin_post_vehicle_inventory_parser_process', 'vehicle_inventory_parser_process' );


function vehicle_inventory_parser_activate() {

    if ( ! wp_next_scheduled( 'vehicle_inventory_parser_cron_job' ) ) {

        wp_schedule_event( time(), 'every_four_hours', 'vehicle_inventory_parser_cron_job' );

    }

}


function vehicle_inventory_parser_run() {

    vehicle_inventory_parser_process();

}


function vehicle_inventory_parser_process() {

    global $wpdb;

    $table_name = $wpdb->prefix . 'vehicle_inventory';

    $charset_collate = $wpdb->get_charset_collate();

    $csv_file = plugin_dir_path( __FILE__ ) . 'broadwayinven.csv';


    $sql = "CREATE TABLE IF NOT EXISTS $table_name (

        id mediumint(9) NOT NULL AUTO_INCREMENT,

        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,

        column1 text NOT NULL,

        column2 text NOT NULL,

        column3 text NOT NULL,

        PRIMARY KEY (id)

    ) $charset_collate;";


    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    dbDelta( $sql );


    $wpdb->query( "TRUNCATE TABLE $table_name" );


    if ( ( $handle = fopen( $csv_file, 'r' ) ) !== false ) {

        while ( ( $data = fgetcsv( $handle, 1000, ',' ) ) !== false ) {

            $col1 = $data[0];

            $col2 = $data[1];

            $col3 = $data[2];


            $wpdb->insert(

                $table_name,

                array(

                    'time' => current_time( 'mysql' ),

                    'column1' => $col1,

                    'column2' => $col2,

                    'column3' => $col3,

                )

            );

        }

        fclose( $handle );

    }


    $redirect_url = admin_url( 'admin.php?page=vehicle-inventory' );

    wp_redirect( $redirect_url );

    exit;

}


function vehicle_inventory_parser_menu() {

    add_menu_page( 'Vehicle Inventory', 'Vehicle Inventory', 'manage_options', 'vehicle-inventory', 'vehicle_inventory_parser_display_data', 'dashicons-chart-line', 6 );

}


function vehicle_inventory_parser_display_data() {

    if ( isset( $_GET['action'] ) && $_GET['action' ] == 'process' ) {

vehicle_inventory_parser_process();

}

global $wpdb;

$table_name = $wpdb->prefix . 'vehicle_inventory';

$results = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A );

echo '<h1>Vehicle Inventory</h1>';

echo '<p><a href="' . admin_url( 'admin-post.php?action=vehicle_inventory_parser_process' ) . '" class="button-primary">Process CSV</a></p>';

echo '<table class="wp-list-table widefat fixed striped posts">

<thead>

<tr>

<th>ID</th>

<th>Time</th>

<th>Column 1</th>

<th>Column 2</th>

<th>Column 3</th>

</tr>

</thead>

<tbody>';

foreach ( $results as $result ) {

echo '<tr>

<td>' . $result['id'] . '</td>

<td>' . $result['time'] . '</td>

<td>' . $result['column1'] . '</td>

<td>' . $result['column2'] . '</td>

<td>' . $result['column3'] . '</td>

</tr>';

}

echo '</tbody></table>';

}


register_deactivation_hook( __FILE__, 'vehicle_inventory_parser_deactivate' );


function vehicle_inventory_parser_deactivate() {

wp_clear_scheduled_hook( 'vehicle_inventory_parser_cron_job' );

}[/CODE]