Codeigniter:如果有多个项目,如何对主机URL进行分组并将其保存到数据库?

Codeigniter:如果有多个项目,如何对主机URL进行分组并将其保存到数据库?

问题描述:

I am asking for following details from user in form:

  • Product name:
  • product Price:
  • Product URL:

The user can add multiple items in cart. During checkout, I want to filter URL like if user enter below multiple URL in the form and these multiple url's will be inserted in products table.

  • aliexpress.com/product/phone_cover
  • aliexpress.com/product/phone_battery
  • nike.com/cat/shoes

In this case I want to get host (Domain) of URL like:

  • aliexpress.com
  • nike.com

and add once in the database store table. For example, user added 2 URLs from aliexpress and one for nike so I want to store one time aliexpress and one time nike in store_url column of store table in database. I am getting host URL by the code below:

foreach ($data as $show) 
{
    $var= parse_url($show->produst_url);                                                                                                
    echo $var['host'];
}

below are database tables

--------        -------
products         store
--------        -------
id              id
product name    products_id
product url     store url
product price

Any help is appreciated. I have tried my best to explain you my problem but if you still need some more detail I will be happy to explain you more. Thanks

Well there are many ways to do this and this is just one of them...

Your primary question is - how do you save unique entries?

This is just some sample code, based upon what you provided with debug so I could see how it was working as I developed it. There is NO Database "stuff" as you haven't provided any information regarding it, so it's just a starter to get you going.

// Create some dummy test data as per the provided code.
$data[] = (object)array('produst_url' => 'http://aliexpress.com/product/phone_cover');
$data[] = (object)array('produst_url' => 'http://aliexpress.com/product/phone_battery');
$data[] = (object)array('produst_url' => 'http://nike.com/cat/shoes');

var_dump($data); // Check the test array

$host_array = array(); // Init the array to store the host values

foreach ($data as $show) {
    $var = parse_url($show->produst_url);
    var_dump($var); // Check the $var array

    $host = $var['host'];

    // Save the host name in the host_array ONLY if it hasn't already been saved.
    if ( ! in_array($host, $host_array)) {
        $host_array[] = $host;
    }
    echo $host; // show the host name
}

// Check the final $host_array

var_dump($host_array); // What does the final answer look like?

The final var_dump ,var_dump($host_array), reveals...

array (size=2)
  0 => string 'aliexpress.com' (length=14)
  1 => string 'nike.com' (length=8)

Not sure if produst should be product but I kept it as you had it. At least it is consistent.

So now you have an array of unique host names. Next you have to save them into the database IF they are not already there, to prevent duplicates.

As you have not provided any insight into your database/table setup,that part should be another question if you are unsure on how to do that.