0

Can someone please show me where i am going wrong with this, i am echoing out some mysql results in a query using a table format, and i am trying to make the row of results clickable so a user can click on it an link to another page.

i dont understand why its not working

<?php include 'config.php';
         $data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, insurance_date, CURDATE()) AS expire_date FROM supplier_stats ORDER BY expire_date DESC") 
         or die(mysql_error()); 

         echo "<table class=\"table\" style=\"width:995px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
         font-size:11px;\" >

    <tr>
        <td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Owner:</td><td style=\"width:200px;\">Note:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";


         while($row = mysql_fetch_array( $data )) { 
            $days = $row['expire_date'];
            $when = $days*-1; 
            $str = $row['expire_date'];
            $str2 = substr($str, 1); // "quick brown fox jumps over the lazy dog."
           if ($when <= 31){
           echo "<a href=\"test.php\"><tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
           echo "<td style=\"width:100px;\"><p>".$row['owner'] . "</p></td>";


           if ($days > 0) {
                echo "<td style=\"width:200px;\"><p><font color=\"red\">Insurance expired!</font>";  
                echo "<td>";
                echo date('d/m/Y',strtotime($row['insurance_date']));
      echo"</td>";
                if ($when >= 7){ echo "<font color=\"green\">{$row['expire_date']}</font> day(s)!</p></td>";  }
            } else {
              $when = $days*-1;           

              echo "<td style=\"width:200px;\"><p>Insurance expires";

              if ($when > 1){
                  if ($when >= 20){ 
                  echo " in <font color=\"green\">{$str2}</font> days</font></td>"; }
                  elseif ($when >= 8){ 
                  echo " in <font color=\"orange\">{$str2}</font> days</font></td>"; }
                  elseif ($when <= 7){ 
                  echo " in <font color=\"red\">{$str2}</font> days</font></td>"; }

              } elseif ($when >= 1){ 
                echo "<font color=\"red\"> tomorrow!</font></td>";
              }
            elseif ($when >= 0){ 

                echo "<font color=\"red\"> today!</font></p></td>";
            }

            echo "<td>";
              echo date('d/m/Y',strtotime($row['insurance_date']));


      echo"</td>";


            }

            if ($when >= 20){
                echo "<td style=\"width:150px;\"><div class=\"green_light\"></div></td>";

            }

            elseif ($when >= 8){
                echo "<td style=\"width:150px;\"><div class=\"amber_light\"></div></td>";
              }

              if ($when <= 7){
                echo "<td style=\"width:150px;\"><div class=\"red_light\"></div></td>";
              }


            echo "<tr></a>";
          }
         }

          echo "</table>"; //Close the table in HTML


        ?>
9
  • your tr tags are wrong, put a tag in side tr not outside Commented May 8, 2014 at 8:56
  • You can't put an <a> tag as a child of <table>. Commented May 8, 2014 at 8:56
  • The <a> tag has to be inside <td>. The hierarchy has to be <table> -> <tr> -> <td> -> stuff. Commented May 8, 2014 at 8:57
  • ive done this but still not linking Commented May 8, 2014 at 8:58
  • If you want to make the whole row clickable, put an onclick attribute in the <tr> Commented May 8, 2014 at 8:58

3 Answers 3

1

You have two ways to do this:

Using javascript:

<tr onclick="document.location = 'links.html';">

Using anchors:

<tr><td><a href="">text</a></td><td><a href="">text</a></td></tr>

I made the second work using:

table tr td a {
    display:block;
    height:100%;
    width:100%;
}

To get rid of the dead space between columns:

table tr td {
    padding-left: 0;
    padding-right: 0;
}

This will work really work

I should say thanks Voyager

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

2 Comments

the problem with this though is that i am going to want each row to link to a seperate url so row one might be test.php?id=1 and the next row might be test.php?id=3 so the link needs to be part of the mysql query it can not be a standard html link
I will give you an answer now, but You said want to have a table link not a row link
0

it is not allowed to use the p tag within an a tag

add this css-code to your css file

a {
  display: block;
}

Comments

0

I think you cannot open

<a> before <tr>

because <table> <tr> <td> must be sequential

you can use other syntax like :

<td colspan="8"> <a> bla bla bla </a> </td>

Comments

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.