PHP - 循环操作csv中的数据 [英] PHP - Manipulating data from csv in a loop

查看:135
本文介绍了PHP - 循环操作csv中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,我需要访问包含我的csv格式数据的.dat文件,然后使用它创建虚构的电子商务网站的产品页面。我做了这样的事情,让我有正确数量的图像/信息显示出来,但不能操纵数组,以便我可以呈现它的具体部分。



我的.dat文件看起来像这样:

  1001,Logitech G703,无线游戏鼠标,$ 99.99,mouse.jpg 
1002,微软键盘,蓝牙机械键盘,$ 59.99,keyboard.jpg
1003,Bose音箱,Teal无线环绕音箱,$ 159.99,speaker.jpg
1004,KickSTART,葡萄风味山露KickSTART,$ 1.49,kickstart.jpg
1005,Google Pixel,Google第一款真正的智能手机,$ 799.99,pixel.jpg
1006,佳能EOS REBEL,2420万像素,$ 849.00,canon.jpg
1007,Surface Pro 4,128GB 4GB RAM Intel Core i5 => $ 698.99,surface.jpg
1008,Luna,西伯利亚雪橇犬女性,$ 999999.99,luna.jpg
1009,Loki,西伯利亚雪橇犬男性,$ 999999.99,loki.jpg
1010,咖啡杯,这咖啡让我很棒,$ 5.00,mug.jpg

它看起来像这样:

 <?php 

$ data = file('products.dat');


foreach($ data as $ products){
$ i ++;
$ dataExplode = explode(,,$ products [$ i]);
?>
< div class =product>
< image class =product_imagesrc =img / loki.jpg> <?php print $ products; ?>
< br> $ 20.99
< form method =postaction =viewcart.php?action = add& code =<?php echo
$ product_array [$ key] [ code];?>>
< div>< input type =textname =quantityvalue =1size =2/>< input
type =submitvalue =Add to cartclass =btnAddAction/>< / div>

< / div>
<?php}; ?>

在我的foreach循环中使用print产品时,它会按照顺序显示产品信息,例如I但是我不能缩小它,并使用逗号分隔的字符串。此外,我需要能够显示与每个产品相关的图像,但一直没能找到一个工作方法。

我感谢帮助和建议!

解决方案

您不需要在产品数组中指定$ i,你需要按行 \\\
然后以逗号分隔每行(产品) 。

当你拿一行产品并将其分解时,它就成了一个数组。

  $ data = file('products.dat'); 
$ b foreach($ data as $ line){
$ product = explode(,,$ line);
$ id = $ product [0];
$ title = $ product [1];
$ price = $ product [2];
$ description = $ product [3];
$ image = $ product [4];
}

希望这有助于您。


I am working on a project where I need to access a .dat file containing my data in a csv format and then use that to create a products page for an imaginary e-commerce site. I have made it so that I have the correct amount of images/information showing up but cannot manipulate the arrays so that I can present specific pieces of it.

My .dat file looks like this:

1001,Logitech G703,Wireless gaming mouse,$99.99,mouse.jpg
1002,Microsoft Keyboard,Bluetooth mechanical keyboard,$59.99,keyboard.jpg
1003,Bose Speaker,Teal wireless surround sound speaker,$159.99,speaker.jpg
1004,KickSTART,Grape flavored Mountain Dew KickSTART,$1.49,kickstart.jpg
1005,Google Pixel,Google's first real smartphone,$799.99,pixel.jpg
1006,Canon EOS REBEL,24.2 Megapixel,$849.00,canon.jpg
1007,Surface Pro 4,128GB 4GB RAM Intel Core i5=>$698.99,surface.jpg
1008,Luna,Siberian Husky Female,$999999.99,luna.jpg
1009,Loki,Siberian Husky Male,$999999.99,loki.jpg
1010,Coffee Mug,"This coffee is making me awesome!",$5.00,mug.jpg

And the rest of it looks like this:

<?php

$data = file('products.dat');


foreach ($data as $products){        
    $i++;
    $dataExplode = explode(",", $products[$i]);
?>
    <div class="product">
    <image class="product_image" src="img/loki.jpg"> <?php print $products; ?>
    <br>$20.99
    <form method="post" action="viewcart.php?action=add&code=<?php echo 
    $product_array[$key]["code"]; ?>">
    <div><input type="text" name="quantity" value="1" size="2" /><input 
    type="submit" value="Add to cart" class="btnAddAction" /></div>

    </div>
<?php }; ?>

When using print on $products within my foreach loop it will display the product information in sequential order like I want but I cannot narrow it down and use the strings that are separated by commas. Furthermore, I need to be able to display the images associated with each product, but have not been able to find a working method for this either.

I appreciate the help and advice!

解决方案

You don't need to specify the $i inside the products array, that points to each column without taking lines into account.

You need to explode by lines \n and then by comma for each line (product).

When you take a line of products and explode it, it becomes an array.

$data= file('products.dat');

foreach ($data as $line){
    $product = explode(",",$line);
    $id = $product[0];
    $title = $product[1];
    $price = $product[2];
    $description = $product[3];
    $image = $product[4];
}

Hope this helps.

这篇关于PHP - 循环操作csv中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆