Kits
Kits

Reputation: 966

anchor tag is not working in php and html is rendered from other file

I am trying get data from a database and pasting it on a webpage in table form and in each table data there are names which have other details in database which I also have to show on other webpage if that name is clicked. I got stuck when anchor tag is not working.

NOT WORKING means that name in anchor tag is like normal text not link, though text turns blue with underline but its not acquiring its linking property.

Table in which I am getting data from database is working plus I am also trying to give anchor tag to table data which are simple names which would link to another file.

I am using 2 file one is .php and other is .html of same name.

Here are some of the LOC I am using and related to this. I have omitted other 3 columns.

file.php

<?php
...

$query="select name from table1 order by name";

$rs=mysql_query($query);

$table = '<table>';

while ($row = mysql_fetch_array($rs))
{
  $cname = $row["name"];

  $table .= '<tr>
             <td><a href="file3.php">'.$cname.'</a></td> /*<a></a> not working*/
             </tr>';
}
$table .= '</table>';
include_once 'file.html';
?>

file.html

<html>
<body>

<form>..</form> /*passes user input to PHP file1*/

<p><?php echo $table;?></p>
</body>
</html>

Html is rendered from html file.

file3.php is the page which I am trying to link through names.

I am using XAMPP 1.7.7 and PHP 5.3.8

Any useful suggestion?

Upvotes: 0

Views: 1777

Answers (3)

Swapnil T
Swapnil T

Reputation: 547

This is your code

$table .= '<tr>
           <td><a href="file3.php">'.$cname.'</a></td> /*<a></a> not working*/
           </tr>';

Just replace the single quote by double quotes

You just need to replace this code with following code: $table = "";

while ($row = mysql_fetch_array($rs))
{
  $cname = $row["name"];

  $table .= "<tr>
             <td><a href='file3.php'>$cname</a></td> 
             </tr>";
}
$table .= "</table>";

This code will print actual variable $cname value from your database Please try this.

Upvotes: -3

Mr. Alien
Mr. Alien

Reputation: 157334

Look at your query, you are using cname as an index for $row and your query fetches name

$query="select name from table1 order by name";
              --^--

$cname = $row["cname"];
               --^--

Is your error reporting turned off? You should get an error for this.. FOR SURE

Note: You should stop using mysql_() as it will be deprecated soon, start using mysqli_() or PDO instead...

Upvotes: 8

Amit Yaron
Amit Yaron

Reputation: 654

I prefer use 'mysql_fetch_assoc' to 'mysql_fetch_array. 2. Check if the table has values. 3. change '$row["cname"]' to '$row["name"]'

Upvotes: 0

Related Questions