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
https://forums.phpfreaks.com/topic/114899-solved-unique-link-target-problem/
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

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.