Help with permalink generation code

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


$_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
I get

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);
  $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];
      $link .= "-" . $value;
      $link = increment_link($link,$field,$table);
      $link .= '-1';
      $link = increment_link($link,$field,$table);
  return $link;

i think this is more complicated then it should be. maybe you just want to take the link, do a loop, incrementing a counter and check every step against your database? this would be less efficient - but how many increments do you plan for a single link? this is done only once per link i guess and the result is saved/cached.

at least check the link on a numeric suffix and increment that value by 1.

PS: or just append the ID

I know it isn’t efficient and it can also be done with other methods but I need to make it work this way. Thank you for your reply.

Isn’t this the problem with it missing off the last word?

      while ($i<count($elements)-1)

For “eating-contest-in-Pittsburgh”, count() will return 4, your loop will run while $i is 0…2 (because it’s running while $i is less than count-1, whereas you really want to be running 0…3, unless there is already a numeric suffix. So you need to figure out whether you already have a number suffix before you decide how that loop should run.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.