【问题标题】:Is my for loop not looping though all data?我的 for 循环不是循环所有数据吗?
【发布时间】:2020-02-17 09:14:46
【问题描述】:

我正在尝试使用 for 循环填充数据库。提交表单时,我收到以下数据:

"name" => "This is a product title"
  "slug" => "this-is-a-product-title"
  "category_id" => "1"
  "brand_id" => "1"
  "description" => "<p>This is a product description</p>"
  "video" => "https://www.youtube.com/watch?v=kY1F_Y0GniQ"
  "imageColor" => array:2 [▼
    0 => "1"
    1 => "2"
  ]
  "image" => array:2 [▼
    0 => "["http://ecom.local/uploads/product-1.jpg","http://ecom.local/uploads/product-2.jpg"]"
    1 => "["http://ecom.local/uploads/product-4.jpg","http://ecom.local/uploads/product-3.jpg"]"
  ]
  "color" => array:2 [▼
    0 => "1"
    1 => "2"
  ]
  "size" => array:2 [▼
    0 => "11"
    1 => "9"
  ]
  "stock" => "2"
  "price" => array:2 [▼
    0 => "4000"
    1 => "3000"
  ]
  "discount" => array:2 [▼
    0 => "0"
    1 => "0"
  ]
  "status" => "active"

为了填充数据,我编写了以下代码:

public function store(Request $request)
{ 
    $rules = $this->product->getRules();
    $request->validate($rules);
    $data = $request->all();
    $data['name'] = $request->name;
    $data['slug'] = $request->slug;
    $data['category_id'] = $request->category_id;
    $data['brand_id'] = $request->brand_id;
    $data['video'] = $request->video;
    $data['status'] = $request->status; 
    $this->product->fill($data);
    $status = $this->product->save();
    $product_id = $this->product->id;
    if($status){
        if(!empty($request->image)){
            for($i = 0; $i < count($request->image);){
                    $images = $request->image[$i];
                    $images = str_replace('"', '', $images);
                    $images = str_replace(array('[',']'),'',$images);
                    $images = explode(',', $images);
                for($j = 0; $j < count($images[$i]);){
                    $image_data['product_id'] = $product_id;
                    $image_data['color_id'] = $request->imageColor[$i];
                    $image_data['image'] = $images[$j];
                    $this->product_image->fill($image_data);
                    $this->product_image->save();
                    $j++;
                }
                $i++;
            }
        }
        if(!empty($request->size)){
            for($i = 0; $i < count($request->size); $i++){
                $size_data['product_id'] = $product_id;
                $size_data['size_id'] = $request->size[$i];
                $this->product_size->fill($size_data);
                $this->product_size->save();
            }
        }
        $request->session()->flash('success','Product created successfully.');
    } else {
        $request->session()->flash('error','Problem while adding product.');
    }
    return redirect()->route('products.index');
}

但是,每次我插入一个产品时,我只会在 product_images 和 product_sizes 表中填充单行。我的 for 循环不起作用还是我的代码中有错误? 现在我用我的代码得到这个结果: 我的预期答案在产品图片表中如下所示

【问题讨论】:

  • 所以您的图像数据是一个数组,由两个条目组成 - 每个条目又包含 两个 图像 URL,但不知何故以数组为字符串的格式? (为什么?)
  • 也许,您想说为什么我要以数组形式获取数据。如果是,我正在使用响应式文件管理器,并且在使用文件管理器选择多个图像时,我们会得到这样的数据。

标签: php loops for-loop


【解决方案1】:

您必须像下面的示例一样创建新图像:

[...]
$product_image = new Image;
$image_data['product_id'] = $product_id;
$image_data['color_id'] = $request->imageColor[$i];
$image_data['image'] = $images[$j];
$product_image->fill($image_data);
$product_image->product_id = $this->id;
$product_image->save();
[...]

否则只会创建一个。

我认为 for 循环也是错误的:

for($j = 0; $j < count($images[$i]);){

改成:

for($j = 0; $j < count($images);){

【讨论】:

  • 你好。我在一开始就定义了我的对象。
  • 而且,我认为这不是问题,因为上面的代码正在将数据插入到相应的表中。问题是所有数据都没有被填充。
  • 您能举例说明您的预期结果和实际结果吗?
  • 您的代码四次覆盖该条目。所以我仍然相信你必须像上面的代码一样初始化新的图像。你试过了吗?
  • 尽管感觉不像是对象问题,但实际上是对象和循环也可以按照您在答案中的建议工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 2014-04-29
相关资源
最近更新 更多