Hi
I'm a newbie to PHP and mySQL I have the following problem.
I have a database with mp3's which I query by artist name, I then print the results in a table with a maximum of 15 rows. I can then have a next, prev, last and first button to navigate. I also have a page with a form to submit the query and this puts it into a variable and into the query. The first page works fine, but if I go to the next page nothing shows, even though it says their are 21 pages. Their are two queries on the page.
$query = "SELECT * FROM mp3s WHERE artist='$artist'Order by album" .
" LIMIT $offset, $rowsPerPage";
$query = "SELECT COUNT(*) AS numrows FROM mp3s WHERE artist='$artist'Order by album";
If I replace the variable $artist in the query with text I eg. U2, the page shows and if I click on next the next page will show with the results.
$query = "SELECT * FROM mp3s WHERE artist='U2'Order by album" .
" LIMIT $offset, $rowsPerPage";
$query = "SELECT COUNT(*) AS numrows FROM mp3s WHERE artist='U2'Order by album";
Here is the full code:-
<?php include 'Connections/mp3.php';?>
<?php
$bgcolor='#f1f122';
echo "<html>";
echo "<head>";
echo "</head>";
echo "<body bgcolor='$bgcolor'>";
$artist = $_POST['search'];
?>
<table width="800" border="1" cellspacing="1" cellpadding="1">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Artist</font></th>
<th><font face="Arial, Helvetica, sans-serif">Album</font></th>
<th><font face="Arial, Helvetica, sans-serif">Song Title</font></th>
<th><font face="Arial, Helvetica, sans-serif">File Path</font></th>
</tr>
<?php
// how many rows to show per page
$rowsPerPage = 15;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
@mysql_select_db($database_mp3) or die( "Unable to select database");
$query = "SELECT * FROM mp3s WHERE artist='$artist'Order by album" .
" LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
// of get number of pages
// print the random numbers
while($row = mysql_fetch_array($result))
{
$f1=$row['artist'];
$f2=$row ['album'];
$f3=$row ['title'];
$f4=$row ['filepath'];
$trimmed = trim($f4, " /var/www/");
$f4 =$trimmed;
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href=" <?php echo $f4; ?>">play</a></font></td>
</tr
<?php
$row++;
}
?>
<?php
// ... the previous code
// how many rows we have in database
$query = "SELECT COUNT(*) AS numrows FROM mp3s WHERE artist='$artist'Order by album";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
?>
<p>
<tr>
<td><?php echo $first; ?></td>
<td><?php echo $prev; ?></td>
<td><?php echo" Showing page $pageNum of $maxPage pages " . $next; ?></td>
<td><?php echo $last; ?></td>
</tr>
</body>
</html>
Submit page code:
<?php
echo "<html>";
echo "<head>";
echo "</head>";
?>
<form method="post" action="pages.php">
<input type="text" name="search" />
<input type="submit" />
</form>
</body>
</html
Not sure why it will work with text and not a variable.
Please Help!