HTML for Form:
<form action=ksongsearch_action.php name="" method=get>
<input type="hidden" name="offset" value="0">
Song Title:
<input name="songtitle" type="text" size="30">
Artist:
<input name="artist" type="text" size="30
Order By:
<select name="sort_order">
<option value="songtitle" selected>Song Title</option>
<option value="artist">Artist</option></select>
<input type="submit" name="name2" value="Find my song"> </form>
PHP:
<?php
$songtitle_search=$songtitle;
$artist_search=$artist;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++BEGIN PHP++++++++++++++++++++++++++++++++++++++++++++++++++
// create connection
$connection = mysql_connect('localhost', pianoman_pianoma, jabber44031672);
// test connection
if (!$connection) {
echo "Couldn't make a connection!";
exit;
}
// select database
$db = mysql_select_db("pianoman_pianotest", $connection);
// test selection
if (!$db) {
echo "Couldn't select database!";
exit;
}
$limit=20; // rows to return
$numresults=mysql_query("SELECT songtitle, artist
FROM karaoke
where songtitle LIKE '%$songtitle%'
AND artist LIKE '%$artist%'");
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=0;
}
// create SQL statement
$sql = "SELECT songtitle, artist
FROM karaoke
where songtitle LIKE '%$songtitle%'
AND artist LIKE '%$artist%'
ORDER BY $sort_order ASC
limit $offset,$limit";
//$sql_total = "select songtitle from karaoke";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection);
//$sql_count = mysql_num_rows($sql_result);
//$sql_total_query = mysql_query($sql_total,$connection);
//$sql_total_num = mysql_num_rows($sql_total_query);
// start results formatting
//*************Search Results at top of Page
if ($numrows<$limit) {
$thru=$numrows;
}
else {
$thru=$offset+$limit;
}
if ($thru>$numrows) {
$thru=$thru-($thru-$numrows);
}
$offset_plus=$offset+1;
echo "<b>Displaying Results $offset_plus thru $thru of $numrows Songs</b><p>";
// next we need to do the links to other results
if ($offset>=1) { // bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
print "<a href=\"$PHP_SELF?offset=$prevoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\"><b><u>Previous Page</u></b></a> | \n";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
//for ($i=1;$i<=$pages;$i++) { // loop thru
// $newoffset=$limit*($i-1);
// print "<a href=\"$PHP_SELF?offset=$newoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\">$i</a> \n";
//}
// check to see if last page
if (!(($numrows)==$thru) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\"><b><u>Next Page</u></b></a>\n";
}
echo "</tr></td></table>";
//*************Close Results at top of page
echo "<p><TABLE width=\"95%\" BORDER=1 cellpadding=\"2\" cellspacing=\"0\" align=\"center\">";
echo "<TR bgcolor=\"#990000\"><TH><font color=\"#FFFFFF\">Title</font></TH><TH><font color=\"#FFFFFF\">Artist</font></TH>";
// format results by row
while ($row = mysql_fetch_array($sql_result)) {
$songtitle = $row["songtitle"];
$artist = $row["artist"];
echo
"<TR nowrap><TD>$songtitle</TD><TD>$artist</TD></TR>";
}
echo "</TABLE>";
if ($numrows<$limit) {
$thru=$numrows;
}
else {
$thru=$offset+$limit;
}
if ($thru>$numrows) {
$thru=$thru-($thru-$numrows);
}
$offset_plus=$offset+1;
echo "<center><p><b>Displaying Results $offset_plus thru $thru of $numrows Songs</b><p>";
// next we need to do the links to other results
if ($offset>=1) { // bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
print "<a href=\"$PHP_SELF?offset=$prevoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\"><b><u>Previous Page</u></b></a> | \n";
}
// calculate number of pages needing links
//$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
//if ($numrows%$limit) {
// has remainder so add one page
// $pages++;
//}
//for ($i=1;$i<=$pages;$i++) { // loop thru
// $newoffset=$limit*($i-1);
// print "<a href=\"$PHP_SELF?offset=$newoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\">$i</a> \n";
//}
// check to see if last page
if (!(($numrows)==$thru) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset&songtitle=$songtitle_search&artist=$artist_search&sort_order=$sort_order\"><b><u>Next Page</u></b></a>\n";
}
// free resources and close connection
mysql_free_result($sql_result);
mysql_close($connection);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++END PHP++++++++++++++++++++++++++++++++++++++++++++++++++
?>