Home php PHP work with CSV files, you need to withdraw all products with...

PHP work with CSV files, you need to withdraw all products with the right name of categories

Author

Date

Category

Good time of day.
Input

group.csv:

ID; name;
1; group 1;
2; group 2;
3; group 3;
4; Group 4;

Products.csv:

id; category; name; price;
1; 1; commodity1; 10
2; 2; commodity2; 20
3; 3; product3; 30
4; 4; product4; 40

I connected them ran through the cycle

& lt;? php
$ file1 = fopen ("groups.csv", "r");
$ file2 = fopen ("Products.csv", "R");
$ File = Array ($ File1, $ File2);
  Foreach ($ Files AS $ File) {
  While (($ Line = Fgets ($ File))! == False) {
  List ($ Index, $ Value) = Explode (';', Trim ($ Line));
  Echo $ Line;
  echo '& lt; hr & gt; & lt; br & gt;';
  }
}
? & gt;

I can not understand how to make me so that the product substituted the product and the price.


Answer 1, Authority 100%

If I understand correctly, you need to withdraw all products with the correct name of categories.
If so, then:

& lt;? php
// For a start forming an array of categories.
$ filegroups = fopen ("groups.csv", "r");
$ groups = [];
While (($ Line = Fgets ($ filegroups))! == False) {
  $ Data = Explode (';', Trim ($ Line));
  if (! (int) $ Data [0]) Continue; // This condition helps skip the header from the file.
  $ Groups [$ Data [0]] = $ Data [1];
}
FClose ($ filegroups);
// TERRER IN $ GROUPS We have all categories from the file
// Headers from files We ignore and remove your own.
echo 'id; category; product; price of goods;';
echo '& lt; hr & gt; & lt; br & gt;';
// start receiving goods from the file and display them
$ fileProducts = Fopen ("Products.csv", "R");
While (($ Line = Fgets ($ fileproducts))! == FALSE) {
  $ Data = Explode (';', Trim ($ Line));
  if (! (int) $ Data [0]) Continue; // This condition helps skip the header from the file.
  // We form the data array desired to display
  $ Print = [
    $ DATA [0],
    Isset ($ Groups [$ Data [1]])? $ Groups [$ Data [1]]: 'Unknown Category', // We substitute the category if it is.
    $ DATA [2],
    $ DATA [3],
  ];
  // Display in the format we need
  Echo implode (';', $ Print);
  echo '& lt; hr & gt; & lt; br & gt;';
}
FClose ($ fileproducts);

appropriately I left the same as you had. To form a beautiful table, you can use the answer from Bloom


Answer 2, Authority 100%

& lt;? php
$ file1 = fopen ("groups.csv", "r");
$ file2 = fopen ("Products.csv", "R");
$ File = Array ($ File1, $ File2);
  Foreach ($ Files AS $ File) {
  While (($ Line = Fgets ($ File))! == False) {
  $ Value [] = Explode (';', Trim ($ Line));
 }
}
For ($ i = 0; $ i & lt; Count ($ value); $ i ++) {
 Echo '& lt; Table CellSpacing = "0" Border = "2" style = "table-layout: fixed; width: 200px" & gt;';
 Echo '& lt; TR & GT;';
 echo '& lt; TD style = "width: 200px; overflow: hidden; text-overflow: ellipsis; White-space: nowrap" & gt;' $ Value [$ i] [0]. '& lt; TD style = "Width : 200px; "& gt; 'iconv (' CP1251 ',' UTF-8 ', $ Value [$ i] [1]).' & Lt; TD style =" width: 100px; "& gt;" iconv (' CP1251 ',' UTF-8 ', $ Value [$ i] [2]).' & Lt; td style = "width: 200px;" & gt; "iconv ('CP1251', 'UTF-8', $ Value [$ i] [3]). '& lt; td style = "width: 200px;" & gt; "iconv (' CP1251 ',' UTF-8 ', $ Value [$ i] [4]).' & lt ; / td & gt; ';
Echo '& lt; / tr & gt;';
Echo '& lt; / Table & gt;';
}
? & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions