Foreach with table

Asked

Viewed 234 times

0

I am creating a website that aims to show the games that are happening in the world in real time. I’m trying to make a go for the rows of the tables to be created "alone" according to the amount of games you will have at the moment coming from the API, I’m using the bet365 API. Attempt I made:

 <?php
  for(i=0;i<=$transaction['results'];i++){
  echo "<th></th>";
  }
 ?>

Following photo with layout template: inserir a descrição da imagem aqui

Note: I wanted the colors to be interspersed also as shown in the photo.

To display the results I am using the following code:

   foreach ($transaction['results'] as $element) {

                echo $element['league']['name'].PHP_EOL;
                echo $element['home']['name'].PHP_EOL;
                echo $element['away']['name'].PHP_EOL;
    }

JSON template coming from API

  {
   "success": 1,
   "pager": {
    "page": 1,
    "per_page": 1000,
    "total": 4
     },
     "results": [
     {
     "id": "77564080",
    "time": "1543528800",
    "time_status": "1",
    "league": {
        "id": "3024",
        "name": "Copa Libertadores - Feminino"
    },
    "home": {
        "id": "9105",
        "name": "EC Iranduba - Feminino"
    },
    "away": {
        "id": "170148",
        "name": "Atlético Huila - Feminino"
    },
    "ss": "1-0",
    "our_event_id": "1093051"
},
  • 2

    Do you intend to do all this via PHP? Because I believe that the ideal way would be to perform all the negotiations related to the organization of the table with Javascript, use PHP only to capture the API data and send to the Views in the IC. The IC itself already induces this structure.

  • I added the template that as comes the API codes, so yes I was thinking of doing everything in PHP.

1 answer

1


I didn’t understand what you couldn’t do, I made a quick example of what you show, here an image showing the result. You’ll have to change the css to look the way you did, but the base is already done, besides adjusting the column names and printing the right variables. If you have questions with Tables you can see here.

<table>
    <tr>
        <th>Coluna 1</th>
        <th>Coluna 2</th>
        <th>Coluna 3</th>
    </tr>
    <?php foreach ($json['results'] as $key => $element) { ?>
        <tr>
        <td><?php echo $element['league']['name'].'<br>'.$element['home']['name'].' X '.$element['away']['name']; ?></td>
        <td><?php echo $element['ss']; ?></td>
        <td><?php echo '00:00'; ?></td>
        </tr>
    <?php } ?>

</table>

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
  • Thank you! fajuchem +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.