t_machine Posted August 29, 2008 Share Posted August 29, 2008 I'm trying to get the last value in the for loop so I can assign a value to it. Example: for($i =0; $i<10; $i++){ if(is_last_value_do_something){ echo 'this is last in the loop.'; } } Thanks for any help Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/ Share on other sites More sharing options...
pocobueno1388 Posted August 29, 2008 Share Posted August 29, 2008 In the example you give, this is how you would do it: <?php for($i =0; $i<10; $i++){ if($i == 9){ echo 'this is last in the loop.'; } } ?> Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628394 Share on other sites More sharing options...
cooldude832 Posted August 29, 2008 Share Posted August 29, 2008 sorta seems to be a bad method to use a loop just to get the last item want to explain? Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628395 Share on other sites More sharing options...
t_machine Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks for the replies I will expand a little on my example. In my example the "10" is defined but in my codes the number is determined by the results in a database. I am using the "for" loop to display the results and would like for it to reach the last result and echo some extra content. Thanks Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628398 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 This is a nice way to do it :-P I prefer this method just to be my pure insane mind at work xD <?php for($i =0; $i<10; $i++){ } echo $i; ?> Tadaa! You just echoed the last item in the loop. It seems redundant, but works, for example: <?php while($row = mysql_fetch_array($query)){ $id = $row[id]; $username = $row[username]; $pass = md5($row[passwd]); } echo $id.$username.$pass; ?> Quite literally, the loop keeps overwriting the variables (so to speak) so that when it finishes, the variable is stored with the last item in the loop. Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628401 Share on other sites More sharing options...
t_machine Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks for the replies I tried this and it seems to do the trick for($i =0; $i<$count; $i++){ if($i == ($count -1)){ echo 'this is last in the loop.'; } } Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628403 Share on other sites More sharing options...
ThYGrEaTCoDeR201 Posted August 29, 2008 Share Posted August 29, 2008 If you want to get the last row in a Mysql table, you can just LIMIT it. Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628505 Share on other sites More sharing options...
kratsg Posted August 29, 2008 Share Posted August 29, 2008 If you want to get the last row in a Mysql table, you can just LIMIT it. Again, if you don't know the number of rows, use mysql_num_rows($query) and then create the LIMIT. There's just so many possible methods that can work in order to achieve the last row. Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628639 Share on other sites More sharing options...
discomatt Posted August 29, 2008 Share Posted August 29, 2008 Assuming you have an auto increment primary key ( most tables should ) SELECT `column` FROM `table` ORDER BY `primary_key` DESC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/121802-how-to-get-last-value-in-for-loop/#findComment-628674 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.