TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-17-2010, 10:35 AM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default If duplicate title, add rand else, do nothing??

Hi all!

Basic stuff, maybe but I jsut can't figure it out? Can someone help me here?

If duplicate title, add rand numbers, if not duplicate title then do nothing. That's easy but when I need to check if that modifyed title isn't also duplicate I hit the wall..

Here's code (codeigniter):

Code:
$title = $this->input->post('title');
if(this->db->where('title', $title)->get('posts'))
{
  $rand = rand(1000, 9000);
  $title = $title . '-' . $rand;
}

Thanks for all help!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 02-17-2010, 04:59 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Your question isn't very descriptive. What are you doing in the first place?
__________________

Village Idiot is offline  
Reply With Quote
Old 02-17-2010, 05:26 PM   #3 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

I'm converting title as product url which i will then insert into product table in database. eg. "Fast computer whit awesome monitor" would be "fast-computer-whit-awesome-monitor".

If users are adding same title for products, we'll add end of url some random numbers eg."fast-computer-whit-awesome-monitor-86553". When generating that random number, how do I check that there is not url whit same random number in place?

Thanks..
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 02-17-2010, 10:12 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

The same way you would check whether or not you needed to generate the random number for duplicate titles in the first case. Make it a function, 'check_duplicate()' and run it once to see if the number needs to be generated, and again to make sure the generated number isn't already in there.

Or take a page from WordPress and generate it in numerical order. Find the last title, 'some-duplicate-title-2' and add 1 to it.
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is causing Duplicate Entries buildakicker MySQL & Databases 5 07-30-2009 04:00 PM
Img Title Embeded on Image? buildakicker General 3 01-08-2009 01:05 AM
[url] tag automatically grabs site title Runar Feedback 7 11-11-2008 03:29 PM
Duplicate Pagination Dan Feedback 2 02-26-2008 04:15 PM
mod rewrite - how to get title in url? Sam Granger Absolute Beginners 5 10-10-2007 02:58 PM


All times are GMT. The time now is 06:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design