doumixiang2227 2011-10-26 11:37
浏览 37
已采纳

如何动态重命名我的数组键,以便该键可以用作表中的主键?

I'm creating the array dynamically using this tool.

http://174.121.67.116/~achadire/aware5/product_options.php?cid=6

The array output looks like this.

$data = array(
  0 => array(
    0 => 300,
    3 => 186,
  ),
  1 => array(
    0 => 341,
    3 => 186,
  ),
  2 => array(
    0 => 257,
    3 => 186,
  ),
  3 => array(
    0 => 300,
    3 => 360,
  ),
  4 => array(
    0 => 300,
    3 => 187,
  ),
  5 => array(
    0 => 341,
    3 => 360,
  ),
  6 => array(
    0 => 341,
    3 => 187,
  ),
  7 => array(
    0 => 257,
    3 => 360,
  ),
  8 => array(
    0 => 257,
    3 => 187,
  ),
);

0 => Array is the product. [0]=>300, [2]=>186 represent a unique product configuration for the given array key. (i.e blue, big.) (each vale is a id for an attribute.)

So I want to upload the results to products table in MySQL. Here is my problem:

First, how do I make my array key a unique 7 digit product code. In other words, how to I reindex my array with a unique 7 digit code so that I never use the same key for an array twice. I assume this means figuring out the next value in the mysql database and then looping through the array with i++ before pushing to the table.

Second, the same configuration should only be applied to one product. So if the array key combination already exists it should skip to the next array.

In short how do I turn my array key into a unique 7 digit product code? Also, when I upload my array how to I skip over values combinations that already exists in the database so I don't have two different product codes with the same configuration.

  • 写回答

2条回答 默认 最新

  • duanmie9682 2011-10-29 17:27
    关注

    What I ended up doing was inserting a record into my skus table and returning the primary key to an array. The I reidex my my array with the new key values and load it into my skus_options table. Here is the code. Hope it helps someone down the road.

    // inserts records into skus table and creates an array of skuids.
    for ($i = 0; $i < $count; $i++) {
    $sql = "INSERT INTO skus (idcodes, dateadded) VALUES ('$cid', '$dateadded')";
    $result = mysql_query($sql, $dbc) or die(mysql_error());
    $sid = mysql_insert_id();
    array_push($skuids, $sid);
    }
    // creates an array using skuid as the array key and inserts the array into the          skus_options table.
    $skus = array_combine($skuids, array_values($skus));
    foreach ($skus as $key => $options) {
    foreach ($options as $option => $value) {
    $query = "INSERT INTO skus_options (skus_options.idskus, skus_options.valueid,   skus_options.idcodes, skus_options.dateadded) VALUES ('$key', '$value', '$cid','$dateadded')";
         $result = mysql_query($query, $dbc) or die(mysql_error());
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题