Jump to content

PHP / AJAX / JavaScript Array Structure Fails


unemployment

Recommended Posts

In my php ajax script I have...

 

foreach ($data[0] as $k => $r)
{
                 if (($r['feedid'] != $user_info['uid']) && (!empty($r['action_id'])) && ($r['accounttyperaw'] !== '') && ($r['action_id'] != 'joinedcompany') && ($r['action_id'] != 'companyprofilepicture'))
	{
		$data[0]['s_u_avatar'] = "/assets/img/avatars/defaultuser.jpg"; 
	}
	else if (($r['accounttyperaw'] == '') || ($r['action_id'] == 'joinedcompany') || ($r['action_id'] == 'companyprofilepicture'))
	{
		$data[0]['c_avatar'] = "/assets/img/avatars/defaultcompany.jpg"; 
	}
	else
	{
		$data[0]['f_u_avatar'] = "/assets/img/avatars/defaultuser.jpg"; 
	}
}

 

That returns this...

 

<pre>Array
(
    [0] => Array
        (
            [id] => 1
            [firstname] => gdd
            [lastname] => Biogdgddgndo
            [username] => dgd
            [industry] => 
            [stage] => 
            [companyid] => 
            [companytag] => 
            [gender] => 0
            [accounttyperaw] => 1
            [accounttype] => dgg
            [country] => 231
            [state] => 10
            [city] => Old Lyme
            [approveddate] => 1307226996
            [feedid] => 4
            [feedfirstname] => fgd
            [feedlastname] => fdg
            [feedusername] => vfgbd
            [action_id] => usergoal
            [details] => ckvzokvozk
            [display_name] => Jason 
            [associate_name] => Chefgdgdgdlsea 
            [f_u_avatar] => /assets/idmg/afgdgdvatars/usdgdgdgfers/jasonbigdgondo.png
            [s_u_avatar] => /asdgdggdggsets/img/ggddgavatars/users/lovebug.png
            [c_avatar] => /assgdgdg
        )

    [1] => Array
        (
            [id] => 1
            [firstname] => Jdgadgdggdgdgdson
            [lastname] => Biogdgdgdgndo
            [username] => gd
            [industry] => 
            [stage] => 
            [companyid] => 
            [companytag] => 
            [gender] => 0
            [accounttyperaw] => 1
            [accounttype] => gd
            [country] => 231
            [state] => 10
            [city] => Old gdg
            [approveddate] => 1307226988
            [feedid] => 4
            [feedfirstname] => dg
            [feedlastname] => Madgdngs
            [feedusername] => dg
            [action_id] => usergoal
            [details] => dofkasokf
            [display_name] => Jason dg
            [associate_name] => Chfgd
            [f_u_avatar] => gdg
            [s_u_avatar] => bbfd
            [c_avatar] => 
        )
)

 

ajax.get('/assets/ajax/home_news_feed.php?uid=' + uid,  function (resp)
{		
var feed 	= eval('(' + resp + ')');

for(var i = 0; i < feed.length; i++)
{
                stuff
         }

}

 

fut my feed.length isn't working at all.  I don't think my array is correct.

 

I find that when using AJAX with PHP, it is much easier to pass arrays from PHP back to Javascript if I use the JSON format. Since it looks like you're using the jQuery library, I would suggest you look at $.post or $.get as a simpler way of doing the AJAX code.

 

Ken

I find that when using AJAX with PHP, it is much easier to pass arrays from PHP back to Javascript if I use the JSON format. Since it looks like you're using the jQuery library, I would suggest you look at $.post or $.get as a simpler way of doing the AJAX code.

 

Ken

 

Hi Ken,

 

I'm not using JQuery and I am encoding the array in json. 

 

When I run feed.length my array is only equal to 1, but I know that there should be more rows listed as the json encoded array includes them.

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.