I have a CSV file with 3 columns and 223 rows. The columns go from items (A column), description (B column), to type (C column). Each row contains information about each item. I want to insert data from all this spreadsheet into my HTML file. Along with this, I want to save time by not copy and pasting 233 sets of information manually.
Originally the information I needed was on a website, however, I found a suggestion to use a webscraper to get all the information I needed. I did this by using python and now I have all the information in the spreadsheet.
The template I would like to follow is shown (using spreadsheet terms). If I were manually doing this the next line of code would look this but with a B instead of an A (A1->B1)
<p class="item-title">(A1 in spreadsheet)</p>
<p class="w-itemid">ItemID: N/A</p>
<p>• (A2 in spreadsheet)</p>
<ul>
<p>Type: (A3 in spreadsheet</p>
<p>Item Pool: N/A</p>
</ul>
I want the solution to be able to link the information from my spreadsheet into my HTML without spending an immense amount of time copy and pasting 233 items. I do not mind using Javascript, jQuery, or PHP as long as it helps me finish this task.
you can try this solution:
$row = 1;
$columnArray = [];
$resultArray = [];
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
if($row == 1){
$columnArray = $data;
}else{
$tmpArray = [];
for($i=0;$i<count($columnArray);$i++){
$tmpArray[$columnArray[$i]] = $data[$i];
}
$resultArray[] = $tmpArray;
}
$row++;
}
fclose($handle);
}
I'm assuming that the first row of your CSV holds the column names. At the end you will have $resultArray
associative array holding all your data.
UPDATE
If your CSV does not contain field names at the first row you can use this code:
$row = 1;
$resultArray = [];
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$resultArray[] = $data;
$row++;
}
fclose($handle);
}
In this case $resultArray
will hold the data as a normal nested array and you can show the info by iterating trough it:
if(count($resultArray)>0){
for($i=0;$i<count($resultArray);$i++){
echo '<p>Item: '.$resultArray[$i][0].'</p>';
echo '<p>Description: '.$resultArray[$i][1].'</p>';
echo '<p>Type: '.$resultArray[$i][2].'</p>';
}
}