• Resolved willemdh

    (@willemdh)


    Hello,

    I’m trying to use PHP to submit a value to a tablepress table cell.

    This is the code:

    <?php
        // Defining the basic cURL function
        function curl($url) {
            $ch = curl_init();  // Initialising cURL
            curl_setopt($ch, CURLOPT_URL, $url);    // Setting cURL's URL option with the $url variable passed into the function
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
            $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
            curl_close($ch);    // Closing cURL
            return $data;   // Returning the data from the function
        }
    
        // Defining the basic scraping function
        function scrape_between($data, $start, $end){
        	$data = stristr($data, $start); // Stripping all data from before $start
        	$data = substr($data, strlen($start));  // Stripping $start
        	$stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
        	$data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
        	return $data;   // Returning the scraped data from the function
        }
    
        $scraped_website = curl("http://exchange.nagios.org/directory/Plugins/Operating-Systems/Windows/NRPE/Check-Microsoft-Windows-Scheduled-Tasks/details");  
    
        $scraped_data = scrape_between($scraped_website,'Hits</div><div class="data">','</div></div><div class="row">');
    
        echo $scraped_data
    ?>

    It works when I run it at home in my WAMP server, but it does not display anything in the tablepress table http://outsideit.net/check_ms_win_tasks/

    I have php in tables extension for Tablepress installed.
    Is there anything I’m doing wrong? Is there anything I can do to make it work?

    Thanks for any advice on this.

    Grtz

    Willem

    https://wordpress.org/plugins/tablepress/

Viewing 1 replies (of 1 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    Where exactly is this PHP code? In the cell of a TablePress table?
    That is absolutely not a good idea, as this would result in an HTTP request on every page load that shows the table.

    The best solution for this is probably to use the WordPress HTTP API, instead of messing with cURL yourself. Then, you should probably also use the Transient API for caching (so that you don’t run the HTTP request too often). With that, you could create a Shortcode that you then use inside the table cells.

    (By the way: The HTML on the page in your link is invalid. The table is wrapped by a <h3> tag for headings. Can you please check that after switching from the “Visual” to the “Text” editor on the “Edit” screen of the page.)

    Regards,
    Tobias

Viewing 1 replies (of 1 total)

The topic ‘Trying to run PHP code to submit values in tablepress cell’ is closed to new replies.