开发者

how to put links in php

<?php>   
 while($row = mysql_fetch_array($result2))
      {
       echo "<tr>";
       echo "<td>" . $row['IDNO'] . "</td>";
    echo "<td>" . $row['ADDRESS'] . "</td>";
      echo "<td>" . $row['LASTNAME'] . "</td>";
        echo "<td>" . $row['FIRSTNAME'] . "</td>";
          echo "<td>" . <a href='update.php'>view</a> . "</td>"; 

      echo "</tr>";
      }
    echo "</table>";
    }
?>

That's my code, I really don't know the correct format of putting links inside the php tags:

 e开发者_Python百科cho "<td>" . <a href='update.php'>view</a> . "</td>"; 

Please help


When using PHP to make webpages, the strings you echo out are pretty much always in the context of a HTML document. That is, if you want to output a HTML link, just echo it:

echo "<td><a href='update.php'>view</a></td>";


PHP is a templating language, there's no need to be throwing HTML around in strings.

<?php while ($row= mysql_fetch_array($result2)) { ?>
    <tr>
        <td><?php echo htmlspecialchars($row['IDNO']); ?></td>
        <td><?php echo htmlspecialchars($row['ADDRESS']); ?></td>
        <td><?php echo htmlspecialchars($row['LASTNAME']); ?></td>
        <td><?php echo htmlspecialchars($row['FIRSTNAME']); ?></td>
        <td>
            <a href="update.php?idno=<?php echo urlencode($row['IDNO']); ?>">view</a>
        </td>
    </tr>
<?php } ?>

Note the use of HTML-escaping. Without this, < and & characters in your strings will be copied into the raw HTML, causing potential cross-site scripting security problems. Whether using PHP templating or sticking strings together, always HTML-escape plain text output.


If you are not pulling any dynamic values into the HTML for the cell with the link, you do not have to do any string concatenation here. Simply print out the HTML as a string:

echo "<td><a href='update.php'>view</a></td>";

However, it does not seem very useful to have the same link in every row of the table. Maybe you need to add a querystring parameter to the linked URL? To do this you will need to do string concatenation. The example below should get you started (notice the position of the quotes):

echo "<td><a href='update.php?id=" . $row['ID'] . "'>view</a></td>";


your "link" is the same HTML tag as <TD>. So, treat it as well


echo "<td><a href='update.php'>view</a></td>";

or if you want to pass parameter try

echo "<td><a href='update.php/value=".$val."'>view</a></td>";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜