0

I have an "Events" table in MySQL : EventsDate('id','event','start_date','end_date')

I'd like to check if multiple events have the same start date to show it differently in my HTML template. My SQL request is : SELECT * FROM EVENTSDATE where event='$id' and start_date>='$today' order by start_date asc

Now my foreach :

foreach ($upcomingDates as $value) { //$upcoming is the array with my sql request

        }

How can I say : "if you find two rows with the same start_date, echo something"

Upalr
  • 2,140
  • 2
  • 23
  • 33
Ty Yt
  • 466
  • 2
  • 9
  • 25

5 Answers5

1

I have a slightly different approach.

// Array to contain all values
$container = array();

// Loop through your existing array
foreach ($upcomingDates as $key => $value) {
    // Check if the value is already in the container array
    // If this is the case, its a duplicate.
    if (array_key_exists($value['start_date'], $container)) {
        $container[$value['start_date']]++;
        echo $value.' is a duplicate with key '.$key;
    }

    // Add each value to the array
    $container[$value['start_date']] = 1;
}

Another method is to use array_count_values()

foreach(array_count_values($upcomingDates) as $value => $c) {
    if ($c > 1) {
        echo $value.' is a duplicate';
    }
}

Note that the second option won't work if your $upcomingDates is an array of arrays.

Peter
  • 8,776
  • 6
  • 62
  • 95
0

You can make an empty array before the for loop, and add each value in as a key. Then, on each iteration you can check that array for the key, like so:

$values = [];
foreach ($upcomingDates as $value) { //$upcoming is the array with my sql request
    if(isset($values[$value])) //duplicate value found
        //do something here
    $values[$value] = 1;
}
ShaneOH
  • 1,454
  • 1
  • 17
  • 29
0

Since you're ordering your events by start_date:

for ($i = 0, $length = count($upcomingDates); $i < $length; $i++) {
    $date = $upcomingDates[$i];

    if (isset($upcomingDates[$i + 1]) &&
        $upcomingDates[$i + 1]['start_date'] == $date['state_date']) {
        echo 'this and the next date are equal';
    }
}
deceze
  • 510,633
  • 85
  • 743
  • 889
0

Try out GROUP BY

look here: GROUP BY

jrergon
  • 9
  • 1
  • Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. Futhermore: links will get deleted one day, then your posting wouldnt have any impact anymore. – B001ᛦ Jun 14 '16 at 14:09
0

if you want to find duplicates, then you can directly get it from database ex.
SELECT * FROM EVENTSDATE where event='$id' and start_date>='$today' GROUP BY start_date having count(start_date) > 1 order by start_date asc
or you can find duplicates from resulting array return only duplicated entries from an array

Community
  • 1
  • 1
Sanjay
  • 1,958
  • 2
  • 17
  • 25
  • Yes, already done in my SQL request but I would like to try in PHP. A little challenge ^^ Thanks ! – Ty Yt Jun 14 '16 at 14:21