PHP-LINKS is a data-driven application that allows collections of links to be viewed by category or located using a powerful search facility. It requires PHP and MySQL to work. The application can be seen working at www.v-consult.co.uk/links.php
By : martin
PHP-LINKS
The following scripts need to be saved under the respective filenames and placed in a virtual directory on a php/mysql enabled web server:
The scripts can be seen working at www.v-consult.co.uk/links.php
=========
LINKS.PHP
=========
<?php
# Connect to the database
include("connect.inc");
# Select a Category
global $catflag;
if ( isset($catflag) && $catflag == "yes" )
{
$sql="select catID from cats where category='$category'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
$catID=$a_row[catID];
}
}
elseif ( isset($searchflag) && $searchflag == "yes" )
{
}
else
{
$catID=2;
}
$catflag = "";
print "<html>";
print "<head>";
?>
<SCRIPT LANGUAGE="JavaScript">
<!--
function load(form) {
var url = form.list.options[form.list.selectedIndex].value;
if (url != "") location.href = url;
return false;
}
// -->
</SCRIPT>
<?php
print "<title>Links</title>";
print "</head>";
print "<body link='#000099' vlink='#000099' alink='#000099' MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0 bgcolor='#0099ff'>";
include("banner.inc");
?>
<table border=0 cellpadding="0" cellspacing="0" width=780 bgcolor='#0099ff'>
<tr>
<td valign=top width=120>
<?php
include("sidebar.inc");
?>
</td>
<td valign=top width=660>
<table border=0 cellpadding="0" cellspacing="0" width=100% bgcolor='#000099'>
<tr bgcolor='#0099ff'>
<?php
print "<td bgcolor='' height=25><b><font face=arial size='4' color='#ffffff'> Demonstration of PHP-LINKS: $category</font></b></td>";
?>
</tr>
<tr>
<td>
<table border=0 cellpadding="5" cellspacing="5" width=100% bgcolor='#0099ff'>
<tr>
<td bgcolor='#66FFCC'>
<?php
# Search form
print "<form action='links.php'>";
print "<input type='hidden' name='searchflag' value='yes'>";
print "<input type='text' name = 'search' value='$search'>";
print "<input type='submit' value='Search'>";
print "</form>";
# form to select category
# <FORM NAME="menu">
# <SELECT NAME="list" onChange="load(this.form)">
# <OPTION VALUE="b" SELECTED>a
# <OPTION VALUE="g">c
# </SELECT>
# </FORM>
print "<form name='menu'>";
print "<input type='hidden' name='catflag' value='yes'>";
print "<select name = 'list' onChange='load(this.form)'>";
$sql="select catID, category from cats order by category";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
$sel="";
if ( $a_row[catID] == $catID )
{
$sel=" selected";
}
print "<option value='links.php?category=$a_row[category]&catflag=yes' $sel>$a_row[category]";
}
print "</select>";
print "</form>";
print "<table border=1 width=100%>";
print "<tr>";
print "<td width=45%><font face=arial size='2'><b>Site Name</b></font></td><td width=55%><font face=arial size='2'><b>Description</b></font></td>";
print "</tr>";
if ( isset($searchflag) && $searchflag == "yes" )
{
$sql="select distinct url, name, description from urls, link, cats where urls.urlID=link.urlID and cats.catID=link.catID and (url like '%$search%' or name like '%$search%' or description like '%$search%' or category like '%$search%') order by name";
$searchflag = "";
}
else
{
$sql="select url, name, description from urls, cats, link where urls.urlID=link.urlID and cats.catID=link.catID and link.catID='$catID' order by name";
}
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<tr>";
print "<td valign=top><font face=arial size='2'><a href='http://$a_row[url]'>$a_row[name]</a></font></td><td valign=top><font face=arial size='2'>$a_row[description]</font></td>";
print "</tr>";
}
?>
</table>
<br>
<a href='amend.php'>Amend</a></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
include("footer.inc");
?>
</body>
</html>
========
LIST.PHP
========
<?php
# Connect to the database
include("connect.inc");
# HTML Header
print "<html>\n";
print "<head>\n";
print "<title>Full Database Listing</title>\n";
print "</head>\n";
print "<body link='#000099' vlink='#000099' alink='#000099' MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0>\n";
print "<center><a href='links.php'>Return to Links Page</a>  <a href='amend.php'>URL Listing</a></center>";
print "<h2>Full Database Listing</h2>\n";
$sql = "select url, name, category from urls, cats, link where cats.catID = link.catID and urls.urlID = link.urlID order by url";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
print "<table border=1>\n";
print "<tr>";
print "<td><b>URL</b></td><td><b>Name</b></td><td><b>Category</b></td>";
print "</tr>";
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<tr>";
print "<td>$a_row[url]</td><td>$a_row[name]</td><td>$a_row[category]</td>";
print "</tr>";
}
print "</table>\n";
?>
</body>
</html>
==========
FOOTER.INC
==========
<table border="0" cellpadding="0" cellspacing="0" width="780" bgcolor="#0099ff">
<tr>
<td height="25">
</td>
</td>
</tr>
</table>
===========
CONNECT.INC
===========
<?php
global $link;
$link = mysql_connect( "localhost", "username", "password" );
if ( ! $link ) die( "Couldn't connect to MySQL" );
mysql_select_db( "linkdemo", $link ) or die ( "Couldn't open linkdemo: ".mysql_error() );
?>
========
CATS.PHP
========
<?php
# Connect to the database
include("connect.inc");
# Edit a Category
global $editcatflag;
if ( isset($editcatflag) && $editcatflag == "yes" )
{
$sql = "update cats set category='$category' where catID='$catID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
$editcatflag = "";
# Add a Category
$warning="";
global $addcatflag;
if ( isset($addcatflag) && $addcatflag == "yes" )
{
$sql = "select catID from cats where category='$category'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
$warning="yes";
}
if ( $warning == "yes" )
{
print "This category already exists";
}
else
{
$sql = "insert into cats(category) values('$category')";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
}
$addcatflag = "";
# HTML Header
print "<html>\n";
print "<head>\n";
print "<title>Edit Categories</title>\n";
print "</head>\n";
print "<body link='#000099' vlink='#000099' alink='#000099' MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0>\n";
print "<center><a href='amend.php'>Return to URL Listing</a></center>";
# Form to add a Category
print "<h3>Add a Category</h3>\n";
print "<form action='$PHP_SELF'>";
print "<input type='hidden' name='addcatflag' value='yes'>";
print "<input type='text' size=40 name='category'>";
print "<input type='submit' value='Add Category'>";
print "</form>";
# Form to edit a Category
print "<h3>Edit Categories</h3>\n";
print "<table>";
$sql = "select * from cats order by category";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query 1");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<tr>";
print "<td>";
print "<form action='$PHP_SELF'>";
print "<input type='hidden' name='editcatflag' value='yes'>";
print "<input type='hidden' name='catID' value='$a_row[catID]'>";
print "<input type='text' size=40 name='category' value='$a_row[category]'>";
print "</td>";
print "<td>";
print "<input type='submit' value='Edit'>";
print "</td>";
print "</tr></form>";
}
print "</table>";
print "<HR>";
?>
</body>
</html>
==========
BANNER.INC
==========
<table border="0" cellpadding="0" cellspacing="0" width="780" bgcolor="#0099ff">
<tr>
<td valign=top rowspan="2" height="40"><a href="../index.html">
<img border="0" src="images/logo.gif" width="250" height="40"></a></td>
<td height="20">
</td>
<td align=right nowrap></td>
</tr>
<tr>
<td colspan=2 height="20" align=right><font face='verdana' size=2><b><a href='amend.php'>Add or Amend URL's</a> <a href='http://www.v-consult.co.uk'>Return</a> </font></b></td>
</tr>
</table>
===========
SIDEBAR.INC
===========
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#0099ff" width=120>
<tr>
<td bgcolor='' height=25></td>
</tr>
<tr>
<td height=5></td>
</tr>
<tr>
<td width=120>
</td>
</tr>
</table>
=========
AMEND.PHP
=========
<?php
# Connect to the database
include("connect.inc");
# Add a URL
global $addurlflag;
$warning="";
if ( isset($addurlflag) && $addurlflag == "yes" )
{
$sql = "select url from urls where url='$url'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "This URL is has been entered previously. ";
$warning="yes";
}
if ( isset($warning) && $warning == "yes" )
{
print " Please use the 'Edit' button to make changes.";
}
else
{
$sql = "insert into urls(url) values('$url')";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
}
$addflag = "";
# Edit a URL
global $editurlflag;
if ( isset($editurlflag) && $editurlflag == "yes" )
{
$sql = "update urls set url='$url' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
$sql = "update urls set name='$urlname' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
$sql = "update urls set description='$urldescription' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
$editurlflag = "";
# Delete a URL
global $deleteurlflag;
if ( isset($deleteurlflag) && $deleteurlflag == "yes" )
{
$sql = "delete from urls where url='$url'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
$sql = "delete from link where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
$deleteurlflag = "";
# HTML Header
print "<html>\n";
print "<head>\n";
print "<title>Update Links</title>\n";
print "</head>\n";
print "<body link='#000099' vlink='#000099' alink='#000099' MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0>\n";
print "<center><a href='links.php'>Return to Links Page</a>  <a href='cats.php'>Categories Table</a>  <a href='list.php'>Full Listing</a></center>";
# Form to add a URL
print "<h3>Add a new URL</h3>\n";
print "<form action='$PHP_SELF'>\n";
print "<input type='hidden' name='addurlflag' value='yes'>\n";
print "<input type='text' name='url'>\n";
print "<input type='submit' value='Add URL'>\n";
print "</form>\n";
# Form to edit or delete a URL
print "<h3>Delete or Edit a URL</h3>\n";
print "<h4>Select Edit to modify the URL, name or description and assign categories</h4>\n";
$sql = "select * from urls order by url";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
print "<table>\n";
print "<tr>";
print "<td><b>URL</b></td>";
print "<td> </td><td> </td><td><b>Warning - at least one category must be set</b></td>";
print "</tr>";
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<tr>";
print "<form action='$PHP_SELF'><td>";
print $a_row[url];
print "</td>";
print "<form action='$PHP_SELF'>";
print "<td><input type='hidden' name='deleteurlflag' value='yes'>";
print "<input type='hidden' name='urlID' value='$a_row[urlID]'><input type='hidden' name='url' value='$a_row[url]'>";
print "<input type='submit' value='Del'>";
print "</td></form>";
print "<form action='addcats.php'>";
print "<td><input type='hidden' name='urlID' value='$a_row[urlID]'>";
print "<input type='submit' value='Edit'>";
print "</td></form>";
print "<td>";
$query="select urlID from link where urlID='$a_row[urlID]'";
$result = mysql_query($query, $link) or die("Couldn't execute SQL query");
$num_rows=mysql_num_rows($result);
if ( $num_rows == 0 )
{
print "Select Categories";
}
else
{
print "$num_rows Set";
}
print "</td>";
print "</tr>";
}
print "</table>\n";
?>
</body>
</html>
===========
ADDCATS.PHP
===========
<?php
# Connect to the database
include("connect.inc");
# Edit a URL
global $editurlflag;
if ( isset($editurlflag) && $editurlflag == "yes" )
{
$sql = "update urls set url='$url' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
$sql = "update urls set name='$urlname' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
$sql = "update urls set description='$urldescription' where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
$editurlflag = "";
# Add a Category
$warning="";
global $addcatflag;
if ( isset($addcatflag) && $addcatflag == "yes" )
{
$sql = "select catID from cats where category='$category'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
$catID=$a_row[catID];
$sql = "select urlID from link where urlID='$urlID' and catID='$catID'";
$result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
while ( $row = mysql_fetch_array( $result ) )
{
$warning="yes";
}
}
if ( $warning == "yes" )
{
print "This category already exists";
}
else
{
$sql = "insert into link(urlID, catID) values($urlID, $catID)";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
}
$addcatflag = "";
# Delete a Category
global $delcatflag;
if ( isset($delcatflag) && $delcatflag == "yes" )
{
$sql = "delete from link where catID='$catID' and urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
}
$delcatflag = "";
# HTML Header
print "<html>\n";
print "<head>\n";
print "<title>Update Categories</title>\n";
print "</head>\n";
print "<body link='#000099' vlink='#000099' alink='#000099' MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0>\n";
print "<center><a href='amend.php'>Return to URL Listing</a></center>";
# Form to edit a URL
print "<h3>Edit URL</h3>\n";
$sql = "select * from urls where urlID='$urlID'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query 1");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
$urlID=$a_row[urlID];
print "<table>";
print "<tr>";
print "<td><form action='$PHP_SELF'>URL: </td><td><input type='text' size=50 name='url' value='$a_row[url]'</td>";
print "</tr>";
print "<tr>";
print "<input type='hidden' name='editurlflag' value='yes'>";
print "<input type='hidden' name='urlID' value='$a_row[urlID]'>";
print "<td>Name: </td><td><input type='text' size=40 name='urlname' value='$a_row[name]'></td>";
print "</tr>";
print "<tr>";
print "<td>Description: </td><td><input type='text' size=40 name='urldescription' value='$a_row[description]'></td>";
print "</tr>";
print "<tr>";
print "<td> </td><td> </td>";
print "</tr>";
print "<tr>";
print "<td colspan=2><input type='submit' value='Edit this URL'></td></form>";
print "</tr>";
print "</table>\n";
}
# Form to add a Category
print "<h3>Add a Category - At least one Category must be assigned</h3>\n";
print "<form action='$PHP_SELF'>";
print "<input type='hidden' name='addcatflag' value='yes'>";
print "<input type='hidden' name='url' value='$url'>";
print "<input type='hidden' name='urlID' value='$urlID'>";
print "<select name='category'>";
$sql = "select * from cats order by category";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query 1");
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<option>$a_row[category]</option>";
}
print "</select>";
print "<input type='submit' value='Add Category'>";
print "</form>";
# Categories for this URL with Delete Option
print "<h3>Display Categories for this URL</h3>\n";
$sql = "select category, link.catID as A from link, cats where link.catID=cats.catID and urlID='$urlID order by category'";
$sql_result = mysql_query($sql, $link) or die("Couldn't execute SQL query");
print "<table>";
while ( $a_row = mysql_fetch_array( $sql_result ) )
{
print "<tr>";
print "<td><form action='$PHP_SELF'>$a_row[category]</td>";
print "<input type='hidden' name='urlID' value='$urlID'>";
print "<input type='hidden' name='url' value='$url'>";
print "<input type='hidden' name='delcatflag' value='yes'>";
print "<input type='hidden' name='catID' value='$a_row[A]'>";
print "<td><input type='submit' value='Delete'></td>";
print "</form>";
print "</tr>";
}
print "</table>\n";
print "<br>";
?>
</body>
</html>
======================================
This is a dump from the MySQL Database
======================================
# MySQL dump 8.8
#
# Host: localhost Database: linkdemo
#--------------------------------------------------------
# Server version 3.23.22-beta
#
# Table structure for table 'cats'
#
CREATE TABLE cats (
catID int(11) NOT NULL auto_increment,
category varchar(50) DEFAULT '' NOT NULL,
PRIMARY KEY (catID)
);
#
# Table structure for table 'link'
#
CREATE TABLE link (
urlID int(11) DEFAULT '0' NOT NULL,
catID int(11) DEFAULT '0' NOT NULL
);
#
# Table structure for table 'urls'
#
CREATE TABLE urls (
urlID int(11) NOT NULL auto_increment,
url varchar(50) DEFAULT '' NOT NULL,
name varchar(50) DEFAULT '' NOT NULL,
description text,
PRIMARY KEY (urlID)
);
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Link Farm Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |