Hi all

I have made a website for a client, a little like a small forum.

There are these inputs when starting a new thread:

Headline
URL
Content

In the URL they can type only letters numbers and a "-". So that their threads URL can be like http://www.something.com/show/this-is-my-thread

But I dont want them to type it themselves. So is there a way it can be automated? Like if they fill out the headline like so: "Why does this not work?" the URL will be turned into this http://www.something.com/show/why-does-this-not-work

How is this possible?

Sorry if I didnt explain properly, Im no good at english explaining :)

You want users to manually type in a weird URL? Biggest usability mistake right there.

However, if you still insist....

$url = $_GET['data'];
$url = str_replace(' ','-',$url);

No no, thats exactly the opposite of what I want. I want so that it generates the URL from the headline. If the headline is example "This is a test!" the URL should automatically be http://www.something.com/show/this-is-a-test

I assume you want functionality like wordpress, where as you type into your title, the url is generated from it and displayed for the user.

While this could be achieved with pure javascript, I think your best solution will be to use an ajax request, so it can check the database if the url is already taken.

I'm just going to reference some jquery stuff as you can try to put the pieces together before we go any further, but essentially, the user enters the headline and on a .focusout() event (when they leave that input field for something else) you take whats in the headline field (See jQuery selectors) and make an ajax GET request to a php script which returns json (for this example) .

The php script, filters the headline to Alphanumeric and spaces. Then replaces the spaces with dashes and then lowercases the entire string. (Filter the string first as this will prevent any multi-byte issues)

<?php

$string = $_GET['headline']; //From the ajax get request. Should produce this-is-my-test-headline
$string = preg_replace( '/[^0-9a-zA-Z ]/i', '', $string );
$string = str_replace( ' ', '-', $string );
$string = strtolower($string);

/* 
Do a database lookup and return a count() value
e.g.

$stmt = $dbh->prepare("SELECT COUNT(ThreadId) AS ThreadCount WHERE Url = ?");
if ( $stmt->execute( $string ) ) {
  //Should only return ONE row
  $row = $stmt->fetch();
  if( $row['ThreadCount'] != '0' ){
	//Tack on ThreadCount + 1 to the headline
	$string = $string . ($row['ThreadCount'] + 1);
  }
}
*/

//Include a couple headers so it doesn't get cached 
//and gets served with correct content-type
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode( array( 'url' => $string ) );

You will need to put your own database connection and query together for your environment but that should give you an idea.

Once the json is returned.

{"url":"this-is-my-test-headline"}

You javascript (using jQuery) would be something like this:
headline would be selected from your headline field's value.
This is also jQuery 1.5 functionality.

var jqxhr = $.getJSON("path/to/your/phpscript.php",
{
  headline : "This is my test headline!"
},
function(json) {
  //Set json.url as the url field's value
})
.error(function() { alert("error"); }));

The only thing to keep in mind is you can NOT trust user-supplied data so when the thread is created run the headline back through the php portion of the code and if the generated headlines match save it, if not handle errors how you see fit.

This is NOT tested or meant to be a fully working example.

Wow thanks man, your kind! I will try to work with this - thanks for giving me a good start :)

You need to use Apache to edit the .htaccess file on your server to manipulate URLs. The command you need is mod_rewrite. You should be able to Google ".htaccess mod_rewrite clean urls" and find your answer easily. Can't help you too much there as Apache really isn't my strong suit.

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.