Page 1 of 2
Split mysql results into pages?????
Posted: Mon Feb 23, 2004 8:27 am
by mikegotnaild
Ok ill just give u the code i have and tell you what i need done.
Code: Select all
$query = "select * from Acoustic order by band_name limit $offset, $limit";
$result = mysql_query($query);
// use $result here to output page content
$num=mysql_numrows($result);
$lo=0;
while ($lo < $num) {
$bandname=mysql_result($result,$lo,"band_name");
$description=mysql_result($result,$lo,"description");
$id=mysql_result($result,$lo,"ID");
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&band=$id"><b>$bandname</b></a><br><br><b>Description:</b> $description<br><br><hr>";
++$lo;
}
This is the initial code that displays a list of mysql rows and the data from the columns in the table. I need
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_showcase&file=acoustic&band=$id"><b>$bandname</b></a>"
to take me to a individual page for each row and display this.
Code: Select all
<? $result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$bandname=mysql_result($result,$i,"band_name");
$description=mysql_result($result,$i,"description");
$history=mysql_result($result,$i,"history");
$influences=mysql_result($result,$i,"influences");
$genra=mysql_result($result,$i,"genra");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");
$mp3file=mysql_result($result,$i,"mp3file");
$imagefile=mysql_result($result,$i,"imagefile");
echo "<img align=right src="http://naild.com/localmm/upload/$imagefile"><br><b>$bandname</b><br><br><b>Description:</b> $description<br><br><b>History:</b> $history<br><br><b>Influences:</b> $influences<br><br><b>E-mail:</b> $email<br><br><b>Website:</b> <a href="$website">$website</a>       <b>Mp3: <a href="http://naild.com/localmm/upload/$mp3file">$mp3file</a><hr><br>";
++$i;
}
?>
If im not explaining this well enough please tell me. I really am desparate to get this working. If you want to see exactly what i want done go here
http://naild.com/localmm than go to Bands Showcase . Youll see that i have a list of genras. If you click on acoustic it will show the results for each band that has submitted. I want to be able to click on the band name. And have it take me to a separate page for their band. and display the stuff from the code i just posted above.
?>
Posted: Mon Feb 23, 2004 8:34 am
by Illusionist
Sorry, such file doesn't exist...
Your link doesn't work.
Are you trying to do a pagination type thing?
and whats with this:
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_showcase&file=acoustic&band=$id"><&bandname</b>"
are you sure that link works and takes you to where you sad it does? I really don't think it will....
Posted: Mon Feb 23, 2004 8:40 am
by mikegotnaild
I UPDATED THE LINK go to
http://naild.com/localmm and then click Bands Showcase. I set up Acoustic with a few rows. And i already have pagination working (10 band listings per page). I just want to have separate pages for each band. Just read what i wrote above. Hopefully you understand what i would like done.
Posted: Mon Feb 23, 2004 10:24 am
by mikegotnaild
I dont want no prev 1 2 3 next because thats already done. I simply want
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_showcase&file=acoustic&band=$id"><b>$bandname</b></a>"
To take me to each individual band page. As you can see in the code above i use the column ID as $id and displays the ID number for the row. NOW i need to have it to where when i click this
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_showcase&file=acoustic&band=$id"><b>$bandname</b></a>"
It goes to each individual band page that echos this:
Code: Select all
<? $result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$bandname=mysql_result($result,$i,"band_name");
$description=mysql_result($result,$i,"description");
$history=mysql_result($result,$i,"history");
$influences=mysql_result($result,$i,"influences");
$genra=mysql_result($result,$i,"genra");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");
$mp3file=mysql_result($result,$i,"mp3file");
$imagefile=mysql_result($result,$i,"imagefile");
echo "<img align=right src="http://naild.com/localmm/upload/$imagefile"><br><b>$bandname</b><br><br><b>Description:</b> $description<br><br><b>History:</b> $history<br><br><b>Influences:</b> $influences<br><br><b>E-mail:</b> $email<br><br><b>Website:</b> <a href="$website">$website</a>       <b>Mp3: <a href="http://naild.com/localmm/upload/$mp3file">$mp3file</a><hr><br>";
++$i;
}
Ive been lookin and askin for days but i just cant figure it out. Its probably not that hard. I know its not a hard 'concept' but im just starting out in php and i dont know how to code it.
Posted: Mon Feb 23, 2004 11:15 am
by Illusionist
i didn't even bother reading your posts because this si WRONG, do you know any HTML at all? DO you know what a link is?
"<a href=\"
http://naild.com/localmm/modules.php?na ... andname</b>"
is not a link. That will never be clickable. I thinkw hat you are trying to do is this:
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_showcase&file=acoustic&band=$id">$bandname</a>";
Posted: Mon Feb 23, 2004 11:50 am
by mikegotnaild
hey... did u ever think that maybe there was a typo.. or that maybe i forgot to CLOSE the link "</a>" and the variable i accidently put & in front instead of $ ? YES i DO know html. Its in my code that i have. And honestly... do you think i could possibly grasp ANY of the concepts that i already have down in black and white if i didnt even know any html??
Posted: Mon Feb 23, 2004 5:31 pm
by Illusionist
black and white? and if it was a typo you should've fixed it. Anyways i see were oyu fixed it now. And ok... i think the problem lies in the second page. I Think your going to have to do another SELECT statement and query that. Then parse throught he results...
Posted: Mon Feb 23, 2004 9:49 pm
by qads
Posted: Tue Feb 24, 2004 5:39 am
by mikegotnaild
Im too new to php to know exactly what to do with this. I know nothing about javascript. I dont need previous or next buttons either. Mine as well call me stupid and get it over with.
Posted: Tue Feb 24, 2004 7:46 am
by Illusionist
your problem is on the second page. You just start out with $result = mysql_query($query); but $query is empty... Thats why i said you'll have to do another SELECT statement on the second page.
Posted: Tue Feb 24, 2004 8:08 am
by mikegotnaild
Code: Select all
<?
$query="SELECT * FROM Acoustic";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$bandname=mysql_result($result,$i,"band_name");
$description=mysql_result($result,$i,"description");
$history=mysql_result($result,$i,"history");
$influences=mysql_result($result,$i,"influences");
$genra=mysql_result($result,$i,"genra");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");
$mp3file=mysql_result($result,$i,"mp3file");
$imagefile=mysql_result($result,$i,"imagefile");
echo "<img align=right src="http://naild.com/localmm/upload/$imagefile"><br><b>$bandname</b><br><br><b>Description:</b> $description<br><br><b>History:</b> $history<br><br><b>Influences:</b> $influences<br><br><b>E-mail:</b> $email<br><br><b>Website:</b> <a href="$website">$website</a>       <b>Mp3: <a href="http://naild.com/localmm/upload/$mp3file">$mp3file</a><hr><br>";
++$i;
}
?>
Now how do i get it to where when i click that link from the first page
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&band=$id"><b>$bandname</b></a>
It goes to each separate band page. Remember that there will always be new bands. When i requre("page2.php"); it automatically shows the information from that second page... i dont want that. I want to be able to click
Code: Select all
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&band=$id"><b>$bandname</b></a>
Than it takes me to the separate extended info on the second page for that particular band.
Posted: Tue Feb 24, 2004 9:02 am
by Illusionist
Well... im assuming your have an id field in your database, and thats where the band=$id is comming from? so if you do your select statement and add WHERE id=$id then i think you will get what your looking for.
Code: Select all
$id = $_GET['band'];
$query="SELECT * FROM Acoustic WHERE id=$id";
$result=mysql_query($query);
get the idea? If im totally wrong either with that, or with that your trying to do, please tell me!
Posted: Tue Feb 24, 2004 9:31 am
by mikegotnaild
Ok im getting this "Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /www/n/naild/htdocs/localmm/modules/Bands_Showcase/page2.php on line 18" Ill just give you my full code for both pages.
acoustic.php
Code: Select all
<?php
require("page2.php");
if (!eregi("modules.php", $PHP_SELF)) {
die ("You can't access this file directly...");
}
include("header.php");
$index = 1;//right sidebar or not 0=off 1=on
title("Acoustic");
OpenTable();
?>
<?php
class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);
$page = max($page, 1);
$page = min($page, $numPages);
$offset = ($page - 1) * $limit;
$ret = new stdClass;
$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;
return $ret;
}
}
?>
<?php
// get the pager input values
$page = $_GET['page'];
$limit = 10;
$result = mysql_query("select count(*) from Acoustic");
$total = mysql_result($result, 0, 0);
// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;
// use pager values to fetch data
$query = "select * from Acoustic order by band_name limit $offset, $limit";
$result = mysql_query($query);
// use $result here to output page content
$num=mysql_num_rows($result);
$lo=0;
while ($lo < $num) {
$bandname=mysql_result($result,$lo,"band_name");
$description=mysql_result($result,$lo,"description");
$id=mysql_result($result,$lo,"ID");
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&band=$id"><b>$bandname</b></a><br><br><b>Description:</b> $description<br><br><hr>";
++$lo;
}
?>
<?
// output paging system (could also do it before we output the page content)
if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&page=" . ($page - 1) . "">Previous</a>";
for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i ";
else
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&page=$i">Page $i</a>";
}
if ($page == $pager->numPages) // this is the last page - there is no next page
echo "Next";
else // not the last page, link to the next page
echo "<a href="http://naild.com/localmm/modules.php?name=Bands_Showcase&file=acoustic&page=" . ($page + 1) . "">| Next</a>";
?>
<?
CloseTable();
include("footer.php");
?>
page2.php
Code: Select all
<?php
if (!eregi("modules.php", $PHP_SELF)) {
die ("You can't access this file directly...");
}
include("header.php");
$index = 1;//right sidebar or not 0=off 1=on
title("Band Info");
?>
<?
$id = $_GET['band'];
$query="SELECT * FROM Acoustic WHERE id=$id";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
$i=0;
while ($i < $num) {
$bandname=mysql_result($result,$i,"band_name");
$description=mysql_result($result,$i,"description");
$history=mysql_result($result,$i,"history");
$influences=mysql_result($result,$i,"influences");
$genra=mysql_result($result,$i,"genra");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");
$mp3file=mysql_result($result,$i,"mp3file");
$imagefile=mysql_result($result,$i,"imagefile");
echo "<img align=right src="http://naild.com/localmm/upload/$imagefile"><br><b>$bandname</b><br><br><b>Description:</b> $description<br><br><b>History:</b> $history<br><br><b>Influences:</b> $influences<br><br><b>E-mail:</b> $email<br><br><b>Website:</b> <a href="$website">$website</a>       <b>Mp3: <a href="http://naild.com/localmm/upload/$mp3file">$mp3file</a><hr><br>";
++$i;
}
?>
<?
include("footer.php");
?>
Do you get what im trying to do?
Posted: Tue Feb 24, 2004 11:15 am
by Illusionist
i may be wrong, but i think its mysql_num_rows()
Posted: Tue Feb 24, 2004 11:31 am
by mikegotnaild
i sure do make alot of typo's. I still get that error though "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/n/naild/htdocs/localmm/modules/Bands_Showcase/page2.php on line 18" I tried removing WHERE ID=$id and i dont get the error but doesnt do what i want.