Jump to content

[SOLVED] Unique Link Target Problem


rscott7706

Recommended Posts

Hey all, I have a unique problem.

 

Here is a snippet from my code:

 

    echo "<a target=\"_blank\" href='$row[link]'>   Go there</a>";

 

Now, I need to have some links to target an Iframe named "container", but others to target _blank.

 

In other words, I have some links that are targeted to my site only, but some to friends of the Chamber that must open in a new window.

 

I know I could build in to my database, a field that would allow target=container or target=_blank, but how do i get my PHP to recognize this, and plop it in after the opening tag <a and before the href= part?

 

Egads....  Thanks in advance...

 

8)

 

Ron Scott

Link to comment
Share on other sites

Well, how are you recognizing which links are targeting container and which are targeting blank? Do you already have something in your DB that identifies links as internal or external?

 

Some more code and an example link would be helpful.

Link to comment
Share on other sites

widox - Thanks for your quick response.

 

The link comes from the database named "link".  In the coding I just made it automatically go to _blank, but soon realized I want the base links (my site links) to go to an Iframe called "container", but external links to go to _blank.

 

I could have the link in link field in the database determine the target, but inexperienced Chamber office works will be putting the link info in the database, so I wanted to keep it simple for them.

 

Here is more code, leaving out log on info:

 

mysql_select_db("lakesie7_articles");

$query = "SELECT * FROM coming_events ORDER by sort";

$result = mysql_query($query) or die(mysql_error());

echo "<font face=\"arial\">";

while($row = mysql_fetch_array($result)){

    echo "<font color= maroon><font size=2><b>";

    echo $row['event'];

echo "</b></font>";

    echo "<font color= black><font size=2>";

    echo $row['article'];

echo "</font>";

    echo "<a target=\"_blank\" href='$row[link]'>   Go there</a>";

    echo "</font>";

    echo "<br />";

    echo "<br />";

}

?>

 

Thanks again...

8)

Ron

Link to comment
Share on other sites

OK. What is the format of your $row['link']? Are they absolute URL's or relative?

If the former, you can simply check for your domain. For the latter, external URL's must begin with http:// so you could check if that exists. Then you just alter the link HTML accordingly.

Link to comment
Share on other sites

The links are all absolute - they have to be for the externals for sure.  But for consistency, I did the internals as absolute also.

 

examples:

 

external:  http://www.jcfeedandsupply.com/events.htm

 

internal: http://www.lakesideca.com/lakeside/events/revitilaztion.htm

 

I don't understand your reference to "Then you just alter the link HTML accordingly." 

 

Everything I am doing here is through the database and PHP coding - no HTML (to that degree).

 

Pardon me for not getting that part...

 

Thanks again...

 

???

Ron Scott

Link to comment
Share on other sites

Ok, that makes it easy to approach.

 

while($row = mysql_fetch_array($result)){
    if(strpos($row['link'], 'lakesideca.com') === false) {
       $linkTarget = '_blank';
    } else {
       $linkTarget = 'container';
    }

    echo "<font color= maroon><font size=2>";
    echo $row['event'];
   echo "</font>";
    echo "<font color= black><font size=2>";
    echo $row['article'];
   echo "</font>";
    echo "<a target=\"$linkTarget\" href='$row[link]'>   Go there[/url]";
    echo "</font>";
    echo "";
    echo "";
}

 

The if statement simply checks if the string "lakesideca.com" (internal links) is in the link field, and if so sets $linkTarget to "container", otherwise it must be external so it get set to _blank.

 

That should get you on your way.

 

 

caution: quick off-the-cuff untested code

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.