Hi All,
Can anyone see why this line dos not produce real links if they have "http://" add to them when added to the data base ?

//----------------------------------------------

$result = mysql_query("SELECT * FROM track ORDER BY rand()");while($row = mysql_fetch_array($result)) { echo '<td> <a class="mylink" target=_parent href="http://', $row['car'],'">',$row['tag'] . "<br>"; }

//----------------------------------------------------

the links work well as long as the link is added "www.abc123doerayme.com" but not when added like "http://www.abc123doerayme.com"

try,

   echo '<td> <a class="mylink" target="_parent" href="http://'.$row['car'].'">'.$row['tag'].'<br>';

echo '<td> <a class="mylink" target="_parent" href="http://'.$row['car'].'">'.$row['tag'].'<br>';

thanks for the help veedeoo , can see that you changed the"," to "." and i sttill get the error page and the web addy is http//www.abc123 ect ect they all are missing the " : "

hi,

can you change your code above to this..

//echo '<td> <a class="mylink" target="_parent" href="http://'.$row['car'].'">'.$row['tag'].'<br>';

echo $row['car']."<br/>";
echo $row['tag']."<br/>";

Tell us what do see on the screen..

nope that prints out both 'car' and 'tag' nether of which are live to click,
car = hyperlink
tag = headline to hyperlink

The reason I made you echo the items coming form your database is to make sure that we are getting something. Now, that we are sure of that. Let's put back the code only with the following correction.

echo '<td> <a href="http://'.$row['car'].'" class="mylink" target="_parent">'.$row['tag'].'</td>';

At first, I did not notice that your href is way outward .. I am not sure if this is what is causing the : to disappear, but I think it is worth trying. On the other hand, I don't think it would be the case though..

I just wan't to make sure that we can duplicate the same error on a regular link..Can you please create a new file , paste the code below and save the file as anyname.php , load this file to your server..

  <?php
 $url= "daniweb.com/web-development/php/threads/422216/url-not-working-right";
 $link = "daniweb";
   echo '<td> <a class="mylink" target="_parent" href="http://'.$url.'">'.$link.'<br>';
?>

if you click the link on it, it should take you back on this thread. Please double check that : is not missing on the link.

To make sure that it is not a parsing problem, the code can also be use.. Assuming that php shorthand is enabled on your server.

 $result = mysql_query("SELECT * FROM track ORDER BY rand()");
   while($row = mysql_fetch_array($result)) {

 ?>
    <td> <a  href="http://<?=$row['car']?>" class="mylink" target="_parent"><?=$row['tag']?></td>
<?php
 }

If the link still don't connect to the tartget url when http:// is used, it is the target site that has a DNS problem.

One last note for you to consider, if the row['car'] is printing http:/someDomainDotCom, then the href above should only be like this

   href="'.$row['car'].'"

yep your link code got me back here.

Quote just wan't to make sure that we can duplicate the same error on a regular link..Can you please create a new file , paste the code below and save the file as anyname.php , load this file to your server..

<?php
$url= "daniweb.com/web-development/php/threads/422216/url-not-working-right";
$link = "daniweb";
echo '<td> <a class="mylink" target="_parent" href="http://'.$url.'">'.$link.'<br>';
?>

have used this code

echo '<td> <a class="mylink" target="_parent" href="http://'.$row['car'].'">'.$row['tag'].'<br>';

works how ever if it has "https://" in the base it links to " http://http//www."

I suspected that might have been the case, that's why I want the echo row['car'].. The problem here is the http//www got inserted on your database. If you don't have any way of correcting them, because your database has already been populated by thousands of those, our last resort to test this code first..If it works then we will use the preg_replace.. it will be double processin on the part of your server. However, that is the only way I could think as quick fix right now.

Remember our test code above? Let's use it again . It is a lot easier that way,,

  $url = preg_replace("/^https?:\/\/(.+)$/i","\\1", $row['car']);
  ## we can echo the url to make sure the http// has been removed.
  echo $url."<br/>";

Once again, I want you to post what do you get? You should be getting something like this www.SomeDomainDotCom. without the htpp//.

ADDED LATER: It should be like this, because there is no :

 $url = preg_replace("/^http?\/\/(.+)$/i","\\1", $url);

I suspected that might have been the case, that's why I want the echo row['car'].. The problem here is the http//www got inserted on your database. If you don't have any way of correcting them, because your database has already been populated by thousands of those, our last resort to test this code first..If it works then we will use the preg_replace.. it will be double processin on the part of your server. However, that is the only way I could think as quick fix right now.

Remember our test code above? Let's use it again . It is a lot easier that way,,

$url = preg_replace("/^https?:\/\/(.+)$/i","\1", $row['car']);
## we can echo the url to make sure the http// has been removed.
echo $url."<br/>";
Once again, I want you to post what do you get? You should be getting something like this www.SomeDomainDotCom. without the htpp//.

ADDED LATER: It should be like this, because there is no :

$url = preg_replace("/^http?\/\/(.+)$/i","\1", $url);

output ===========

daniweb.com/
web-development/php/threads/422216/url-not-working-right

both link back to here live,
base is allmost empty just a now,
so will this strip out any "http://" thats added later ?

Please double check on my added Later info. above. I normally type in the codes raw in this text area, so changes on my posted codes can occur between the time I posted it upto the set time, I can no longer edit.. it should work. Your corrected code should be like this...

$result = mysql_query("SELECT * FROM track ORDER BY rand()");
    while($row = mysql_fetch_array($result)) {
     $url = preg_replace("/^http?\/\/(.+)$/i","\\1", $row['car']);

    ?>
    <td> <a href="http://<?php echo $url; ?>" class="mylink" target="_parent"><?php echo $row['tag'];?></td>
    <?php
    }

I remove the short tags, because I don't know your server settings. It is a lot safer this way..

Yes, it will strip http// ONLY, and not www.someurl from your database as row['car'] value. the link of the corrected codes above should be like this http://www.whateverDotCom.

commented: missed a : however very helpful and very quick and thank you,jerrijeff. +2

needed the ":" to be added to

preg_replace("/^http?\

and works a treat :@) thanks veedeoo :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.