1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How To Call Links In PHP?

Discussion in 'PHP' started by Adam Fletcher, Nov 19, 2011.

  1. #1
    I'm looking to call links on my .html pages from a php file. I would like one file (currently links.php) that holds all the link information that I can call. Here is what I have created so far:

    links.php file

    $link_text = "Link 1";
    $link_url = "http://www.google.com";
    
    $link_text2 = "Link 2";
    $link_url2 = "http://www.bing.com";
    
    ?>
    Code (markup):
    index.html file

    Is this the best way to call links to my html pages? I am very new to php so any advice you can give me would be appreciated :)
     
    Adam Fletcher, Nov 19, 2011 IP
  2. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What you are trying to do is 'return a variable'.
    Mate, time for you to go 1 step ahead and dive into Object Oriented PHP.
    One of the interesting php oop tut I saw was : http://www.youtube.com/watch?v=tTPW_fTJNNk
    Quick tutorial 11 mins but, worth it!

    For now:
    VERY BASIC would be:

    Links.php
    
    class Links
    {
    function linktext1()
    {
    $link_text = "Link 1";
    return $link_text;
    }
    function link1()
    {
    $link_url = "http://www.google.com";
    return $link_url
    }
    function linktext2()
    {
    $link_text2 = "Link 2";
    return $link_text2;
    }
    function link2()
    {
    $link_url2 = "http://www.bing.com";
    return $link_url2
    }
    }
    
    PHP:
    then, call it using your index.php file (NOT index.html)

    
    <body>
    <?php include("inc/links.php"); 
    $lin = new Links();?>
    
    <p>The search engine I most like is <a href="<?php print "$lin->linktext1()"; ?>"><?php print "$lin->link_text1"; ?></a></p>
    
    <p>I also search using <a href="<?php print "$lin->link_url2"; ?>"><?php print "$lin->link_text2"; ?></a></p>
    </body> 
    
    PHP:
    You can also use GLOBALS but, I would not recommend using it wrt Security of the website.

    Cool enough?
     
    eleetgeek, Nov 19, 2011 IP
  3. Jaxo

    Jaxo Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Another problem you might be having is that any file with PHP executing in it has to end with .php (or use a .htaccess line for really advanced people). So, naturally, your code isn't working ;). If you used this:

    
    <body>
    <?php include("inc/links.php"); ?>
    
    <p>The search engine I most like is <a href="<?php print "$link_url"; ?>"><?php print "link_text"; ?></a></p>
    
    <p>I also search using <a href="<?php print "$link_url2"; ?>"><?php print "link_text2"; ?></a></p>
    </body> 
    
    Code (markup):
    And saved it as index.php, then saved your $links file to links.php, it would work fine!

    Off-topic: I remember when I was in the very same position as you! I had the same setup going, too, an include with a variable in another file. Keep it up, man, one day you might become as great as I am xD (joking).
     
    Jaxo, Nov 22, 2011 IP
  4. Adam Fletcher

    Adam Fletcher Well-Known Member

    Messages:
    783
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Ha, thanks for the help guys! I've got it working now :)
     
    Adam Fletcher, Nov 23, 2011 IP
    Smyrl likes this.
  5. jasonsc

    jasonsc Well-Known Member

    Messages:
    1,696
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Great solution. If you php include a file from another source (let's say http://www.mysite.com/links.php), would that be usually blocked on servers? I am looking for a solution to be able to change links in a theme I am distributing, so that links are acutally called from a file on my sever. Maybe it does not even have to be a .php file, can be a .html file with a simple html code, so that people would trust it more.

    Is there any way to find out first if the file can be called from another server and if not, just use a static link?

    Thanks
     
    jasonsc, Nov 23, 2011 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Actually it's time for him to take 3 steps back and learn basic PHP before he dives into OO programming.
     
    NetStar, Nov 27, 2011 IP