0

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 `ucsite_statistics_pages` WHERE id=1428 AND `date` BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
    for($i=0;$i<count($results);$i++)
    if (endsWith(get_site_url().''.$results[$i]['uri'], 'http://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 } "]'); 
?>
  • 1
    edit your question to show us the code of your shortcode – mmm Sep 21 '15 at 13:30
0

If I understand correctly, you have a shortcode called wp_charts that accepts a pramaeter datasets, and you want to create a new shortcode that runs a database query and embeds the results of the query as the value of datasets?

If so, there are several ways to do this, but I think the easiest - especially if you didn't write wp_charts yourself - is to create a new shortcode that runs the query and then calls the wp_charts shortcode.

For example:

function my_charts_with_data_shortcode_impl($atts, $content = null) {
    $sql_query = ...
    $results = $wpdb->get_results($sql_query, ARRAY_A );
    $datasets = [];
    foreach ($results as $row) {
        if (endsWith(get_site_url().''.$row['uri'], 'http://universitycompare.com/universities/the-uni-of-westminster/')) {
            $datasets[] = $row['count'];
        }
    }

    return do_shortcode('[wp_charts '.
        'title="linechart" canvaswidth="976px" canvasheight="244px" relativewidth="4" '.
        'width="976px" height="244px" type="line" align="alignright" '.
        'datasets="'. join(",",$datasets) . '"]');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.