3

I am echo'ing out results from a WP database plugin and I have managed to narrow down all the results required down to a post ID, it's now displaying all url records related to that post ID. (Scroll down on this link to see results: http://universitycompare.com/analytics-test/)

This is what I'm using to echo the SQL database records:

  <?php
echo "<strong>Stack Overflow Results</strong>";
echo "<br><div style='float:left; width:100%; padding:1em; background:#FFF; margin:2em 0;'>";
$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=37 AND `date` BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
for($i=0;$i<count($results);$i++)
{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
} ?>

I have a number of results being echoed that are related to the URL but are not specific, (see below for an example of whats being echoed). What I would like to do is only echo results that match this URL: http://universitycompare.com/universities/anglia-ruskin-uni/

universitycompare.com/universities/anglia-ruskin-uni/ (103) universitycompare.com/universities/anglia-ruskin-uni?offset=10 (1) universitycompare.com/universities/anglia-ruskin-uni/?offset=10 (1) universitycompare.com/universities/anglia-ruskin-uni (1)

Above is an example, I don't want to echo any results that don't match the chosen URL exactly. Ideally, only the record that should be shown, should be the the results with (103) next to it. I don't know the best possible way to do this.

I've looked a regex, but a little unsure? Is this something that I need to use?

Edit, (Answer to the above question, too low of a user score to answer own question):

      <?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

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);
}

echo "<strong>Stack Overflow Results</strong>";
echo "<br><div style='float:left; width:100%; padding:1em; background:#FFF; margin:2em 0;'>";
$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=37 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/anglia-ruskin-uni/'))

{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
}


?>
O O'Neill
  • 71
  • 5
  • I have already worked out the answer thanks to @Ahmed's EndsWith, by running the EndsWith as the entire URL, I am able to display only the full URL. Answer is now in the question. – O O'Neill Sep 20 '15 at 22:14

1 Answers1

1

You may be looking for startsWith(). Try the following:

if (startsWith($results[$i]['count'], 'universitycompare.com/universities/anglia-ruskin-uni'))
{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
}

implementation of startsWith() and endsWith() can be found here startswith-and-endswith-functions-in-php

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
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);
}
Community
  • 1
  • 1
Ahmed Ghonim
  • 4,455
  • 1
  • 17
  • 23
  • Hey @Ahmed, little unsure on how to implement this directly. startsWith() didn't work. I am getting this error: Fatal error: Call to undefined function startsWith() – O O'Neill Sep 20 '15 at 21:11
  • @OO'Neill, startsWith() is a user defined function. You need to define it before you can call it in the if statement. Copy the function definition into same script and try again. Take a look at http://www.w3schools.com/php/php_functions.asp – Ahmed Ghonim Sep 20 '15 at 21:15
  • Hi @Ahmed, now it echo's no results, but no errors? Do I need to edit the function? – O O'Neill Sep 20 '15 at 21:17
  • Are you trying to match against "h ttp://universitycompare.com/universities/anglia-ruskin-uni/" or "universitycompare.com/universities/anglia-ruskin-uni/"? Notice the leading http://. If you need to match the first, you need to change the if statement into if (startsWith($results[$i]['count'], 'h ttp://universitycompare.com/universities/anglia-ruskin-uni')) – Ahmed Ghonim Sep 20 '15 at 21:19
  • Yeah I have done that, I have done all the above - still no luck? :/ – O O'Neill Sep 20 '15 at 21:58
  • P.s. Even when I run just startsWith using just the letter 'h' I receive noting, so my assumption is that the startsWith isn't working. P.s.s - I have edited my question to add the additional code I am now using.. – O O'Neill Sep 20 '15 at 21:58
  • I have it working, we were using $results[$i]['count'] and not $results[$i]['uri'] - It's filtering correctly, how do we make it so it ends as requested and only narrows down that one as they all start the same. – O O'Neill Sep 20 '15 at 22:04