0

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 :)

3
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by MagicMedia
0

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);
1

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.

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.