Come creare un collegamento ipertestuale in PHP
Il linguaggio di scripting PHP fornisce agli sviluppatori Web un metodo open source (gratuito) per fornire dati lato server ai visitatori del sito web. PHP consente agli sviluppatori di creare un'esperienza di navigazione Web dinamica per gli utenti in base alle loro esigenze e preferenze. Gli sviluppatori possono utilizzare PHP per codificare i collegamenti ipertestuali HTML su cui gli utenti possono fare clic per visitare altre pagine e generare collegamenti dinamici che vengono recuperati da un database o da un file.
Creazione di un collegamento statico o codificato
Passo 1
Crea una nuova istanza dell'istruzione print di PHP. Questo è il comando che indirizzerà l'HTML a visualizzare il collegamento e il testo di accompagnamento sulla schermata del browser degli utenti:
print "";?>
Step 2
Place an HTML anchor tag inside of the print statement. This is the same anchor tag that is used in traditional HTML coding. Include the target website as well as the link text that will accompany the link:
print "Click here to visit the destination page.";?>
Escape the quotation marks with the backslash character. In the previous example, the PHP would have broken because the quotation marks that surround the destination page address would be interpreted as a command to stop the print statement. The backslash character tells PHP to render the quotation mark as part of the anchor tag and continue the print statement. The backslash will not be printed and for practical purposes is invisible to the Web user:
print "Click here to visit the destination page.";?>
Creating Dynamic Hyperlinks with PHP and MySQL
Step 1
Connect to your MySQL database using the PHP mysql_connect and mysql_select_db functions:
mysql_connect("addressOfDatabase", "yourUsername", "yourPassword") or die(mysql_error());
mysql_select_db("yourDatabaseName") or die(mysql_error());
Step 2
Create a variable to retrieve the links from the MySQL database using the PHP mysql_query function. This example assigns the variable $data with a mysql_query function that will search the database tablenamed links and return all of the links:
$data = mysql_query("SELECT * FROM links") or die(mysql_error('Error, no links were found.'));
Extract the links using the mysql_fetch_array function and print them out for the user. The example creates a new array named $info. It assigns the array with the information from the $data variable that was created in the previous step. It then loops through the data using the PHP "while" command. For each piece of data, a new variable known as $link is created. The link from each MySQL link table's linkName field is assigned to the new $link variable. The $link variable is inserted into a PHP print statement and HTML anchor tag using the PHP concatenation rule:
while($info = mysql_fetch_array( $data ))
{
$link=$info['linkName'];
print "Click here to visit the destination page.";
}