我最近学会了通过php创建动态链接,但我今天的问题是我不知道如何在php块中执行此操作,例如我知道如何执行此操作:
<div><a href="blah.php?id=<?php echo $id; ?>">content</a></div>
但是我在php块中的head标签上面这样做,所以基本上我正在为动态内容创建动态链接,本质上我将在主页上动态呈现图像,这是产品的链接。这就是我所拥有的:
<?php
include_once "scripts/connect_to_mysql.php";
//Select query for latest items
$dynamic_newest = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 24");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$pid = $id;
$category = $row["category"];
$subcategory = $row["subcategory"];
$dynamic_newest .= "<a href="THIS IS WHAT I DONT KNOW HOW TO DO"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
}
} else {
$dynamic_newest = "<h1>There are no products to display yet.</h1>";
}
?>
这可能很容易,但我找不到它,也许我不是要求魔术谷歌正确的问题。提前谢谢!
答案 0 :(得分:3)
逃避问题,改变:
$dynamic_newest .= "<a href="THIS IS WHAT I DON'T KNOW HOW TO DO"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
要:
$dynamic_newest .= "<a href=\"blah.php?id=$id\"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
答案 1 :(得分:2)
不确定这是否是您正在寻找的但无论如何..
$dynamic_newest .= "<a href='blah.php?id=$pid'><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
答案 2 :(得分:2)
虽然您现在不使用此功能,但我相信您将来会发现它的用途。稍后,当您需要编写大块预格式化的非PHP文本并想要将php变量添加到其中时,您可以执行以下操作:
echo<<<YOUR_IDENTIFIER
<h1>Hi!</h1>
<form action='post'>
Welcome to my webpage =).
You can place code here as if it were not inside a PHP block,
but you can also use PHP variables. That means you can even
insert quotes like this --> "", though since this is still HTML,
it would be more accurate to use > and ". Your cleanest
bet and best-practice coding method is to {$encapsulate} php
variables in squiggley brackets. You can even
{$encapsulateArrays['likethis']}.
</form>
YOUR_IDENTIFIER;
echo "Back to regular PHP code.";
确保YOUR_IDENTIFIER;
之前没有空格或标签。
要回答你的原始问题(与我上面的blabber无关),请务必使用反斜杠\
正确转义任何引号,以免意外终止字符串文字。不要忘记{}
封装您的变量,即使您正在进行常规echo "My {$variable} here";
它也不会影响解析,但会让您更容易调试2个月,当您'重新开始你的下一个项目。