Ridge Robinson Ridge Robinson - 5 months ago 18
PHP Question

PHP MySQL Foreach loop on Fetch_Array

I am working to build a management system to manage user bets (on sports games). There is a lot that goes into this, but knowing this will make explaining my question a little easier to understand.

I want to keep track of each individual user bet in a table similar to the following:

User Name Date Betting Site Parlay ID Home Team Away Team Wager Odds Bet Info Success
Ridge R 3/1/16 Bet365 1 Chelsea Man City 1000 2.0 Home Win Yes
Ridge R 3/2/16 Bet365 1 Barcelona Real Madrid 400 1.7 Home Win Yes
John D 3/4/16 William Hill 2 Wigan Newcastle 2100 1.4 Home Win No


I would get this info from our DB through joining a couple tables. We have many users, betting sites and obviously games that can be bet on. This table is then created from a while loop:

while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['First Name'].' '.$row['Last Name'].'</td>';
$output .= '<td>'.$row['Game Date'].'</td>';
$output .= '<td>'.$row['Betting Site Name'].'</td>';
$output .= '<td>'.$row['Parlay ID'].'</td>';
$output .= '<td>'.$row['Home Team'].'</td>';
$output .= '<td>'.$row['Away Team'].'</td>';
$output .= '<td>'.$row['Wager'].' kr</td>';
$output .= '<td>'.$row['Odds'].'</td>';
$output .= '<td>'.$row['Bet Info'].'</td>';
$output .= '<td>'.$row['Success'].'</td>';
$output .= '</tr>';

echo $output;
}


What I am having trouble with is this:

How can I join each bet record into the same where each of the parlay id's are the same? The reason I am asking is because I stripe my table by every other row with different color, but I need all games that are part of a parlay to be the same color.

I tried something similar to...

while($row = $result->fetch_array()) {
$output = '<tr>';
...
foreach ($row['Parlay ID'] as **COULDN'T FIGURE OUT THIS PART**) {
$output .= '<table><tr>';
$output .= '<td>'.$row['First Name' ].' '.$row['Last Name'].'</td>';
$output .= '</tr></table>';
.......
}
THEN CONTINUE TABLE FOR OTHER ONES
}


I know this is a pretty poor attempt, but I hope it gives across the idea I am going for! Perhaps a foreach loop is not each the right idea...any ideas on how this can work?

Answer

If you want same rows to share the same color you could:

$colors = array("red", "blue");
$lastParlayID = false; $lastColor = 1;
while($row = $result->fetch_array()) {
      if ($row["Parlay ID"] !== $lastParlayID) {
          $lastParlayID = $row["Parlay ID"];
          ++$lastColor;
      }
      if ($lastColor >= count($colors)) $lastColor=0;
      $output = '<tr style="background:'.$colors[$lastColor].'>';
      $output .= '<td>'.$row['First Name'].' '.$row['Last Name'].'</td>';
      $output .= '<td>'.$row['Game Date'].'</td>';
      $output .= '<td>'.$row['Betting Site Name'].'</td>';
      $output .= '<td>'.$row['Parlay ID'].'</td>';
      $output .= '<td>'.$row['Home Team'].'</td>';
      $output .= '<td>'.$row['Away Team'].'</td>';
      $output .= '<td>'.$row['Wager'].' kr</td>';
      $output .= '<td>'.$row['Odds'].'</td>';
      $output .= '<td>'.$row['Bet Info'].'</td>';
      $output .= '<td>'.$row['Success'].'</td>';
      $output .= '</tr>';
      echo $output;
}

You can also replace the $colors with CSS classes and do class= instead of style=.