Page 1 of 1

Array Help - Trying To Get Clarification

Posted: Sun Dec 20, 2009 12:41 am
by JakeJ
It's possible my previous posts haven't been very clear on what my real problem is so I'm going to try again. This problem is very vexing to me and it's important that I get it solved.

I am pulling data from a query. We'll say I have 3 fields: nperi, apr, balance. Let's also say I have 3 rows.

I need to specifically refer to nperi for the 3rd row. So far I have not found a good way to do this. My query will have a variable number of rows, so how do I get to element 1 of row x (where x is the last row)?

All help is greatly appreciated. This problems is driving me nuts. Thanks!

nperi | apr | balance
10 .07 200000
15 .06 15000
20 .05 5000

I just now realized that I could (since my array is sorted by nperi) create another query in descending order, limit it to 1 record and then get the last nperi that way, but there must be a better way to do it than that.

Thanks!

Re: Array Help - Trying To Get Clarification

Posted: Mon Dec 21, 2009 2:35 pm
by AbraCadaver
Depends upon how you're doing it. Code would be helpful. Do you need to return all rows from the db or just the one with the max nperi? An idea assuming this is how you are doing it:

Code: Select all

while($rows[] = mysql_fetch_array($result)) { }
 
$last_nperi = $rows[count($rows)-1][0];
//or
$last_nperi = $rows[count($rows)-1]['nperi'];

Re: Array Help - Trying To Get Clarification

Posted: Mon Dec 21, 2009 9:53 pm
by JakeJ
AbraCadaver wrote:Depends upon how you're doing it. Code would be helpful. Do you need to return all rows from the db or just the one with the max nperi?
Well, for some other stuff, I need all the rows, but so far my solution to just return the one row and do it as a separate query works great and won't cause me too much overhead unless this site gets so wildly successful that I have 1000 people hitting the submit button at the same time in which case I will be paying a team of developers to refine and expand the project.

Anyway, thank you for your suggestion, it's much appreciated and I can see how it would be better. As soon as I get the product submitted and beginning refining it after that, I'll be sure to implement this.