0

I created a method for making auction names websafe, and i just added a check for dupplicate webnames, which basicly tries to fetch an auction with the given websafe name, then adds a random number to the webname. But the method ends up returning null...

private function dupplicateUrlFix($url){
    var_dump($url)//Correct
    $existingAuction = Auction::get($url, "webname");

    //if webname exists, cancatenate with random int, and check if new webname exists
    if(!empty($existingAuction->webname)){
        $newUrl = $existingAuction->webname.rand(0,9);
        $this->dupplicateUrlFix($newUrl);
        return;
    }
    var_dump($url) //Correct Not a dupplicate
    return $url;
}

public function get_url_clean($string) {
    $string = strToLower($string);
    //cleaning....
    //checks for dupplicate
    var_dump($this->dupplicateUrlFix($string)); //is null?
    die();

    return $clean;
}

I have tried switching the order in the dupplicateUrlFix() method and simply returning a string. It only goes into the if(!empty... one time.

Is there a solution for this, or a better approach?

5
  • I don't think $existingAuction->webname.rand(0,9) is valid. Perhaps do $newUrl = ($existingAuction->webname).rand(0,9) Commented Sep 18, 2014 at 17:20
  • @Doctus It makes more visual sense spaced as $newUrl = $existingAuction->webname . rand(0,9) <- concatenation. Commented Sep 18, 2014 at 17:21
  • @MichaelBerkowski that's subjective - they're both valid Commented Sep 18, 2014 at 17:22
  • @Doctus I didn't say your was invalid. I thought you implied it was invalid because the dot was intended as a class operator. I misread your comment. Commented Sep 18, 2014 at 17:23
  • Oh I see what you mean! Yeah I'm not exactly sure, but I know in the past when concatenating object properties with strings I've had to wrap them in brackets. Commented Sep 18, 2014 at 17:24

1 Answer 1

1

I think there's no need for recursion here:

private function dupplicateUrlFix($url) {
    $existingAuction = Auction::get($url, "webname");
    while (!empty($existingAuction->webname)) {
        $url = $existingAuction->webname . rand(0, 9);
        $existingAuction = Auction::get($url, "webname");
    }

    return $url;
}
Sign up to request clarification or add additional context in comments.

5 Comments

That works! Thank you! it even looks alot cleaner :)
It can be even converted to do .. while so as not to write $existingAuction = ... twice)
You'll have an infinite loop when you hit the 12th name. And assuming the get() method hits a database, you'll have an infinite loop hitting the database.
Still we can add some counter to while to e.g. throw exception when number of attempts exceeded.
rand(0, 9) returns one character. So, you'll get the base name foo plus ten extra names foo0, foo1, etc. After eleven attempts, you have no more names to try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.