In: Computer Science
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>
<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 Charlie</td>
</tr>
print "</table>";
?>
</body>
</html>
PHP CODE
<!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
$Stores = file("stores.csv");
foreach ($Stores as $line) {
//Using explode and comma as separator
assigning each line in diffenet variables
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>
stores.csv
'
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
Output
Please comment for any further assistance
NOTE: Keep the code and csv file in same directory