0

I have a community site, where people can put up pages, and the url generates from their name. e.g. My Business, becomes my-business. I want to create a way, that if there is another My Business, it will check and make the url my-business-2, my-business-3, etc.

function check_url($url) {
  $qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
  if(mysqli_num_rows($qry)>0) {
        $slugs = array();
        while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
        if(in_array($url, $slugs)) {
              $max = 1;
              while(in_array(($url . '-' . ++$max ), $slugs)) $url .= '-' . $max;
        }

  }
  return $url;
}

This is my function, but this still wont work, as if there is a business called My Bus, it will make it my-bus-2, when it would have been unique at my-bus. I have experimented with other functions too, but this one is the closest I got. Can anyone tell me one that works perfectly?

Vivian Kennedy
  • 121
  • 1
  • 8
  • I think you should have a column something like `title` with just "my businuss" in it for maybe three rows. When you select the `title` you can use `mysqli_num_rows` to put the amount of rows behind the title which entirely becomes the url – SuperDJ Aug 26 '15 at 22:09
  • nope, because what if someone deletes their listing – Vivian Kennedy Aug 26 '15 at 22:11
  • http://stackoverflow.com/q/5416548/1301076 – rjdown Aug 26 '15 at 22:40

2 Answers2

0

So form my understanding, you are trying to avoid inserting/generating duplicate URL. Output will be something like this -

www.example.com/my-business
www.example.com/my-business-1
www.example.com/my-business-2
www.example.com/my-business-3
...

Try using this function -

function check_url($base_url, $new_url='', $num=0) {
  if($new_url == '') {
    $new_url = $base_url;
  }
  $qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url = '$new_url'");
  if(mysqli_num_rows($qry) > 0) {
    while($row = mysqli_fetch_array($qry)) {
      $num++;
      check_url($base_url, $base_url . '-' . $num, $num);
    }    
  }
  return $url;
}

Basically, this function will check the database until it finds a valid URL. Each time it will increment the number and recursively call the same function to generate new/valid URL.

Simple and basic!

SpritsDracula
  • 1,080
  • 9
  • 16
0

@SpritsDracula has working code, but his function will query your database each time it executes. That being said, the recursion is a nice concept. Here is a piece of code that will save your the multiple database calls.

function check_url($url) {
    $qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
    if(mysqli_num_rows($qry)>0) {
        $baseUrl = $url;
        $slugs = [];
        while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
        $max = 1;
        while(in_array($url, $slugs)) {
            $url = $baseUrl . "-" . $max++;
        }

    }   
    return $url;
}
Joel Lubrano
  • 606
  • 3
  • 9