JsusSalv Posted September 17, 2008 Share Posted September 17, 2008 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 More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 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. Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644312 Share on other sites More sharing options...
JsusSalv Posted September 18, 2008 Author Share Posted September 18, 2008 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! Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644319 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 <?php // assume that $inarray is your column values $fliparray = array_flip($inarray); krsort($fliparray); $maxvalue = key($fliparray); ?> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644320 Share on other sites More sharing options...
JsusSalv Posted September 18, 2008 Author Share Posted September 18, 2008 Thanks for all the help. Youre code prompted me to think a bit harder and I ended up with the following: $num_rows = mysql_num_rows($result); $count = $num_rows + 1; echo "$count"; Thank you! Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644376 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 You're welcome. Just remember that what you typed would only return the number of rows plus one, and not necessarily the highest number. Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644381 Share on other sites More sharing options...
JsusSalv Posted September 18, 2008 Author Share Posted September 18, 2008 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. Link to comment https://forums.phpfreaks.com/topic/124735-solved-how-to-detect-last-number/#findComment-644496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.