How a string of text every other two lines in a foreach loop in PHP? (retrieving records from a database)

I am working on a "boxing records" database for a school project. The loop retrieves records from a SQL statement. I want to add a "VS" string of text between every other two lines in order to show records outputted somewhat like this.

Upcoming Fights

Sergey Kovalev (28-0-25) VS Jean Pascal (30-3-17)

Another Boxer (123-0-5) VS Some Boxer (123-3-1)

However, my current loop outputs like this

Sergey Kovalev (28-0-25) VS Jean Pascal (30-3-17) VS Another Boxer (123-3-1) VS Some Other Boxer (123-3-1) VS

The loop I currently have is the following

foreach($records as $record) {
              $i = 0;
              echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";
              $i=$i*2;
              if($i%2 == 0)
              {
                echo "VS <br/>";
              }
              else{
                echo "<br />";
              }

I know I could probably change the SQL in order to display two fighters in the same row, and then append "vs" on the echo, but I thought that just modifying the for loop would work by using a variable counter $i. I thought it would be pretty easy to make the "VS" appear between every two rows but im missing something in my logic.

Answers


You need to increment value of $i by one rather than multiplying it by 2 and initialize $i outside foreach loop.

$i = 0; // Initialize counter here
foreach($records as $record) {

    echo $record['name'] . " (" . $record['wins'] . "-" . $record['losses'] . "-" . $record['kos'] . ")" . "<br>";

    if($i%2 == 0)
    {
        echo "VS <br/>";
    }
    else
    {
        echo "<br />";
    }
    $i++; // Increment counter here
}

Need Your Help

Should we drop stored procedures and run database calls from java programs

java database db2 ibm-midrange db2-400

I am fighting to keep the use of stored procedures in our company. There are a few people who say they are bad and we should not use them. We are using DB2 on the i-series.

Chef Recipe Group not working

chef chef-recipe

I'm getting the following error when I try to run my Chef recipe and I am at a loss as to what I'm doing wrong. I'm only really getting started with it.