tail Posted February 21, 2009 Share Posted February 21, 2009 Ok I'm using this query for getting the previous ID: $prev_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` > '$db_id' ORDER by `artist`,`song` ASC LIMIT 1")); And this to display the link: if (!empty($prev_id)) { $prev = '<a href="index.php?id=music&page=view&song_id='.$prev_id['id'].'" class="boldlink">Previous Song</a>'; } else { $prev = '<b>Previous Song</b>'; } Query for next ID: $next_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` < '$db_id' ORDER by `artist`,`song` DESC LIMIT 1")); And this to display the link: if (!empty($next_id)) { $next = '<a href="index.php?id=music&page=view&song_id='.$next_id['id'].'" class="boldlink">Next Song</a>'; } else { $next = '<b>Next Song</b>'; } What I'm trying to accomplish is to have links point to the next and previous ID's ordered by artist, then song. Is it possible to do it the way I'm trying? If so, please point me in the right direction. Thanks for all the help so far. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/ Share on other sites More sharing options...
chriso20 Posted February 21, 2009 Share Posted February 21, 2009 What's $db_id? ASC/DESC is irrelevant if you're limiting to 1 by the way. Also don't SELECT *, it's quicker to SELECT id. SELECT * is returning everything, artist, id, song.. etc... not just id which is what you want. But yeah, what's $db_id? Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767635 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 $db_id is the id of the current record being viewed. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767638 Share on other sites More sharing options...
aliento Posted February 21, 2009 Share Posted February 21, 2009 $next= '$db_id+1; SELECT * FROM `tracks` WHERE `id` = '$next' Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767643 Share on other sites More sharing options...
aliento Posted February 21, 2009 Share Posted February 21, 2009 or $next= '$db_id+1; $next_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` = '$next' ")); Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767644 Share on other sites More sharing options...
sasa Posted February 21, 2009 Share Posted February 21, 2009 SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767647 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 or $next= '$db_id+1; $next_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` = '$next' ")); Thanks for the quick replies! I wish it was that simple though, however there are gaps in the id's in my database due to deletions and etc. so that would not work. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767648 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1 My code now reads $prev_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1")); $next_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `id` < '$db_id' AND `artist`<=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`<=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1")); if (!empty($prev_id)) { $prev = '<a href="index.php?id=music&page=view&song_id='.$prev_id['id'].'" class="boldlink">Previous Song</a>'; } else { $prev = '<b>Previous Song</b>'; } if (!empty($next_id)) { $next = '<a href="index.php?id=music&page=view&song_id='.$next_id['id'].'" class="boldlink">Next Song</a>'; } else { $next = '<b>Next Song</b>'; } However, the id's are not being acquired in the right order. Also, some are being skipped.This might be due to the fact that some artists in the database, when ordered by id, are not in alphabetical order perhaps? I'm not sure though as I'm not very advanced with MySQL queries. If you need to see a demo of what's happening, here is the page. You need to click on the play button to view the individual result. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767651 Share on other sites More sharing options...
aliento Posted February 21, 2009 Share Posted February 21, 2009 ah ok. you only know the current id so : $dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` DESC LIMIT 2"); while($db_return = mysql_fetch_row($database_q)) { $next['artist']=$db_return['artist']'; $next['song']=$db_return['song']; } $dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` ASC LIMIT 2"); while($db_return = mysql_fetch_row($database_q)) { $previous['artist']=$db_return['artist']'; $previous['song']=$db_return['song']; } I am sure that there is an easier code out there but that code i learned to use. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767655 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 So after getting the value of the next artist and song, then I query the database like this: $next_query = 'SELECT `id` FROM `tracks` WHERE `artist`='$next['artist'], `song`='$next['song']'; $prev_query = 'SELECT `id` FROM `tracks` WHERE `artist`='$previous['artist'], `song`='$previous['song']'; Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767658 Share on other sites More sharing options...
aliento Posted February 21, 2009 Share Posted February 21, 2009 yes it seems right to me Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767659 Share on other sites More sharing options...
sasa Posted February 21, 2009 Share Posted February 21, 2009 ups try this SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song`, id ASC LIMIT 1 UNION SELECT `id` FROM `tracks` WHERE `artist` > (SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song` > (SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song`, id ASC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767660 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 yes it seems right to me I'm now getting this error:" Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" with this code: $dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` DESC LIMIT 2"); while($db_return = mysql_fetch_row($dq)) { $next['artist'] = $db_return['artist']; $next['song'] = $db_return['song']; } $dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` ASC LIMIT 2"); while($db_return = mysql_fetch_row($dq)) { $prev['artist'] = $db_return['artist']; $prev['song'] = $db_return['song']; } $next_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `artist`='$next['artist']',`song`='$next['song']")); $prev_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `artist`='$prev['artist']',`song`='$prev['song']")); Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767664 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 ups try this SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song`, id ASC LIMIT 1 UNION SELECT `id` FROM `tracks` WHERE `artist` > (SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song` > (SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song`, id ASC LIMIT 1 I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767667 Share on other sites More sharing options...
aliento Posted February 21, 2009 Share Posted February 21, 2009 try to google the problems and find through others posts the solution if you are in hurry the white_space is the empty gap between the begining of the file and <?php or the end and ?> no empty spaces before the <?php and ?> - the end Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767674 Share on other sites More sharing options...
tail Posted February 21, 2009 Author Share Posted February 21, 2009 Google doesn't seem to be my friend :-\ Thanks for all the help so far. Anyone know any way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767682 Share on other sites More sharing options...
sasa Posted February 21, 2009 Share Posted February 21, 2009 ( SELECT * FROM `tracks` WHERE `artist` = ( SELECT `artist` FROM `tracks` WHERE `id` = '$db_id' ) AND song > ( SELECT `song` FROM `tracks` WHERE `id` = '$db_id' ) ) UNION ( SELECT * FROM `tracks` WHERE `artist` > ( SELECT `artist` FROM `tracks` WHERE `id` = '$db_id' ) ) ORDER BY `artist` , `song` ASC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-767923 Share on other sites More sharing options...
tail Posted February 22, 2009 Author Share Posted February 22, 2009 Thank you so much! However that code is only working to get the next id. I figured that switching the >'s to <'s would solve the problem, however this just gets the id of the first song when ordered by artist and song. Here is a link to the first record when sorted by artist and song and you can see what I mean. Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-768249 Share on other sites More sharing options...
sasa Posted February 22, 2009 Share Posted February 22, 2009 yes and change order to ORDER BY `artist` DESC , `song` DSSC Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-768257 Share on other sites More sharing options...
tail Posted February 22, 2009 Author Share Posted February 22, 2009 Thanks so much for all your help! The script is now working flawlessly! I can't tell you enough how much this has been frustrating me. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/146211-solved-getting-previous-and-next-record-in-database/#findComment-768274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.