phpocean Posted May 5, 2009 Share Posted May 5, 2009 How do you get the next and previous field from a row? If the page I am on is $row['title'] and I wanted the next and previous field "title" to show on that page as well, what query or php code would I use? Can someone point me in the right direction, as I am quite new to php. Thank people. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/ Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Welcome to PHPFreaks phpocean, If you're calling $row['title'] somewhere in your code, may I ask what $row is defined to be? I'm assuming it's a mysql result but, could you provide the query please? Also, could you explain how your DB is set up? Your question is very bland. We can't offer help unless we know how your data is stored and how you want to get them. Thanks, Ken Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826709 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 You should have an id associated with the title so you can just increment and decrement by 1. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826719 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 Thanks for the fast respond you two. Here is my complete query. The query I need help on are the two last query, which is used to call the next and previous field "title_main" <?php // Connect to DB. // find out how many rows are in the table $query = "SELECT COUNT(*) FROM info_all"; $result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $img_per_page = 1; // find out total pages $totalpages = ceil($numrows / $img_per_page); // Get current page or set a default. if (isset($_GET['Article']) && is_numeric($_GET['Article'])) { // cast var as int $Article = (int) $_GET['Article']; } else { // Default page number. $Article = 1; } // end if // if current page is greater than total pages... if ($Article > $totalpages) { // set current page to last page $Article = $totalpages; } // end if // if current page is less than first page... if ($Article < 1) { // set current page to first page $Article = 1; } // end if // Get table information. $query = "SELECT * FROM info_all WHERE Article=$Article"; $result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR); $row = mysql_fetch_assoc($result); // Get previous page number. $prevpage = $Article + 1; // Get next page. $nextpage = $Article - 1; // Get prev title_main. $pt_query = "SELECT title_main FROM info_all WHERE title_main > $Article ORDER BY title_main ASC LIMIT 1"; $res1 = mysql_query($pt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $pt_title = mysql_fetch_assoc($res1); // Get next title_main. $nt_query = "SELECT title_main FROM info_all WHERE title_main < $Article ORDER BY title_main DESC LIMIT 1"; $res2 = mysql_query($nt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $nt_title = mysql_fetch_assoc($res2); ?> And here is my HTML that I used to call my next and previous field "title_main" <tr style="height:25px;"> <td><img src="prev.gif" class="vam" alt="" /></td> <td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?nPageNo=<?php echo $_GET['nPageNo']; ?>&Article=<?php print $prevpage; ?>"><?php print $pt_title['title_main']; ?></a></span></td> </tr> <tr style="height:1px;"><td colspan="2" class="bgc9"></td></tr> <tr style="height:25px;"> <td><img src="next.gif" class="vam" alt="" /></td> <td class="tal fc14"><span style="margin:0 0 0 5px;"><a href="read.php?nPageNo=<?php echo $_GET['nPageNo']; ?>&Article=<?php print $nextpage; ?>"><?php print $nt_title['title_main']; ?></a></span></td> </tr> Hope this help. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826740 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Wow.. that's a really bad way to ask for help. Just post up your codes and have people read them all and figure out a solution. SELECT title_main FROM info_all WHERE title_main > $Article ORDER BY title_main ASC LIMIT 1 Why are you checking if title_main is greater than $Article? You're checking if a title (which I assume is a string) is greater than a number? I think what you meant to say is to grab the title_main where Article is equal to $prevpage right? You don't need the ORDER BY if you're just LIMITing by 1. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826751 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 Update: it's actually Article > $Article and Article < $Article NOT title_main > $Article and title_main < $Article Since Article and title_main are both field on the same row, I can just use Article (PRIMARY) to find out what row I am on and grab title_main from that row. Of course I would want the next and previous title_main. Sorry If I posted all my code. How could I have posted my code for better help, Ken2k? Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826752 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 Wow.. that's a really bad way to ask for help. Just post up your codes and have people read them all and figure out a solution. Code: [select] SELECT title_main FROM info_all WHERE title_main > $Article ORDER BY title_main ASC LIMIT 1Why are you checking if title_main is greater than $Article? You're checking if a title (which I assume is a string) is greater than a number? I think what you meant to say is to grab the title_main where Article is equal to $prevpage right? You don't need the ORDER BY if you're just LIMITing by 1. Is it more understandable now ken? Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826758 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Well part of asking for help is explaining what went wrong. Posting 2 codes and saying you need help with the last 2 SQLs doesn't tell me what's wrong so I have to read through it, understand what you're doing and then figure out how to answer your query. Of course, depending on people's mood at the time, others like myself can just ignore your inquiry. So it's best to explain things and not slap down pieces of codes. Anyways, did my answer help? Edit: Did you read my comments? Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826759 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 I get what you mean by not having to do the ORDER BY but not the other part. Anyway, it work but it's not consistent. Sometime it would return a value higher or a value lower. My query to get the next and previous field title_main is as follow: // Get prev title_main. $pt_query = "SELECT title_main FROM info_all WHERE Article > $Article ORDER BY title_main ASC LIMIT 1"; $res1 = mysql_query($pt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $pt_title = mysql_fetch_assoc($res1); // Get next title_main. $nt_query = "SELECT title_main FROM info_all WHERE Article < $Article ORDER BY title_main DESC LIMIT 1"; $res2 = mysql_query($nt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $nt_title = mysql_fetch_assoc($res2); Where I set $Article = $_GET[Article] to get my current article ID. Then in my query I wanted to get the next Article and previous Article ID but I don't want the data in field Article, what I wanted is the field title_main in those row. Do I make sense? Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826779 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 I think what you meant to say is to grab the title_main where Article is equal to $prevpage right? Exactly Ken. That's what I am trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826791 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 So in the WHERE clause of your SQL, just say you want Article equal to $prevpage. And $rightpage for the other one. Then take out the ORDER BY. Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826794 Share on other sites More sharing options...
phpocean Posted May 5, 2009 Author Share Posted May 5, 2009 You are a genius Ken. Works like a charm. I might just stay around phpfreaks.com for a while. In case anyone looking for similar code: Here is a working code. // Get previous page number. $prevpage = $Article + 1; // Get next page. $nextpage = $Article - 1; // Get prev title_main. $pt_query = "SELECT title_main FROM info_all WHERE Article = $prevpage LIMIT 1"; $res1 = mysql_query($pt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $pt_title = mysql_fetch_assoc($res1); // Get next title_main. $nt_query = "SELECT title_main FROM info_all WHERE Article = $nextpage LIMIT 1"; $res2 = mysql_query($nt_query, $conn) or trigger_error("SQL", E_USER_ERROR); $nt_title = mysql_fetch_assoc($res2); Than use <?php print $pt_title['title_main']; ?> and <?php print $nt_title['title_main']; ?> to call upon it. caos....... Quote Link to comment https://forums.phpfreaks.com/topic/156951-solved-how-to-get-next-and-previous-rowtitle-field-from-next-and-previous-rowhelp/#findComment-826832 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.