2016-11-25 69 views
0

我的csv文件有3列時間,事件和位置。我將每列加載到一個單獨的數組中。使用for循環,我將數組顯示爲一個html表格。但是,它並沒有顯示出來。爲什麼?爲什麼我的桌子不出現?

EventsScheduleFriday.php

<?php 
echo "<link rel='stylesheet' type='text/css' href='../styles/styles.css' />"; 

$time = array(); 
$events = array(); 
$location = array(); 

function get_data(&$time, &$events, &$location) { //references variable declared above 
    $file = fopen(__DIR__."/../data/EventsScheduleFriday.csv", "r"); 
    while(!feof($file)) { //while end of file has not been reached 
     $content = fgetcsv($file, ","); //converts first line of csv to an array 
     array_push($time, $content[0]); 
     array_push($events, $content[1]); 
     array_push($location, $content[2]); 
    } 
    fclose($file); //closes csv file 
} 

// put the data on the screen in readable form 
function display_table(&$time, &$events, &$location) { 
    echo "<table class='tg'>"; 
    for($i = 0; $i < count($time); $i++) { 
     echo "<tr>\n"; 
     if ($i == 0){ //create table header cell 
      echo "<th>"; 
      $time[$i]; 
      echo "</th>\n"; 
      echo "<th>"; 
      $events[$i]; 
      echo "</th>\n"; 
     } 
     else { 
      echo "<td class='cell-time'>"; 
      $time[$i]; 
      echo "</td>\n"; 
      echo "<td class='cell-descript'>"; 
      $events[$i]; 
      echo "<br class = 'space'>"; 
      echo "<div class = 'table_description'>"; 
      echo "Location: " + $location[$i]; 
      echo "</div></td>\n"; 
     } 
     echo "</tr>\n"; 
    } 
    echo "\n</table>"; 
} 

get_data($time, $events, $location); 
display_table($time, $events, $location); 


?> 

EventsScheduleFriday.csv

Time,Event,Location, 
12:30pm,Hilby The Skinny German Juggle Boy,West State Street, 
4:45pm,Hilby The Skinny German Juggle Boy,West State Street, 
6pm,Finger Lakes Comedy Festival Competition 1st Round (Age 21+),Lot 10, 
8pm,Stand-up Comedy Show,Acting Out NY, 
10pm,All-Star Comedy Show,Acting Out NY 

events.php

<div class = "schedule"> 
       <?php include "scripts/EventsScheduleFriday.php" ?> 
</div> 

回答

1

您必須將您的變量連接到輸出字符串。

您有:

echo "<th>"; 
$time[$i]; 

您需要:

echo "<th>" . $time[$i]; 

當您連接,您使用.操作。不是+。後來你想:

echo "Location: " + $location[$i];

它應該是:

echo "Location: " . $location[$i];

閱讀上此位置:http://php.net/manual/en/language.operators.string.php