Hello everyone and thank you for taking your time to view my post.
I am new to this forum and this is my first post, I hope we get along.
My current code works like this :
$result_variable = increment_link($_POST['event-name'],"permalink-column","events");
Where
$_POST['event_name']
is the name of the event you get from POST.
"permalink-column"
is the column in my "events"
table.
As an example $_POST['event-name']
would be let’s say “Eating Contest in Pittsburg”.
That Gets converted with the seoURL function below and it becomes “eating-contest-in-pittsburgh”.
Then it checks with the other function if it exists in my “permalink-column” in the “events” table.
If it doesn’t exist it returns it as it is if it does exist it adds “-1” at the end and if there exist both of those answers it increments further with “-2” and so on.
My problem is that it does what its supposed to do but it also removes the last word so instead of
“eating-contest-in-pittsburg-1”
I get
“eating-contest-in-1”
This is the first function used inside the next one:
function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
The problem function :
function increment_link($text,$field=null,$table=null,$optional_sql='') {
global $connection;
$link = seoUrl($text);
//existing
$sql = "SELECT * FROM " . $table . " WHERE " . $field."='{$link}' " . $optional_sql;
$exists = mysqli_fetch_assoc(mysqli_query($connection,$sql));
if (is_array($exists))
{
$elements = explode("-", $link);
if (count($elements) > 1)
{
$value = $elements[count($elements)-1]+1;
$link = '';
$i = 0;
while ($i<count($elements)-1)
{
if ($i == 0) $link .= $elements[$i];
else $link .= "-" . $elements[$i];
$i++;
}
$link .= "-" . $value;
$link = increment_link($link,$field,$table);
}
else
{
$link .= '-1';
$link = increment_link($link,$field,$table);
}
}
return $link;
}