Question

In: Computer Science

how to read a csv file in php not using fgetscsv or fgets. Just using file("Stores.csv");...

how to read a csv file in php not using fgetscsv or fgets. Just using file("Stores.csv"); or fopen("Stores.csv", "r"); and making into a html table? It print but it keeps saying Notice: Undefined offset: , Notice: Undefined offset: 2, etc at the top.

Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie

142,160,28,10,5,3,60,0.28,3167

175,180,18,8,4,1,12,0.43,4033

129,132,13,6,3,1,41,0.33,1471

138,140,17,7,3,1,22,0.46,3204

232,240,25,8,4,3,5,2.05,3613

135,140,18,7,4,3,9,0.57,3028

150,160,20,8,4,3,18,4.00,3131

207,225,22,8,4,2,16,2.22,5158

271,285,30,10,5,2,30,0.53,5702

89,90,10,5,3,1,43,0.30,2054

153,157,22,8,3,3,18,0.38,4127

87,90,16,7,3,1,50,0.65,1445

234,238,25,8,4,2,2,1.61,2087

106,116,20,8,4,1,13,0.22,2818

175,180,22,8,4,2,15,2.06,3917

165,170,17,8,4,2,33,0.46,2220

166,170,23,9,4,2,37,0.27,3498

136,140,19,7,3,1,22,0.63,3607

148,160,17,7,3,2,13,0.36,3648

151,153,19,8,4,2,24,0.34,3561

180,190,24,9,4,2,10,1.55,4681

293,305,26,8,4,3,6,0.46,7088

167,170,20,9,4,2,46,0.46,3482

190,193,22,9,5,2,37,0.48,3920

184,190,21,9,5,2,27,1.30,4162

157,165,20,8,4,2,7,0.30,3785

110,115,16,8,4,1,26,0.29,3103

135,145,18,7,4,1,35,0.43,3363

567,625,64,11,4,4,4,0.85,12192

180,185,20,8,4,2,11,1.00,3831

183,188,17,7,3,2,16,3.00,3564

185,193,20,9,3,2,56,6.49,3765

152,155,17,8,4,1,33,0.70,3361

148,153,13,6,3,2,22,0.39,3950

152,159,15,7,3,1,25,0.59,3055

146,150,16,7,3,1,31,0.36,2950

170,190,24,10,3,2,33,0.57,3346

127,130,20,8,4,1,65,0.40,3334

265,270,36,10,6,3,33,1.20,5853

157,163,18,8,4,2,12,1.13,3982

128,135,17,9,4,1,25,0.52,3374

110,120,15,8,4,2,11,0.59,3119

123,130,18,8,4,2,43,0.39,3268

212,230,39,12,5,3,202,4.29,3648

145,145,18,8,4,2,44,0.22,2783

129,135,10,6,3,1,15,1.00,2438

143,145,21,7,4,2,10,1.20,3529

247,252,29,9,4,2,4,1.25,4626

111,120,15,8,3,1,97,1.11,3205

133,145,26,7,3,1,42,0.36,3059

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<!-- title for web page -->

<title>Stores</title>

<link rel="stylesheet" href="style.css">

</head>

<body>

<h1>Stores</h1>

<table>

<?php

$Stores = file("homes.csv");

foreach($Stores as $key => $data) {

$Stores[$key] = explode(",", $data);

}

foreach($Stores as $key => $data)

print "".$Homes[$key][0]. "";

print "".$Homes[$key][1]. "";

print "".$Homes[$key][2]. "";

print "".$Homes[$key][3]. "";

print "".$Homes[$key][4]. "";

print "".$Homes[$key][5]. "";

print "".$Homes[$key][6]. "";

print "".$Homes[$key][7]. "";

print "".$Homes[$key][8]. "";

print "";

}

print "";

?>

Solutions

Expert Solution

There are multiple errors in this code

1. HTML tags are incomplete. THere are no closing tags for table, body and html.

2. Homes variable is undefined.

Please find complete working code PHP below

******************************************************************************************

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Stores</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Stores</h1>
<table>
<tr>
<th>Acme</th>
<th>Walmart</th>
<th>Ross</th>
<th>BJs</th>
<th>Target</th>
<th>Marshalls</th>
<th>Foot Locker</th>
<th>Giant</th>
<th>Charming Charlie</th>
</tr>
<?php

//Iterating on each line
$Stores = file("stores.csv");
foreach ($Stores as $line) {
  

//Explode function takes
   //Parameter1 as the seperator value here it is comma ","
   //Parameter2 is the string that need to be seperated
   list($Acme, $Walmart, $Ross, $BJs, $Target, $Marshalls, $FootLocker, $Giant, $CharmingCharlie) = explode(",", $line);

    //Making each row in html format
   echo "<tr>";
   echo "<td>".$Acme."</td>";
   echo "<td>".$Walmart."</td>";
   echo "<td>".$Ross."</td>";
   echo "<td>".$BJs."</td>";
   echo "<td>".$Target."</td>";
   echo "<td>".$Marshalls."</td>";
   echo "<td>".$FootLocker."</td>";
   echo "<td>".$Giant."</td>";
   echo "<td>".$CharmingCharlie."</td>";
   echo "</tr>";

}

?>
</table>
</body>
</html>

******************************************************************************************

INPUT

142,160,28,10,5,3,60,0.28,3167
175,180,18,8,4,1,12,0.43,4033
129,132,13,6,3,1,41,0.33,1471
138,140,17,7,3,1,22,0.46,3204
232,240,25,8,4,3,5,2.05,3613
135,140,18,7,4,3,9,0.57,3028
150,160,20,8,4,3,18,4.00,3131
207,225,22,8,4,2,16,2.22,5158
271,285,30,10,5,2,30,0.53,5702
89,90,10,5,3,1,43,0.30,2054
153,157,22,8,3,3,18,0.38,4127
87,90,16,7,3,1,50,0.65,1445
234,238,25,8,4,2,2,1.61,2087
106,116,20,8,4,1,13,0.22,2818
175,180,22,8,4,2,15,2.06,3917
165,170,17,8,4,2,33,0.46,2220
166,170,23,9,4,2,37,0.27,3498
136,140,19,7,3,1,22,0.63,3607
148,160,17,7,3,2,13,0.36,3648
151,153,19,8,4,2,24,0.34,3561
180,190,24,9,4,2,10,1.55,4681
293,305,26,8,4,3,6,0.46,7088
167,170,20,9,4,2,46,0.46,3482
190,193,22,9,5,2,37,0.48,3920
184,190,21,9,5,2,27,1.30,4162
157,165,20,8,4,2,7,0.30,3785
110,115,16,8,4,1,26,0.29,3103
135,145,18,7,4,1,35,0.43,3363
567,625,64,11,4,4,4,0.85,12192
180,185,20,8,4,2,11,1.00,3831
183,188,17,7,3,2,16,3.00,3564
185,193,20,9,3,2,56,6.49,3765
152,155,17,8,4,1,33,0.70,3361
148,153,13,6,3,2,22,0.39,3950
152,159,15,7,3,1,25,0.59,3055
146,150,16,7,3,1,31,0.36,2950
170,190,24,10,3,2,33,0.57,3346
127,130,20,8,4,1,65,0.40,3334
265,270,36,10,6,3,33,1.20,5853
157,163,18,8,4,2,12,1.13,3982
128,135,17,9,4,1,25,0.52,3374
110,120,15,8,4,2,11,0.59,3119
123,130,18,8,4,2,43,0.39,3268
212,230,39,12,5,3,202,4.29,3648
145,145,18,8,4,2,44,0.22,2783
129,135,10,6,3,1,15,1.00,2438
143,145,21,7,4,2,10,1.20,3529
247,252,29,9,4,2,4,1.25,4626
111,120,15,8,3,1,97,1.11,3205
133,145,26,7,3,1,42,0.36,3059

From Output it is evident that are no such errors mentioned in the question

Output

NOTE: Keep input file as it is. No extra lines


Related Solutions

how to read a csv file in php and make a html table? I can't use...
how to read a csv file in php and make a html table? I can't use the PHP function fgetcsv. I can use explode. I can't put a php inside a php. Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie 142,160,28,10,5,3,60,0.28,3167 175,180,18,8,4,1,12,0.43,4033 129,132,13,6,3,1,41,0.33,1471 138,140,17,7,3,1,22,0.46,3204 232,240,25,8,4,3,5,2.05,3613 135,140,18,7,4,3,9,0.57,3028 150,160,20,8,4,3,18,4.00,3131 207,225,22,8,4,2,16,2.22,5158 271,285,30,10,5,2,30,0.53,5702 89,90,10,5,3,1,43,0.30,2054 153,157,22,8,3,3,18,0.38,4127 87,90,16,7,3,1,50,0.65,1445 234,238,25,8,4,2,2,1.61,2087 106,116,20,8,4,1,13,0.22,2818 175,180,22,8,4,2,15,2.06,3917 165,170,17,8,4,2,33,0.46,2220 166,170,23,9,4,2,37,0.27,3498 136,140,19,7,3,1,22,0.63,3607 <!DOCTYPE html> <html> <head>    <meta charset="utf-8">    <title>Stores</title>    <link rel="stylesheet" href="style.css"> </head> <body> <h1>Stores</h1> <?php <table> <tr> <th>Acme</th> <th>Walmart</th> <th>Ross</th> <th>BJs</th> <th>Target</th> <th>Marshalls</th> <th>Foot Locker</th>...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file ( Use any for example, You can use small dataset) You need to use pandas library You need to use Flask Make search table like YouTube has.
how to read a csv file in php and make a html table? Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie...
how to read a csv file in php and make a html table? Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie 142,160,28,10,5,3,60,0.28,3167 175,180,18,8,4,1,12,0.43,4033 129,132,13,6,3,1,41,0.33,1471 138,140,17,7,3,1,22,0.46,3204 232,240,25,8,4,3,5,2.05,3613 135,140,18,7,4,3,9,0.57,3028 150,160,20,8,4,3,18,4.00,3131 207,225,22,8,4,2,16,2.22,5158 271,285,30,10,5,2,30,0.53,5702 89,90,10,5,3,1,43,0.30,2054 153,157,22,8,3,3,18,0.38,4127 87,90,16,7,3,1,50,0.65,1445 234,238,25,8,4,2,2,1.61,2087 106,116,20,8,4,1,13,0.22,2818 175,180,22,8,4,2,15,2.06,3917 165,170,17,8,4,2,33,0.46,2220 166,170,23,9,4,2,37,0.27,3498 136,140,19,7,3,1,22,0.63,3607 <!DOCTYPE html> <html> <head>    <meta charset="utf-8">    <title>Stores</title>    <link rel="stylesheet" href="style.css"> </head> <body> <h1>Stores</h1> <?php <table> <tr> <th>Acme</th> <th>Walmart</th> <th>Ross</th> <th>BJs</th> <th>Target</th> <th>Marshalls</th> <th>Foot Locker</th> <th>Giant</th> <th>Charming Charlie</th> </tr> $Stores = fopen("stores.csv", "r"); <tr> <td>$Acme</td> <td>$Walmart</td> <td>$Ross</td> <td>$BJs</td> <td>$Target</td> <td>$Marshalls</td> <td>$Foot Locker</td> <td>$Giant</td> <td>$Charming...
* readCsvFile() -- Read in a CSV File and return a list of entries in that...
* readCsvFile() -- Read in a CSV File and return a list of entries in that file.    * @param filePath -- Path to file being read in.    * @param classType -- Class of entries being read in.    * @return -- List of entries being returned.    */    public <T> List<T> readCsvFile(String filePath, Class<T> classType){        return null;    } implement this class. Return a list of T type. dont worry about CSV format. Just assume...
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
Goal: to write a Python program that will read a playlist from a CSV file and...
Goal: to write a Python program that will read a playlist from a CSV file and display it in the console in the form of a table. https://s3.eu-west-2.amazonaws.com/bb-python-modules/Coursework/CW3/playlist_text_question.html The following is a link to the question. It includes all instruction and a template file (with additional instructions) where the answer must be completed.
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Using the data from the csv file, answer the questions with rstudio # number_children - The...
Using the data from the csv file, answer the questions with rstudio # number_children - The number of children in the home # internet - Does the home have internet access? # mode - The way the household took the survey # own - Do the residents own with or without a mortgage or rent? # language - The primary language spoken in the home # decade_built - The decade the home was built 1) In how many households, wife’s...
What is a csv file and how does database work in general?
What is a csv file and how does database work in general?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT