Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP prevent double clean url (improvements?)
    primarykey
    data
    text
    <p>For a client at work we have build a website.The website has an offering page which can contain variants of the same type/build, so they ran into problems with double clean-urls.</p> <p>Just now I wrote a function to prevent that from happening by appending a number to the URL. If thatclean url also exists it counts up.</p> <p>E.g.</p> <p>domain.nl/product/machine</p> <p>domain.nl/product/machine-1</p> <p>domain.nl/product/machine-2</p> <p>Updated! return $clean_url; on recursion and on return</p> <p>The function I wrote works fine, but I was wondering if I have taken the right approach and if it maybe could be improved. Here's the code:</p> <pre><code>public function prevent_double_cleanurl($cleanurl) { // makes sure it doesnt check against itself if($this-&gt;ID!=NULL) $and = " AND product_ID &lt;&gt; ".$this-&gt;ID; $sql = "SELECT product_ID, titel_url FROM " . $this-&gt;_table . " WHERE titel_url='".$cleanurl."' " . $and. " LIMIT 1"; $result = $this-&gt;query($sql); // if a matching url is found if(!empty($result)) { $url_parts = explode("-", $result[0]['titel_url']); $last_part = end($url_parts); // maximum of 2 digits if((int)$last_part &amp;&amp; strlen($last_part)&lt;3) { // if a 1 or 2 digit number is found - add to it array_pop($url_parts); $cleanurl = implode("-", $url_parts); (int)$last_part++; } else { // add a suffix starting at 1 $last_part='1'; } // recursive check $cleanurl = $this-&gt;prevent_double_cleanurl($cleanurl.'-'.$last_part); } return $cleanurl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload