Jump to content

[SOLVED] How to detect last number?


JsusSalv

Recommended Posts

Hello Everyone:

 

Does anyone know how to detect the last (highest) number in a series?  For example, I have a MySQL column with a list of numbers: 0-9.  I'd like to use PHP to detect the last (highest) number, in this case it would be the number 9.  Can someone shed some light on how to do this?  I'm not looking for some long-winded, convoluted function with all the bells and whistles.  I just need a simple code example that can do this.  Thank you!

 

 

~Hugo

Link to comment
https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/
Share on other sites

The easiest way would be to do it in your query:

 

SELECT MAX(column_name) FROM table_name WHERE whatever

 

But, if you already had the numbers in an array, I would swap the values and keys, sort it, and grab the highest key by pointing it at the end. If this doesn't make sense, include some of your code.

I don't have code...that's why I'm asking for help.  I'm not exactly sure where to start.  I've searched google examples but I must not be typing the search query correct.

I'm not interested in changing the SQL query.  I just want to grab all the numbers from the column, detect which number within that column is the last (highest) number and echo that number to the screen.

 

"swap the values and keys, sort it, and grab the highest key by pointing it at the end"  ==>  That's what I'm trying to do but I am not sure how to do it in PHP.  It must be a simple thing for sure but I'm not finding some examples that could help.

 

Any ideas or code examples?

 

Thanks!

What I wanted to do was to find the last/highest number in the column then add 1 to it.  This would return a value for the input field in a form.  But your code made me think of just taking the total number of records in the column and then just add one to that...therefore my results.  It works well for what it's purpose.  Thank you F1Fan, couldn't of done it without your help and input.

Archived

This topic is now archived and is closed to further replies.

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