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! |
This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. 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!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |