URL and link text from database

一世执手 提交于 2021-02-19 08:42:46

问题


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.

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.

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

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

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.


回答1:


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



回答2:


What you need is to:

  1. get your result array from the database. Use something like

    $query = "SELECT * FROM urls"; $result = mysql_query($query);

  2. For every row in your results table, show the corresponding url. Note that calling mysql_fetch_array on a result resource, returns the first row of the results table when called for the first time, the second on second time etc. The function returns false when there are no more rows to return.

(See more on that in the mysql_fetch_array() documentation)

While( $row = mysql_fetch_array($result) ){
    echo '<a href='.$row['url'].'>'.$row['text'].'</a>';
    }



回答3:


Here is a sample you can start with:

$con = mysql_connect("host","user","password");
if (!$con) {
   die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT the_url, the_text FROM my_table");

while($row = mysql_fetch_array($result))
{
   echo '<a href="' . $row['the_url'] . '">"' . $row['the_text'] . '</a> <br />';
}

mysql_close($con);



回答4:


First, you want to select only the relevant lines in your database for the page that the user is currently viewing. For example, if the user is viewing page 2, with entries 15-30 present, we only want to pull those entries from the database. This is an efficiency concern.

The code you're after is something like this:

$link = mysql_connect(/*connection parameters go here*/);
if ($link === false)
  die;

mysql_select_db('my_database');

$result = mysql_query("SELECT text, url FROM my_table LIMIT 15,30");

print "<ul>\n";
while ($row = mysql_fetch_assoc($result)) {
  print "<li><a href=\"{$row['url']}\">{$row['text']}</a></li>\n";
}
print "</ul>\n";

mysql_close();

The first three lines establish a connection to the database, and exit the script if an error occurs.

The next line selects the appropriate database on the server.

The next block runs the appropriate query for the second page. After the query is run, a 'result' is stored in $result. This result is comprised of a number of rows, and mysql_fetch_assoc($result) obtains those lines one at a time. It then formats these lines into the appropriate link format and outputs them. The entire set of links is wrapped in a dot-point list.

Finally, mysql_close() closes the connection to the database.




回答5:


I'm assuming since you just started you're probably doing all this procedurally.

First you want to query the database and get the info you need.

<?php
$result = mysqli_query($link, "SELECT url, text FROM table_name");
while ($row = mysqli_fetch_array($result)) $hrefs[] = $row;

foreach ($hrefs as $href) {
    echo "<a href=\"".$href['url']."\">".$href['text']."</a>";
}
?>

Please note I've done no error handling here.



来源:https://stackoverflow.com/questions/9473594/url-and-link-text-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!