help with this loop guys

#1
[eluser]billie joe[/eluser]
for($i = 0; $i <= count($data['getComment']) ; $i++ )
{
echo$i;
}


the answer would be this:
01234

i cant figure out how ti get only the last number..
for example i got:
012345678910

i would only get the last vale 10...

thanks for your help i appreciate it

#2
[eluser]Dam1an[/eluser]
If all you want, is the last element of the array, you don't need to loop, you could use array_pop()
Code:
$last_item = array_pop($data['getComment']);

Alternativly, you could determine the index of the last item (1 less then the size) and call that position in the array

#3
[eluser]billie joe[/eluser]
thanks dude but it give me some error..

A PHP Error was encountered
Severity: Warning
Message: array_pop() [function.array-pop]: The argument should be an array
Filename: controllers/MostLikedSongsController.php
Line Number: 41


i dont understand it's an array already.... but CI can read it

#4
[eluser]Dam1an[/eluser]
Can you show me the code where you create the array and populate it, maybe I can see where its going wrong

You should also try defining it explicity as an array when you create it
Code:
$my_array = array();

#5
[eluser]billie joe[/eluser]
this is the array generated by my sql

Array
(
[0] => Array
(
[user_id] => 4
[music_id] => 35
)

[1] => Array
(
[user_id] => 3
[music_id] => 35
)

[2] => Array
(
[user_id] => 2
[music_id] => 35
)

[3] => Array
(
[user_id] => 1
[music_id] => 35
)

)

this is my code to get those array

$musicDetails = new UserMusicLikeModel();
$musicDetails = Doctrine_Query::create()
->leftjoin('MusicModel m')
->innerjoin('m.UserMusicLikeModel uml')
->orderby('uml.user_id DESC')
->execute(array(), Doctrine::HYDRATE_ARRAY);

$data['getComment'] = $musicDetails[0]['UserMusicLikeModel'];

echo '<pre>';
print_r($data['getComment']);
echo '</pre>';

#6
[eluser]billie joe[/eluser]
and if i do this

for($i = 0; $i <= count($data[‘getComment’]) ; $i++ )
{
echo$i;
}

it would return me the value of

01234

#7
[eluser]Dam1an[/eluser]
Hmm... what if you try explicitly casting it to an array (never tried this, so not sure if it'll work)

Code:
$last_item = array_pop((array) $data['getComment']);

#8
[eluser]billie joe[/eluser]
Fatal error: Only variables can be passed by reference in C:\wamp\www\twitmusic\application\controllers\MostLikedSongsController.php on line 30


that one....

#9
[eluser]billie joe[/eluser]
heheh guys its working now! thanks to my brain though its too small to code php

#10
[eluser]Dam1an[/eluser]
So what was wrong with it in the end?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.