Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Mysql


  • Please log in to reply
9 replies to this topic

#1 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 28 January 2010 - 08:06 AM

Hi

I am pulling info from a database to a webpage and I would like to create a clickable link.

The link would take this form...

www.my-site.com/name/detail-20151-this-is-my-link.html

I have set up a page that displays all the contents of a table using echo. (part sample below) This works fine.

The first code is the id number with "20" before, to make it a 5 figure number.
(This is the number in the url above) The database stores 3 figure numbers.

The second piece of code, 'listing_title_1' is the "this-is-my-link" part

The "-" has to be inserted before and after the number, as above.

And the ".html" added at the end.

CODE
20<? echo $rows['listing_id']; ?>
      
<? echo $rows['listing_title_1']; ?>


I have searched this and cant find a way to get a clickable link

Can anyone help?

#2 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 28 January 2010 - 08:48 AM

If I understand correctly, then what I would probably do is this:-

CODE
<? echo '<a href="http://www.my-site.com/name/detail-20', $rows['listing_id'], '-', $rows['listing_title_1'],'.html">Click here</a>'; ?>


Hope that makes sense

#3 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 28 January 2010 - 09:16 AM

Hi rolf

Thanks for the quick response.

You were spot on.

However, I didn't notice at first but the urlīs are produced automaticly from user input. (The page title)

i.e. this url
CODE
http://www.my-site.com/folder/detail-20151-la-isla-d7-holiday-apartment-in-javea-spain.html


is produced from id #20151
QUOTE
La Isla D7 - Holiday apartment in Javea Spain


Notice the capitals are changed from upper to lower case and hyphens are inserted in place of the spaces.

This is all done in the background created by the cms.

Using your code I ended up with a link...
CODE
http://www.my-site.com/folder/detail-20151-La Isla D7 - Holiday apartment in Javea Spain.html


Which is a broken link, of course. Not your fault, mine!

I think this is way beyond me.

Thanks for the help and I hope you have a good year.

M

#4 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 28 January 2010 - 10:00 AM

Hi,

glad to be of some help.

the php function str_replace() would be able to swap the spaces for hyphens and I'm pretty sure there is a function to change upper case to lower case, although I can't quite put my finger on it. (upper_to_lower() maybe?)

From a few of the posts you've made it seems like you're close enough to grasping the concepts behind php that you would get a lot of benefit from looking around php.net - start off by doing a search for str_replace() for a thorough explanation of that function and to see the sort of help on the site.

If I had more time I'd try to find the upper to lower case function for you but I'm about to go into a meeting so have to go, sorry :-s

good luck with it

#5 cfreek

cfreek

    AAAHHHH!!1one1!

  • Active Members
  • PipPipPipPip
  • 166 posts
  • Location:Richmond, Virginia, Earth

Posted 28 January 2010 - 11:53 AM

To lower case:

strtolower()
http://php.net/manua....strtolower.php


#6 1dmf

1dmf

    Keep Asking, Keep Questioning, Keep Learning

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,167 posts
  • Location:Worthing - England

Posted 29 January 2010 - 04:52 AM

You see why I love perl rolf.... wink1.gif

lc() , uc() and to replace.... s/[what_to_find]/[what_to_replace_it_with]/;

#7 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 29 January 2010 - 02:40 PM

With help from Ralf I got this to work thumbup1.gif

CODE
<? echo '<a href="http://www.my-site.com/name/detail-20', $rows['listing_id'], '-', $rows['listing_title_1'],'.html">Click here</a>'; ?>


However the "listing_title_1" part needs the whitespace riped out and replaced with a hyphen

At the moment I am getting this as the link...

CODE
http://www.my-site.com/name/detail-20139-Villa%20Estepona%20-%20Attractive%20holiday%20vill%20in%20Javea.html


As I said, it works but just needs the final ingredient.

Anyone got the magic answer

Edited by madams, 29 January 2010 - 02:49 PM.


#8 Mhoram

Mhoram

    HR 4

  • Active Members
  • PipPipPipPip
  • 114 posts
  • Location:Quincy, Illinois, USA

Posted 29 January 2010 - 04:42 PM

QUOTE(madams @ Jan 29 2010, 01:40 PM) View Post
However the "listing_title_1" part needs the whitespace riped out and replaced with a hyphen


Use the regex routines borrowed from perl:

$rows['listing_title_1'] = preg_replace('/\s+/','-',$rows['listing_title_1']);


#9 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 29 January 2010 - 05:38 PM

Thanks Mhoram

Works great

Is there a way in that code to strip a hyphen out first, then insert one.

I have titles like below that makes up the url...

Fish Eggs - Fried Fish Eggs Are Great

Using Rolfs suggested code and yours the output url is...

CODE
http://www.my-site.com/name/detail-20139-Fish Eggs---Fried-Fish-Eggs-Are-Great.html


Although this works as a link it is not quite correct.

Is there a way to achieve this.




#10 Mhoram

Mhoram

    HR 4

  • Active Members
  • PipPipPipPip
  • 114 posts
  • Location:Quincy, Illinois, USA

Posted 30 January 2010 - 12:16 PM

I'd suggest reading up on regex, as pattern matching is an extremely powerful tool that's commonly needed in any sort of text processing.

Then think about exactly what it is you want to find and replace. You could do it in two steps as you describe, by first stripping out all hyphens and then replacing each chunk of whitespace with a hyphen:

CODE
preg_replace('/-/','',$string);
preg_replace('/\s+/','-',$string);


But if you have any cases where a hyphen is used without whitespace on either side, like "Fish Eggs-Fried Fish", then there won't be whitespace for the second command to replace, and you'll get "Fish EggsFried Fish".

You can fix that by having the first replacement replace each hyphen with a space:

CODE
preg_replace('/-/',' ',$string);


Or you could use a more complex single command that replaces any combination of whitespace and hyphens with a single hyphen:

CODE
$str = "Fish-Eggs - Fried         Fish";
print preg_replace('/[-\s]+/','-',$str);

output: Fish-Eggs-Fried-Fish





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

We are now a read-only forum.
 
No new posts or registrations allowed.