#1
If we create a post with a name of 'My excellent holiday' , then if we have created a 'url_string' field in the table, this will be published to the db as 'my-excellent-holiday'. However, if we then create a second post of the same name, then we get the same identical URL string. Which I don't think is ideal.

I came up with the following code, but am struggling with an issue I have come up with before and my solution was not elegant and involved me writing some custom SQL code. It would be good to use the Trongate database helpers.

So, to ensure I do not have identical URL strings, I have the following code, but am getting the following error



My code is below and I have commented where I have the issue , but I can't understand why.


Regards

John
#2
If you do a var_dump on the $key variable, you'll find that it's an object. However, with your code you are treating it as if it's an array.

So, instead of saying:



You should say...



Does that help?
#3
Hi John, I just had a quick look at your code but think there might be a slight bug, it's always searching only for the url_string "my-excellent-holiday", if this exists it will be unique and your count will only ever return 1, so each subsequent post of same name would always be suffixed -2. I think you need to use the LIKE operator. You should be able to do this with just another simple little function if all you want is that each additional post of similar name gets suffixed with a number, eg. my-excellent-holiday, my-excellent-holiday-2, my-excellent-holiday-3 etc.

Change this line to use a function


then somewhere downstairs add a function like this


I haven't tested this but think it should work! :)
#4
Hi both,

many, many thanks for the response.

DC, quite right. I had being using the PHP Array lessons to try and get the code together in conjunction with TG helpers. In doing so I had missed the Object vs Array difference. Thanks again.

trefwoordpunk - you were quite right. I woke this morning and the first thing that came into my head was 'mmm.. not sure that code is going to quite do the job here' and rather marvelously, you had replied with a solution which does work.

Many thanks - all good now and I have unique URLs.

DC - could we have a place where we can share code solutions in a repository somewhere? Would be very useful.

In terms of marking which person solved my problem - tricky, because there were two solutions to two different problems, but just to keep the honors on an even keel, I am going to select trefwoordpunk ;-) Sorry DC ;-)
#5
You just made my day! I was encountering the same problem... thank you so much for this one.