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?
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:
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.
Why do you need to mess up your code with escape character? Try this: $output = '<a href="' . $newUrl . '" target="_blank">' . $username . '</a> said:';
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.
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!
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); } ?>
It gives exact value that user has entered. If I have entered google.com it shows google.com only but when I try to make it a hyperlink it gets messed up with http://localhost/try/google.com Even if I use str_ireplace to remove http://localhost/try, it keeps adding http://localhost/try before the url entered by user. One more thing I tried, I didn't input any url so it made the name a hyperlink pointing towards the current page that is http://localhost/try/post.php
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.
Oh Yes!! 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?
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
Just Do this things <?php $urlsubmittedbyuser = 'example.com'; $prefixurl = 'http://'; $newurl = "$prefixurl"."$urlsubmittedbyuser"; // use this variable to write in DB ?> PHP:
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:
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!