Using URL to highlight a table row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chelle22
    New Member
    • Jun 2013
    • 1

    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


    Code:
    <?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 border='1'>";
    $table .= "<tr>
                 <th> ID </th>
                 <th>Graden Celcius</th> 
                 <th>Aanduiding</th>
                 <th>Image</th> 
            </tr>";
    
    // 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 .="<td><img src='$value' /></td>"; 
            } elseif ($key == "temp") {
                $table .="<td><a href='array.php?id=$value'>$value</a></td>"; 
            } else {
                $table .="<td>$value</td>";         
            }
    
        }
        $table .= "</tr>";
    }
    
    $table .="</table>";
    
    echo $table;
    ?>
    Last edited by Rabbit; Jun 20 '13, 03:16 PM. Reason: Please use code tags when posting code.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    First of all it looks like from your code you are only returning one record and not the entire content of the data base table, because of the "where" clause in your sql query

    If that is the case why would you need to highlight it?

    But if you were returning multiple records and want to hightlight only the requested line.
    1) eliminate the where
    2) make a test for the id when scrolling out the lines for the table you want to display.
    Code:
    if (id == paramId)
    {
      <td style="background-color: blue;">
    }else{
      <td>
    }

    Comment

    Working...