Question

In: Computer Science

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>

<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>

Solutions

Expert Solution

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


Related Solutions

How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
* 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...
take the following html code and make it work for html validator. heres the ,html file...
take the following html code and make it work for html validator. heres the ,html file <!DOCTYPE html> <html lang="en">    <head>        <title> GettingStarted</title>        <meta charset="utf-8">        <link href="Style.css" rel="stylesheet">    </head>       <body>        <header><h1>GettingStarted</h1></header>        <nav>               <b>        <a href="Home.html">Home</a>&nbsp;        <a href="GettingStarted.html">Getting Started</a>&nbsp;        <a href="MaterialsNeeded.html">Materials Needed</a>&nbsp;                      <a href="TroubleShooting.html">TroubleShooting</a>&nbsp;        <a href="InfoMaterials.html">Infomation on materials</a>&nbsp;   ...
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 -...
How would I structure the following PHP (PDO) code into a table format using only PHP?...
How would I structure the following PHP (PDO) code into a table format using only PHP? //Our SQL statement, which will select a list of tables from the current MySQL database. $sql = "SELECT * FROM jobs"; //Prepare our SQL statement, $statement = $pdo->prepare($sql); //Execute the statement. $statement->execute(); //Fetch the rows from our statement. $tables = $statement->fetchAll(PDO::FETCH_NUM); //Loop through our table names. foreach($tables as $table){ //Print the table name out onto the page. echo $table[0], ' '; echo $table[1], '...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT