• How would I put the below results into the WP Short code?

    PHP Results:

    <?php
    
     function endsWith($haystack, $needle) {
         // search forward starting from end minus needle length characters
         return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
     }
    
     $lastDate = date('Y-m-d', strtotime('today - 30 days'));
     $todayDate = date('Y-m-d');
    
     $results = $wpdb->get_results( "SELECT * FROM wp_statistics_pages WHERE id=1428 AND <code>date</code> BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
        for($i=0;$i<count($results);$i++)
        if (endsWith(get_site_url().''.$results[$i]['uri'], 'https://universitycompare.com/universities/the-uni-of-westminster/')) {
            echo $results[$i]['count'];
            echo ',<br />';  } ?>

    How do I insert the above ‘if results’ that are being echoed out into the below shortcode?

    <?php
    echo do_shortcode('[wp_charts title="linechart" canvaswidth="976px" canvasheight="244px" relativewidth="4" width="976px" height="244px" type="line" align="alignright" datasets=" { data to be here } "]');
    ?>
Viewing 1 replies (of 1 total)
  • “You need to change echo to return in shortcode:

    function endsWith($haystack, $needle) {
         // search forward starting from end minus needle length characters
         return $needle === """" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
     }
    
     $lastDate = date('Y-m-d', strtotime('today - 30 days'));
     $todayDate = date('Y-m-d');
    
    $out = '';
    
     $results = $wpdb->get_results( ""SELECT * FROM wp_statistics_pages WHERE id=1428 AND <code>date</code> BETWEEN '$lastDate' AND '$todayDate' "", ARRAY_A );
        for($i=0;$i<count($results);$i++)
        if (endsWith(get_site_url().''.$results[$i]['uri'], 'https://universitycompare.com/universities/the-uni-of-westminster/')) {
            $out .= $results[$i]['count'];
            $out .= ',<br />';  } ?>

    Then you can use the result of the execution in a shortcode.

Viewing 1 replies (of 1 total)
  • The topic ‘Run PHP Results inside WP Shortcode’ is closed to new replies.