简体   繁体   中英

PHP if name appears more than once in foreach loop

I would like to determine if the same team name (that is outputted with the variables $home_team['team_name'] and $away_team['team_name'] ) appears more than once inside the

if ($game_date_converted == $next_monday) 

statement.

If the team name does appear more than once (for example, if it appears twice), I would like that text to be displayed in RED.

In plain English, for the list of games, if the team name (which could be either home team or away team) appears twice show that team name in RED.

Thank you for your help!

<?php foreach ($games as $game) { ?>

<?php

$team_id = $game['home_team_id'];
$home_team = sw::shared()->teams->getForID($team_id); 

$away_team_id = $game['away_team_id'];
$away_team = sw::shared()->teams->getForID($away_team_id); 

$leagues = sw::shared()->packages->getAll(); 

$game_date = $game['date'];
$game_date_converted = date('Y-m-d', strtotime($game_date));

$date_converted = date('l', strtotime($game_date));
?>

<?php if ($game_date_converted == $next_monday) { ?>
    <tr>
        <td><?php echo $home_team['team_name']; ?></td>
        <td><?php echo $away_team['team_name']; ?></td>


    <!-- more HTML -->

If you wish to style the way it is displayed, you may want to add a classname (yourclass) to the parsing of the teams and then apply css.

<td><?php echo "<span class='home-team'>" . $home_team['team_name'] . "</span>"; ?></td>
<td><?php echo "<span class='away-team'>" . $away_team['team_name'] . "</span>"; ?></td>

In the head section you can add your jquery library:

Then add the jquery script:

    $(document).ready(function(){

        $(".home-team").not(":first").css("color","red");
        $(".away-team").not(":first").css("color","red");
   });

Best to see this in a live situation, so I createed a fiddle: http://jsfiddle.net/djwave28/TNAGr/13/

Adjusted it again. No problem !! All rendered teams have a span with a class, so they can be read out with javascript. I then read the text content in them and put that in an array and see if I have multiple values. If yes, I look for all the ones in the HTML that gave me a double content and add a class to them.

<?php echo "<td><span class='team'>" . $home_team['team_name'] . "</span></td>"; ?>
<?php echo "<td><span class='team'>" . $away_team['team_name'] . "</span></td>"; ?>

  $(document).ready(function () {
            var teams = Array();
            $(".team").each(function () {

                teams.push($(this).text());

            });
            teams.sort();

            for (var i = 0; i < teams.length; i++) {

                if (teams[i] === teams[i + 1]) {

                    $(".team").each(function () {

                        if ($(this).text() == teams[i]) {
                            $(this).addClass("doubleteam");
                        }
                    });
                }
            }

        });

See: http://jsfiddle.net/djwave28/TNAGr/37/

This runs through your schedule display for that day and looks for doubles teams. If it find them, it will add the class "doubleteam". You can style this class to your desire in the CSS stylesheet. Further you can also style your team names as a whole in the CSS sheet. I hope this is exactly what your looking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM