Reputation: 757
Apologies if this may be a simple question, I haven't worked with flat file databases before and finding this pretty non-intuitive.
I have a flat file, tab \t
and \n
new line delimited database as such:
Sebastian Ingrosso Kidsos (Wippenberg Mix) 2F32829628 Electro
Avicii Silhouettes 2F47987574 House
(two rows just for sample). I want to be able to iterate through each line, encapsulate each element delimited by a \t
tab character inside a span
tag and then move on to the next line, preferable encapsulating each new line in a <li>
element.
I have this to start with, which doesn't add spans or li's:
function display_tracklist($max) {
$db = fopen('db/db.txt', 'r');
$row = 0;
while (($tracks = fgetcsv($db, "\n")) !== FALSE) {
if ($max > count($tracks)) {
$max = count($tracks);
}
$row++;
for ($index=0; $index < $max; $index++) {
echo "<li>" . $tracks[$index] . "</li>";
}
}
fclose($db);
}
What is the best way to handle this task? Normally I would have done this with a SQL database making things a lot more intuitive but that's not an option here. I've thought of nested foreach loops, etc. no luck so far.
End result should be:
<li><span>Sebastian Ingrosso</span> <span>Kidsos (Wippenberg Mix)</span> <span>2F32829628</span> <span>Electro</span></li>
<li><span>Avicii</span> <span>Silhouettes</span> <span>2F32829628</span> <span>House</span></li>
etc.
Upvotes: 2
Views: 343
Reputation: 11586
Assuming your data is alike my $data
, that looks like giving what you want;
$data = "Sebastian\tIngrosso\tKidsos (Wippenberg Mix)\t2F32829628\tElectro\nAvicii\tSilhouettes\t2F47987574\tHouse";
foreach ((array) explode("\n", $data) as $lines) {
print "<li>";
foreach ((array) explode("\t", $lines) as $line) {
print "<span>{$line}</span>";
}
print "</li>\n";
}
Output;
<li><span>Sebastian</span><span>Ingrosso</span><span>Kidsos (Wippenberg Mix)</span><span>2F32829628</span><span>Electro</span></li>
<li><span>Avicii</span><span>Silhouettes</span><span>2F47987574</span><span>House</span></li>
Upvotes: 1