Array data to nested HTML
I'm having trouble converting an array into correctly nested HTML. Assuming I have an array with the following example values -
An, Author---Some Book
Another, Author---A Book
Another, Author---Another Book
Be, Author---Book 1
Be, Author---Book 2
No, Author---Book 1
How do turn that into an organised HTML like -
<div>
<div class="letter">A</div>
<div>
<div class="author">An, Author</div>
<div class="title">Some Book</div>
</div>
<div>
<div class="author">Another, Author</div>
<div class="title">A Book</div>
<div class="title">Another Book</div>
</div>
<div>
<div>
<div class="letter">B</div>
<div>
<div class="author">Be, Author</div>
<div class="title">Book 1</div>
<div class="title">Book 2</div>
</div>
<div>
<div>
<div class="letter">N</div>
<div>
<div class="author">No, Author</div>
<div class="title">Book 1</div>
</div>
<div>
I don't have any trouble with PHP, its just I'm not sure how I would organise the data. Split into three different arrays (Letters, Authors, Books) before pulling the data back together? But then how would I asso开发者_如何转开发ciate the books (Letter->Author->Book)?
Thanks for any ideas.
Assuming your array is already sorted as the example:
for ($i = 0; $i < count($array); $i++) {
$parts = explode("---", $array[$i]);
$author = $parts[0];
$book = $parts[1];
$letter = substr($author, 0, 1);
if (!isset($last_author) || $last_author != $author) {
if ($i > 0 && $last_letter != $letter) {
echo "\t</div>\n";
echo "</div>\n";
} else if ($i > 0) {
echo "\t</div>\n";
}
if (!isset($last_letter) || $last_letter != $letter) {
echo "<div>\n";
echo "\t" . '<div class="letter">' . strtoupper($letter) . '</div>' . "\n";
$last_letter = $letter;
}
echo "\t<div>\n";
echo "\t\t" . '<div class="author">' . $author . '</div>' . "\n";
echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";
$last_author = $author;
} else {
echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";
}
}
if (count($array) > 0) echo "\t</div>\n</div>";
Output from your sample array:
<div>
<div class="letter">A</div>
<div>
<div class="author">An, Author</div>
<div class="title">Some Book</div>
</div>
<div>
<div class="author">Another, Author</div>
<div class="title">A Book</div>
<div class="title">Another Book</div>
</div>
</div>
<div>
<div class="letter">B</div>
<div>
<div class="author">Be, Author</div>
<div class="title">Book 1</div>
<div class="title">Book 2</div>
</div>
</div>
<div>
<div class="letter">N</div>
<div>
<div class="author">No, Author</div>
<div class="title">Book 1</div>
</div>
</div>
You can also produce a multidimensional array which may require less code to render:
$new = array();
for ($i = 0; $i < count($array); $i++) {
$parts = explode("---", $array[$i]);
$author = $parts[0];
$book = $parts[1];
$letter = substr($author, 0, 1);
$new[$letter][$author][] = $book;
}
foreach ($new as $letter => $authors) {
foreach ($authors as $author => $books) {
foreach ($books as $book) {
}
}
}
You've got div soup there, however you parse your arrays.
Try outputting something meaningful and semantic using lists.
精彩评论