简体   繁体   English

如何停止 PHP 循环以重复并覆盖旧数据?

[英]How can I stop PHP the while loop to repeat and cover old data?

I am new to PHP, so I want the while loop the result every times is different because I fetch data from CSV file, so it should not be all the data is same.我是 PHP 的新手,所以我希望 while 循环每次的结果都不同,因为我从 CSV 文件中获取数据,所以它不应该是所有数据都是相同的。

This is my code这是我的代码

<div style="margin: 1%">


            <?php
            //include 'barcode128.php';
            require 'vendor/autoload.php';

                if (($csvfile = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE) {
                    while (($csvdata = fgetcsv($csvfile, 1000, ",")) !== FALSE) {

                        $colcount = count($csvdata);

                        if($colcount!=5) {
                            $error = 'Column count incorrect';
                        } else {

                            /*$product = $csvdata[0];
                            $product_id = $csvdata[1];
                            $rate = $csvdata[2];
                            $description = $csvdata[3];
                            $image = $csvdata[4];
                            $imageData = base64_encode(file_get_contents($image));

                                echo '<div class="wrapper">';
                                echo '<div class="a"><img src="data:image;base64,'.$imageData.'" width="50"/>
                                      <div><b>Item: '.$product.'</b></div>
                                      <div><svg id="barcode"><script>JsBarcode("#barcode", "'.$product_id.'",{
                                      format: "code128",width: 1,height: 35,fontOptions: "bold",marginRight:30});</script></svg></div><span><b>Price: '.$rate.' </b></span><div><span><b>Desc: </b>'.$description.'</span></div></p></span>
                                      </div></div>';
                            */

                            $imageData = base64_encode(file_get_contents($csvdata[4]));

                                echo '<div class="wrapper">';
                                echo '<div class="a"><img src="data:image;base64,'.$imageData.'" width="50"/>
                                      <div><b>Item: '.$csvdata[0].'</b></div>
                                      <div><svg id="barcode"><script>JsBarcode("#barcode", "'.$csvdata[1].'",{
                                      format: "code128",width: 1,height: 35,fontOptions: "bold",marginRight:30,font:"arial"});</script></svg></div><span><b>Price: '.$csvdata[2].' </b></span><div><span><b>Desc: </b>'.$csvdata[3].'</span></div></p></span>
                                      </div></div>';

                        }

                    }
                    fclose($csvfile);
                }
            ?>

    </div>

The result of the current code: image-result当前代码的结果: image-result


The CSV file: image-csv CSV 文件: image-csv


This is what I want the result: enter image description here这就是我想要的结果:在此处输入图像描述

So, how can I make the result can loop every times and break it, then loop the next data from the CSV file?那么,我怎样才能让结果每次循环并打破它,然后循环 CSV 文件中的下一个数据? Thank you谢谢

The problem is that you are creating multiple svg elements with the same id.问题是您正在创建多个具有相同 ID 的svg元素。 You are then using this single id in your JsBarcode() call.然后,您将在JsBarcode()调用中使用这个单一 ID。 By doing this, the Javascript will always act on the first element with this id.通过这样做,Javascript 将始终作用于具有此 id 的第一个元素。

To remedy this, we must create elements with unique ids, and then use this unique id in each Javascript call.为了解决这个问题,我们必须创建具有唯一 ID 的元素,然后在每个 Javascript 调用中使用这个唯一 ID。

Since you have $product_id = $csvdata[1];因为你有$product_id = $csvdata[1]; in your commented-out code, this answer is going assume that $csvdata[1] is unique per row.在您注释掉的代码中,此答案将假定$csvdata[1]每行都是唯一的。

Inside of your echo , change在你的echo里面,改变

<div><svg id="barcode"><script>JsBarcode("#barcode", "'.$csvdata[1].'",{

to

<div><svg id="barcode_'.$csvdata[1].'"><script>JsBarcode("#barcode_'.$csvdata[1].'", "'.$csvdata[1].'",{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM