1

I have a site who are gonna show which games are running. They are in a table and each game have a th. I wants to make them different from each other. Ex if the game are HS U11A then it should be th class=blue

Current script:

$sql = "SELECT kamp, kategori, raekke, tid, loebenr FROM turneringsinfo WHERE loebenr LIKE '1' ORDER BY loebenr ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<th>" . $row["kategori"]. ' ' . $row["raekke"]. "</th>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

Outputs:

<table>
<th>HS U11 A</th>
<th>DS U13 B</th>
<th>HS U17 A</th>
<th>HS SEN A</th>
<th>HS SEN D</th>
<th>HS U11 B</th>
<th>HS U19 A</th>

Wanted output:

<table>
<th class="red">HS U11 A</th>
<th class="green">DS U13 B</th>
<th class="blue">HS U17 A</th>
<th>HS SEN A</th>
<th>HS SEN D</th>
<th>HS U11 B</th>
<th>HS U19 A</th>

What I've found that I think could give help me in this case

$setning  = "You should eat fruits, vegetables, and fiber every day.";
$gammeltord = array("fruits", "vegetables", "fiber");
$nyttord   = array("pizza", "beer", "ice cream");
$nysetning = str_replace($gammeltord, $nyttord, $setning);


echo "$nysetning"

The site is also live so you can see the visuals here: http://turneringsportalen.no/beta/tidslinje.php

Hope i have explained myself and are looking forward for your help :)

Regards Øystein

1 Answer 1

1

I'm not sure how these game names are generated, or perhaps they are static strings but since you specifically ask to tag those values:

$tags = [
  'HS U11 A' => 'red',
  'DS U13 B' => 'green',
  'HS U17 A' => 'blue'
];

$sql = "SELECT kamp, kategori, raekke, tid, loebenr FROM turneringsinfo WHERE loebenr LIKE '1' ORDER BY loebenr ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "<table>";
  // output data of each row
  while($row = $result->fetch_assoc()) {
    if(array_key_exists(($name = $row["kategori"]. ' ' . $row["raekke"]), $tags)){
      echo "<th class='".$tags[$name]."'>" . $name. "</th>";
    } else {
      echo "<th>" . $name. "</th>";
    }
  }
  echo "</table>";
} else {
  echo "0 results";
}

This works by checking if the key of array $tags exists (setting a new var $name with those values) and print out if it exists.

Sign up to request clarification or add additional context in comments.

7 Comments

Is it a missing { or } somewhere in your code? Just getting error and a page that looks like this: turneringsportalen.no/beta/uttest.php
Thx for the answer. Worked like a charm! :)
I know, I noticed the updates on your live page, also noticicing you should use <td> where td stands for table data and <th> stands for table header. Doing this, would align your table. Also, it would be appreciated to up-vote as well.
Thx again. Has updatet it again wit <tr> correct td and correct th :) Thx!
You've updated already! To keep up to date, header information belongs into <thead>, body information to <tbody> and optionally put footer info into <tfoot>. Either way your table is mature enough for today's standard.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.