I've been pulling hair out for 2 days on this one.. getting close but I'm fresh out of ideas and the PHP manual isn't offering me anything more at this point.
I'm trying to get my band website up and running and it's coming along nicely. My big problem right now is getting the songlist (which is parsed from a pipe delimited [song|artist] text file) to be searchable. Reference http://www.foundationband.ca/songlist.php
excuse the messy code (probably 90% of my problem...):
Songlist.php (minus extraneous HTML)
<?php
function createSearch() { // create the form to search for song results
echo "<form name=\"songsearch\" action=\"$PHP_SELF\" method=GET>";
echo "Song or Artist:<br>";
echo "<input name=\"criteria\" type=\"text\" size=\"25\"> ";
echo "<input type=\"submit\" name=\"submit\" value=\"Find It\">";
echo "</form>";
} // end function createSearch()
if ($criteria) {
// if $criteria exists, show search form + show search results
createSearch();
echo "<hr size=\"1\" noshade>";
$criteria = strtoupper($criteria);
echo "<h5>Search Results for $criteria</h5>";
// execute search results
$fp = file("includes/songs.php");
foreach($fp as $index=>$line) {
$array = explode("[|]", $line);
for($c = 0; $c < count($array); $c++) {
if(eregi($criteria, trim($array[$c]))) {
$results = str_replace("|", " | ", $array);
echo($results[$c] . "<br>");
}
}
}
echo "<hr size=\"1\" noshade>";
} else {
// if not, show search form + all results
createSearch();
echo "<hr size=\"1\" noshade>";
echo "<h5>Song List</h5>";
echo "<table width=\"100%\">";
include("includes/controlsongs.php");
echo "</table>";
}
?>
controlsongs.php
<?php
include("includes/vars.php");
// get the data, explode it into array
$fp = fopen("includes/songs.php", "r");
$line_data = file("includes/songs.php");
sort($line_data);
reset($line_data);
for($i=0;$i<count($line_data);$i++){
$line = explode("|", $line_data[$i]);
if($i % 2 ==0) {
$bgcolor='#efefef'; }
else {
$bgcolor='#ffffff'; }
// line[0] = song title
// line[1] = artist
// parse the array elements into the html template
echo "<tr><td valign=\"middle\" style=\"background-color: $bgcolor; padding: 2px 0 2px 7px;\"><strong>$line[0]</strong> | <em>$line[1]</em></td></tr>";
}
fclose($fp);
?>
controlsongs.php works in the page, and the search works standalone (but not integrated in the page).
Am I going way overboard here? I'm just lost . . .
Any help is appreciated. Thanks in advance!
Rob