รับกระสุนที่ไม่ซ้ำใครใน Codeigniter

ฉันต้องการรับกระสุนที่ไม่ซ้ำใครสำหรับบทความของฉัน ฉันใช้โค้ดอิกนิเตอร์ ฉันสงสัยว่าจะมีบางอย่างเช่น sample-title-1 และ sample-title-2 หากมีสองบทความที่มีชื่อเหมือนกันเหมือนกับที่ Codeignier ทำกับการอัปโหลดไฟล์ filename(:num) ฉันไม่สามารถหาวิธีที่จะทำได้ ฉันไม่ใช่ผู้เชี่ยวชาญด้าน Codeigniter ฉันกำลังเรียนรู้มัน

ฉันได้เตรียมฟังก์ชันไว้ เมื่อส่งผ่านสตริง $str มันจะตรวจสอบว่ามีกระสุนอยู่หรือไม่ หากมี มันจะเพิ่ม ID ของบทความนั้นที่ส่วนท้ายของกระสุนนั้น และส่งคืน ถ้าไม่มี ก็จะส่งคืนกระสุน

มันทำงานได้ดีและตอบสนองวัตถุประสงค์ของทากที่ไม่เหมือนใคร แต่สิ่งที่ฉันต้องการคือการมีบางอย่างเช่น sample-title-1 และ sample-title-2 มีวิธีใดบ้างที่จะทำเช่นนั้น?

$data['slug'] = $this->get_slug($data['title']);


public function get_slug ($str)
    {
        $slug = url_title($str, 'dash', true);
        // Do NOT validate if slug already exists
        // UNLESS it's the slug for the current page

        $id = $this->uri->segment(4);
        $this->db->where('slug', $slug);
        ! $id || $this->db->where('id !=', $id);
        $category = $this->category_m->get();

        if (count($category)) {
            return $slug.$id;
        }

        return $slug;
    }

person prakashchhetri    schedule 30.07.2013    source แหล่งที่มา
comment
เป็นไปได้ที่ซ้ำกันของ การใช้ slugs ใน codeigniter   -  person alpere    schedule 14.12.2013


คำตอบ (3)


ใช้งานง่ายและมีประโยชน์มากในการสร้างทากที่ไม่เหมือนใคร โปรดดูที่ CI slug Library

อ่านเอกสารประกอบเพื่อนำไปใช้

person umefarooq    schedule 30.07.2013

สิ่งที่ฉันเคยทำคือทำ slug db field UNIQUE

จากนั้นทำทุกอย่างได้อย่างง่ายดายด้วย Url Helper และ ตัวช่วยข้อความ

    $last_id_inserted = //get from db the last post's ID;
    $post_title = "My slug would be";
    $slug =  mb_strtolower(url_title(convert_accented_characters($post_title))).'-'.$last_id_inserted;
    echo $slug;
    //outputting my-slug-would-be-123



//insert the new post with slug

ดังนั้น ID จะไม่ซ้ำกันและทากด้วย

person itsme    schedule 31.07.2013

ฉันคิดว่าคุณต้องการอะไรแบบนั้น:

//Database loaded
//Text helper loaded

function post_uniq_slug($slug, $separator='-', $increment_number_at_end=FALSE) {    
    //check if the last char is a number
    //that could break this script if we don't handle it
    $last_char_is_number = is_numeric($slug[strlen($slug)-1]);
    //add a point to this slug if needed to prevent number collision..
    $slug = $slug. ($last_char_is_number && $increment_number_at_end? '.':'');

    //if slug exists already, increment it
    $i=0;
    $limit = 20; //for security reason
    while( get_instance()->db->where('slug', $slug)->count_all_results('posts') != 0) {
        //increment the slug
        $slug = increment_string($slug, $separator);

        if($i > $limit) {
            //break;
            return FALSE;
        }

        $i++;
    }

    //so now we have unique slug
    //remove the dot create because number collision
    if($last_char_is_number && $increment_number_at_end) $slug = str_replace('.','', $slug);

    return $slug;
}

ตัวอย่าง:

post_uniq_slug('sample'); //"sample" exists
//sample-1

post_uniq_slug('sample-2013'); //"sample-2013" exists
//sample-2013-2

post_uniq_slug('sample-2013', '-', TRUE); //increment "sample-2013"
//sample-2014

*ไม่ได้ทดสอบ

person Aurel    schedule 31.07.2013