Using URL to highlight a table row

I have a table in a database and my question is how can I create it … that when I type …/array.php?id=1 into the url, it will highlight that row. If I put id=2 in, then it will highlight row 2

[php]<?php
function connect(){
// Make a MySQL Connection
$link = mysqli_connect(“localhost”, “root”, “”, “school”) or die(mysqli_error());
return $link;
}

if ($_GET)
{
if(isset($_GET[“id”])) $id = $_GET[“id”];
}

// select query
$query = “SELECT * FROM graden” ;

if (isset($id)) { $query.= " WHERE id = $id "; }

$result = mysqli_query(connect(),$query);

// table making
$table = “

”;
$table .= "




";

// keeps getting the next row until there are no more to get
while($row = mysqli_fetch_assoc( $result )) {
// Print out the contents of each row into a table
foreach ($row as $key => $value) {
if ($key == “Image”) {
$table .="

";
} elseif ($key == “temp”) {
$table .="";
} else {
$table .="";
}
}
$table .= "</tr>";

}

$table .="

ID Graden Celcius Aanduiding Image
$value$value
";

echo $table;
?>[/php]

give your td’s an id, IE

<td id=“2”… etc.

increment the counter in the loop and then check the id and highlight if appropriate.

Example:

CSS: .highlight { background-color:yellow; }

PHP:
[php]<?php
$counter = 0;
// keeps getting the next row until there are no more to get
while($row = mysqli_fetch_assoc( $result )) {
// Print out the contents of each row into a table
foreach ($row as $key => $value) {
// check if the id and counter match?
if($_GET[‘id’] == $counter) {
$highlight = ‘highlight’; // if they do, add highlighter string.
}
else {
$highlight = ‘’; // if they don’t, add empty string
}

    if ($key == "Image") {
        $table .= '<td id="' . $counter  . '" class="' . $highlight . '"><img src="' . $value . '" /></td>'; 
    } elseif ($key == "temp") {
        $table .= '<td id="' . $counter  . '" class="' . $highlight . '"><a href="array.php?id=' . $value . '">' . $value . '</a></td>'; 
    } else {
        $table .= '<td id="' . $counter  . '" class="' . $highlight . '">' . $value . '</td>';         
    }
    $counter++; // increment the counter.
}
$table .= "</tr>";

}
?>[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service