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 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答