Jump to content

[SOLVED] How to get next and previous $row['TITLE'] field from next and previous row?HELP


phpocean

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.......

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.