Link Farm Code

  Home arrow Link Farm Code arrow PHP-LINKS
LINK FARM CODE

PHP-LINKS
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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'>&nbsp;&nbsp;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>&nbsp&nbsp<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>&nbsp;&nbsp;<a href='http://www.v-consult.co.uk'>Return</a>&nbsp;&nbsp;</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>&nbsp&nbsp<a href='cats.php'>Categories Table</a>&nbsp&nbsp<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>&nbsp</td><td>&nbsp</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>&nbsp</td><td>&nbsp</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

    blog comments powered by Disqus

    LINK FARM CODE ARTICLES

    - FreshSoftware Banner Image Rotater
    - LinkMachine
    - Pagination
    - Ask and Receive version 2.0
    - MyLinks Version 1.1
    - Flexphplink Pro
    - Flexphplink
    - Please remove
    - Simple Outgoing Links/Url Hit Counter
    - PHP-LINKS

    Developer Shed Affiliates

     



    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap