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.

Problem with creating hyperlinks

Discussion in 'PHP' started by chetandhiman, May 18, 2010.

  1. #1
    Hi,
    I am trying to create hyperlinks using data from mysql but I am having problem with it. What I am trying to do is take Name of the person url of the website of the person from mysql and create a hyperlink pointing to the website with name of the poster as anchor text.

    <?php $username= $results[$i]['Name'];
    $weburl= $results[$i]['Website'];
    $output = "<a href=\"$weburl\" target=\"_blank\">$username</a>";
    ?>
    This is generating hyperlink in this format:
    http://www.mywebsite.com/http://www.websiteofuser.com

    How to remove http://www.mywebsite.com from the above link and create a hyperlink to the website of user with his name as anchor text?
    :confused:
     
    Last edited: May 18, 2010
    chetandhiman, May 18, 2010 IP
  2. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Its should be like this

    
    $username= $results[$i]['Name'];
    $weburl= $results[$i]['Website'];
    $newurl = str_ireplace("http://www.mywebsite.com/","","$weburl");
    $output = "<a href=\"$newurl\" target=\"_blank\">$username</a>";
    
    PHP:
     
    Last edited: May 18, 2010
    roopajyothi, May 18, 2010 IP
  3. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    It didn't work however it should :(
    BTW I like your signature :)
     
    chetandhiman, May 18, 2010 IP
  4. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Have you changed this

    $output = "<a href=\"$newurl\" target=\"_blank\">$username</a>";
     
    roopajyothi, May 18, 2010 IP
  5. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Yes here is the current code that is in the file:

    <?php
    $username= $results[$i]['Name'];
    $weburl= $results[$i]['Website'];
    $newurl = str_ireplace("http://www.vedikastro.co.cc/","","$weburl");
    $output = "<a href=\"$newurl\" target=\"_blank\">$username</a>". " said:"; ?>
    <div class="name"><?php echo $output; ?> </div>

    If you want I can attach all the files I am using.
     
    Last edited: May 18, 2010
    chetandhiman, May 18, 2010 IP
  6. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    No reply.....:(
     
    chetandhiman, May 19, 2010 IP
  7. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    Why do you need to mess up your code with escape character?

    Try this:

    $output = '<a href="' . $newUrl . '" target="_blank">' . $username . '</a> said:';
     
    mike.judd, May 19, 2010 IP
  8. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    problem is still the same. I think it is because of the pagination code. Kindly take a look at the whole file and suggest something.
     

    Attached Files:

    chetandhiman, May 19, 2010 IP
  9. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #9
    Instead of paging like this, I suggest you to you the LIMIT in the SQL version to paging

    $startPage = $_POST['start'];
    $pageNum = 4;

    $sqlQuery = "SELECT * FROM data LIMIT " . $startPage . " , " . $pageNum;

    ....

    Then, try to output the Url of user. If the URL is correct, then plug into the output statement. Hope that helps!
     
    mike.judd, May 19, 2010 IP
  10. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    I removed the pagination code problem is still there even with this simple code:

    <?php

    $username="chetan";
    $password="racercar";
    $server="localhost";
    $database="comments";

    $db_handle = mysql_connect($server, $username, $password);
    $db_found = mysql_select_db($database,$db_handle);
    if($db_found) {
    $sql = "SELECT * FROM data ORDER BY 'postdate' DESC";
    $result = mysql_query($sql);
    while($db_field = mysql_fetch_assoc($result)){
    print $db_field['postdate']. "<BR>";

    $name = $db_field['Name'];
    $url = $db_field['Website'];

    $output = '<a href="' . $url . '" target="_blank">' . $name . '</a> said:';
    print $output;

    print $db_field['Comment'];
    }
    }
    else {
    print "Database NOT Found ";
    mysql_close($db_handle);
    }

    ?>
     
    chetandhiman, May 19, 2010 IP
  11. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #11
    Could you print out only $url for me. I want to see its value
     
    mike.judd, May 19, 2010 IP
  12. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #12
    chetandhiman, May 19, 2010 IP
  13. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #13
    All right, I know where's your problem at. The Url stored in the database must be in this form http://example.com, not example.com to work.

    I just try it myself. When I have http://, it works fine. If I don't have, it will change to relative path right away.
     
    mike.judd, May 19, 2010 IP
  14. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #14
    Oh Yes!!:D It works fine.
    Thanks a lot!
    Is there any way I can make this http:// thing in url field permanent so even if user enters without http:// it gets added automatically? Should I work on my html form or is there any way to do it in php?
     
    chetandhiman, May 19, 2010 IP
  15. mike.judd

    mike.judd Greenhorn

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #15
    well, you would need to put it right when the user input it. So you will you substr function to extract 6 first letter, check to see whether it is http://. If it is, store as is. If not, then add it. That's why you can always make sure the URL saved in the database is legitimate
     
    mike.judd, May 19, 2010 IP
  16. chetandhiman

    chetandhiman Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #16
    Thanks a lot again. With your help I have learnt something new :)
     
    chetandhiman, May 19, 2010 IP
  17. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #17
    Just
    Do this things
    
    <?php
    $urlsubmittedbyuser = 'example.com';
    $prefixurl = 'http://';
    $newurl = "$prefixurl"."$urlsubmittedbyuser"; // use this variable to write in DB
    ?>
    
    PHP:
     
    roopajyothi, May 20, 2010 IP
  18. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #18
    you can also use this:
    
    <?php 
       $username= $results[$i]['Name'];
       $weburl= str_replace('http://http://', 'http://', 'http://'. $results[$i]['Website']);
       $output = sprintf('<a href="%s" target="_blank">%s</a>', $weburl, $username);
    ?>
    
    PHP:
     
    gapz101, May 20, 2010 IP
  19. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #19
    Gapz I have already said that here
    http://forums.digitalpoint.com/showthread.php?t=1806635&p=14219109#post14219109

    But he says that its not working!
     
    roopajyothi, May 20, 2010 IP
  20. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #20
    ^^ i'm just sharing idea :D
     
    gapz101, May 20, 2010 IP