SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Php Linking

  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php Linking

    Hi All,
    I am new to PHP and as my first script I am making a very simple CMS system. So far it has all being going great with no real problems but I am now trying to build a links script where people can add there own link to my database.

    My question is this:

    How can I display a title of a URL from my database but have it so that it is a clickable link to the other site?

    I am using dreamweaver MX 20004 if this helps?

    I am also teaching myself so simple explanations would be great lol.

    Thanks in advance,
    Neal

  2. #2
    From downunder but sure 2 rise Hazardous's Avatar
    Join Date
    Aug 2003
    Location
    New Zealand
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<a href="'.$url.'">'.$url.'</a>'
    is that what you mean?
    Hazardous
    My Blog

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers. I guess I should explain a little better.

    Ok, on db I have 3 tables: link_ID , link_URL (this holds the URL of the other site) & link_Title.

    How could I use the above code in conjunction with my db?

    Sorry for being vauge on this but I have only been learning for a 2 days!

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    Helsingborg, Sweden
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say you have three tables do you mean a table with three columns? These entries should all be in one table...

    PHP Code:
    $result mysql_query("SELECT link_URL, link_Title FROM Table WHERE link_ID = '".$value."'") or die(mysql_error());
    $link mysql_fetch_array($result);
    echo 
    "<a href=\"".$link['link_URL']."\">".$link['link_Title']."</a>\n"
    If you want to select all links the skip the where clause and iterate through the links with:
    PHP Code:
    while($link mysql_fetch_array($result)){
      echo 
    "<a href=\"".$link['link_URL']."\">".$link['link_Title']."</a>\n";


  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, I have a table that has three colums in ( link_ID , link_URL & link_Title ) I also have a recordset in dreamweaver cnamed rstLinks with these colums in.

    I have been playing with the above code that Borje so kindly supplied but I think I am going wrong somewhere as I cannot seam to get it to work.

    Neal

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    Helsingborg, Sweden
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Show us your code. I didn't run it to check for errors so there might be a parse error or something. By the way, the word Table in "FROM Table" should be substituded with the name of the table. You didn't write the name of it...

    EDIT: Is the database a mysql databse? I just assumed...

  7. #7
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The name of the table is called "links". The code below is the entire page copied from dreamweaver, it also includes the code that you supplied with the amended table name:

    <?php require_once('../Connections/ghost2ghostsconn.php'); ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO links (link_Title, link_URL, link_email) VALUES (%s, %s, %s)",
    GetSQLValueString($_POST['link_Title'], "text"),
    GetSQLValueString($_POST['link_URL'], "text"),
    GetSQLValueString($_POST['link_email'], "text"));

    mysql_select_db($database_ghost2ghostsconn, $ghost2ghostsconn);
    $Result1 = mysql_query($insertSQL, $ghost2ghostsconn) or die(mysql_error());
    }

    mysql_select_db($database_ghost2ghostsconn, $ghost2ghostsconn);
    $query_rstLinks = "SELECT * FROM links";
    $rstLinks = mysql_query($query_rstLinks, $ghost2ghostsconn) or die(mysql_error());
    $row_rstLinks = mysql_fetch_assoc($rstLinks);
    $totalRows_rstLinks = mysql_num_rows($rstLinks);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>

    <body>

    <? $result = mysql_query("SELECT link_URL, link_Title FROM links WHERE link_ID = '".$value."'") or die(mysql_error());
    $link = mysql_fetch_array($result);
    echo "<a href=\"".$link['link_URL']."\">".$link['link_Title']."</a>\n"; />
    <p></p>
    <form name="form1" method="POST" action="<?php echo $editFormAction; ">?>
    <table width="500">
    <tr>
    <td width="72">Link Title </td>
    <td width="416"><input name="link_Title" type="text" id="link_Title"></td>
    </tr>
    <tr>
    <td>URL</td>
    <td><label>
    <input name="link_URL" type="text" id="link_URL" value="http://www.">
    </label></td>
    </tr>
    <tr>
    <td>Email</td>
    <td><label>
    <input name="link_email" type="text" id="link_email">
    </label></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form1">
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    <?php
    mysql_free_result($rstLinks);
    ?>

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    Helsingborg, Sweden
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result = mysql_query("SELECT link_URL, link_Title FROM links WHERE link_ID = '".$value."'")

    The variable $value here is something I just put there if there is just one link you would to grab from the database. Set $value to a number that you know exists in the database as a link_ID. Or grab all rows by omitting the WHERE claus. Read http://dev.mysql.com/doc/refman/5.0/en/select.html to understand how a select claus works.

    try:
    PHP Code:
    <?php
    mysql_select_db
    ($database_ghost2ghostsconn$ghost2ghostsconn);
    $query "SELECT link_URL, link_Title FROM links";
    $result mysql_query($query$ghost2ghostsconn) or die(mysql_error());

    while(
    $link mysql_fetch_array($result)){
      echo 
    "<a href=\"".$link['link_URL']."\">".$link['link_Title']."</a>\n";

    ?>
    EDIT: embedded with php tags, you are using them wrong now with a <? as a start tag and no end tag. <? Can be used if short tags are allowed but I'd rather you use <?php as start tag.

  9. #9
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! Thats got it. Thanks very much for your help, I would be pulling my hair out without you lol.

    Neal

  10. #10
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Borje
    EDIT: embedded with php tags, you are using them wrong now with a <? as a start tag and no end tag. <? Can be used if short tags are allowed but I'd rather you use <?php as start tag.
    In case your wondering, its something to do with compatability issues when your scripts are running on other servers with other settings (or something like that)
    Just makes sure that your script will run on all servers!
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  11. #11
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers, I will remember that for the future. Just one other question, how can I make it so that it open the linked site in a new window? I have tried adding target="_blank" in the string but no luck.

    Neal

  12. #12
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forget my last comment, I figured it out. Thanks very much for all of your help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •