html lists - PHP echo to li table from input text file -
i have input user method post , place input text file. separated semicolon (;). , each new input makes new line in text file. want read input text file , echo out li in html. having problems echo right way.
here input php-code:
<?php if (isset($_post["submit"])) { $title = $_post['movietitle']; $rating = $_post['movieratings']; $plot = $_post['plot']; $handle = fopen('movies.txt', 'a'); $names_array = array("$title","$rating","$plot"); $string = implode(';', $names_array); fwrite($handle, $string."\n"); fclose($handle); } ?> this code above works fine, problem mentioned below when read text file.
<ul class="movies" id="movies"> $filename = 'movies.txt'; $handle = fopen($filename, 'r'); $datain = fread($handle, filesize($filename)); $names_array = explode(';', $datain); foreach($names_array $name) { echo '<li>'.$name.'</li>'; } ?> </ul> this echo's out reading file in new line. this:
<li>movie title1</li> <li>movie ratings1</li> <li>plot 1</li> <li>movie title2</li> <li>movie ratings2</li> <li>plot 2</li> <li>movie title3</li> <li>movie ratings3</li> <li>plot 3</li> i want have same li line every line text file, this:
<li>movie title1, movieratings1, plot1</li> <li>movie title2, movieratings2, plot2</li> <li>movie title3, movieratings3, plot3</li> etc. new php, not arrays , loops in php. thank in advance!
you should explode() file on newlines "\n" first. loop through array , in inner loop, explode() on semicolons. this:
$lines = explode("\n", $datain); foreach($lines $line){ list($title,$ratings,$plot) = explode(";", $line, 3); // echo <li>s $title,$ratings,$plot here } it worth looking php's fgetcsv() , fputcsv() functions, designed doing manually.
Comments
Post a Comment