dt3674 2018-03-30 21:51
浏览 47
已采纳

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

  • 写回答

1条回答 默认 最新

  • doubao12345 2018-03-30 23:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大