[php] HREF="" automatically adds to current page URL (in PHP). Can't figure it out

Longtime reader of stackoverflow but first question.

I'm working with Wordpress (specifically thesis theme) in the custom_functions.php file and am finding for some reason is automatically adding the current page url. For example this code is to query the database and then loop through outputting each product in it's own div:

$result = db_connection($query);
while ($row = mysql_fetch_array($result)) { ?>
    <div class="box"><a href="">
        <img src="http://www.electricbikehub.co.nz<?php echo $row['product_root_directory']         . $row['mid_size_image'] ?>">
        <h2><?php echo $row['name']?></h2>
        <p><?php echo $row['description_brief'];?></p>
        <p><span class="multiple_product_red"><span class="multiple_product_linethrough">RRP: <?php echo $row['list_price']; ?>.</span> Our discounted price: <?php echo $row['our_price']; ?>. Includes delivery and GST.</span></p>
        </a>
    </div>
<?php } ?>

As you can see 3rd line says href="" but the actual link being generated is the current page (in this case 'http://www.electricbikehub.co.nz/?page_id=1192'). If I add anything in the href, such as href="something" it will just add it to the end, ie http://www.electricbikehub.co.nz/?page_id=1192something.

Any help would be greatly appreciated!

This question is related to php html

The answer is


Add http:// in front of url

Incorrect

<a href="www.example.com">www.example.com</span></p>

Correct

<a href="http://www.example.com">www.example.com</span></p>

You do realize this is the default behavior, right? if you add /something the results would be different.

you can do a number of things to prevent default behavior.

href="#":

Will do nothing but anchor - not the best solution since it may jump to page top.

<a href="#">

href="javascript:void(0);"

Will do nothing at all and is perfectly legit.

<a href="javascript:void(0);"></a>

href="your-actual-intended-link" (Best)

obviously the best.

<a href="<your-actual-intended-link>"></a>

If you don't want an a tag to go somewhere, why use an a tag at all?


In any case, your code will generate invalid markup: You shouldn't wrap block contents in a link. tags don't work like this. If you want this effect you should use js or create an absolutely positioned link above the content (z-index). More on this here: Make a div into a link.

You should make sure to validate your code when it renders: http://validator.w3.org


A solution that works no matter if you are developing on a local server or live is to add "//" in front of your link. This will effectivly remove the URL of the site you are currently on.

Example:

<a href="www.google.com">Google.com</a>

This will in output http://localhost/mySite/currentPage/www.google.com

What you should do instead is this:

<a href="//www.google.com">Google.com</a>

This will output www.google.com


You can just put // in front of $yourUrl in href:

<a href="//<?=$yourUrl?>"></a>

Use this:

<a href="<?php echo(($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">Whatever</a>

It will create a HREF using the current URL...


if you want to redirect it to some other url lets google.com then make your like as happy to help other says rikin <a href="//google.com">happy to help other says rikin</a> this will remove self site url form the href.