简体   繁体   English

从MySQL DB获取数据后,如何在php页面中创建可点击的网址?

[英]How do I make a clickable url in my php page after getting data from MySQL DB?

I'm trying to pull data from 1 column of a db and making it into a link where the URL is being pulled from another column. 我正在尝试从数据库的1列中提取数据,并将其放入从另一列中提取URL的链接中。 Unfortunately, I have not been able to do this. 不幸的是,我无法做到这一点。 I've attached the screenshot of the output here's the code I'm using: 我已经附上了输出的屏幕截图,这是我正在使用的代码:

$sql = "SELECT DISTINCT item_title, item_url, item_date, item_author FROM nbth";
 $result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
    $HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";
    echo $HTML;
}
} else {
echo "0 results";
}

Screenshot of the results: [![enter image description here][1]][1] 结果的屏幕截图:[![在此处输入图片描述] [1]] [1]

The titles do become links but the get looped again and again, the first title in the first line, then next the first title and second, then the first, second and third, so on and so forth. 标题确实成为链接,但是get一次又一次地循环,第一行中的第一个标题,然后是第一个标题和第二个,然后是第一个,第二个和第三个,依此类推。 So, how do I fix this looping and still display the titles as links. 因此,如何解决此循环问题并仍将标题显示为链接。

$HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";

Remove the period (string concat) operator before the equal sign: 删除等号前的句点(字符串concat)运算符:

$HTML = "<a href=".$row['item_url'].">".$row['item_title']."</a>";

It causes each iteration of the loop to concat the "<a href=".$row['item_url'].">".$row['item_title']."</a>"; 它导致循环的每次迭代都连接"<a href=".$row['item_url'].">".$row['item_title']."</a>"; part every time to the $HTML variable. 每次都移至$HTML变量。

Also, pls enclose the href attribute's value by quotation marks( " ). 另外,请用引号( " )括住href属性的值。

echo $html outside the loop 在循环外回显$ html

or 要么

while($row = $result->fetch_assoc()) {
$HTML ='';
echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
$HTML = "<a href=".$row['item_url'].">".$row['item_title']."</a>";
echo $HTML;
}
} else {
echo "0 results";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM