来自数据库的URL和链接文本 [英] URL and link text from database

查看:30
本文介绍了来自数据库的URL和链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前仍在学习PHP,因此有些事情仍然困扰着我.我一直在慢慢学习,阅读了一些有帮助的教程,但我无法弄清楚这一点.

I am currently still learning PHP so some things I still struggle with. I have been taking it slowly and reading tutorials which has helped but I can't figure this one out.

我有一个数据库表(在mysql中),假设有100个网址.有一个名为"url"的列,第二个列为"text".我已经有了可以使用的分页代码,因此也将使用它.

I have a database table (in mysql) with let's say, 100 urls. There is a column called 'url' and a second column 'text'. I already have the pagination code which works, so will also be using that.

我想做的是回显URL(这些URL都位于网站根目录中的Blog文件夹中),但是使用文本作为链接.

What I want to do is echo out the URLs (which are all in folder called blog in the root of my site), but use the text as the link.

例如,表中的前三行可能是:

So for example the first three rows in my table might be:

URL
001.php
002.php
003.php

url
001.php
002.php
003.php

文本
随机文字
一些随机的文字
更多文字

text
random text
some random text
more text

当回显链接时,将显示列文本中的文本,例如:

when echoed out the links show the text from the column text like:

随机文本
一些随机的文字
更多文字

random text
some random text
more text

,并且在点击后会打开相关的网址

and will open to the relevant url when clicked

我猜想它将需要某种循环来收集所有URL,并省去我手动添加链接文本的麻烦,然后我的分页代码会将它们拆分.

I'm guessing it will need some kind of loop to collect all the URLs and save me adding the link text in manually, and then my pagination code will split them up.

这是我第一次在这里问一个问题,所以如果不清楚,或者您需要更多信息,请告诉我.

This is my first time asking a question on here, so if it wasn't clear enough or you need more info, let me know.

我在互联网上进行了多次搜索,但似乎找不到教程.

I have done multiple searches on the internet but can't seem to find a tutorial.

推荐答案

假定您使用用户名"root"和密码"root"连接到本地mysql服务器,并将您的网址存储在名为 url_table 在名为 url_database 的数据库中,您可以执行以下操作:

Assuming you connect to a local mysql server with username "root" and password "root", and have your url's stored in a table named url_table in a database named url_database you could do something like:

$connection = mysql_connect("127.0.0.1","root","root"); // Connect to the mysql server
mysql_select_db("url_database"); // Open the desired database

$query = "SELECT url,text FROM url_table"; // Query to select the fields in each row
$result = mysql_query($query); // Run the query and store the result in $result

while($row = mysql_fetch_assoc($result)) // While there are still rows, create an array of each
{
    echo "<a href='".$row['url']."'>".$row['text']."</a>"; // Write an anchor with the url as href, and text as value/content
}

mysql_close($connection); // close the previously opened connection to the database

这篇关于来自数据库的URL和链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆