botlife Posted February 13, 2013 Share Posted February 13, 2013 Hey there, so I have a custom Wordpress loop that is working just fine. Inside that loop I have a second loop using Salesforce API. What is happening is that the main loop is looping but the secondary Salesforce loop is showing the same record for each and not looping. There is a unique ID for each so that isn't the problem. while ($wp_query->have_posts()) : $wp_query->the_post(); $sid = get_the_ID(); $SalesForceId = get_post_meta($sid, 'SalesForceId', true); $query = "SELECT Name, Phone, BillingCity, BillingState from Account WHERE Id = '" . $SalesForceId . "'"; $response = $mySforceConnection->query(($query)); foreach ($response->records as $record) { $Name = $record->{Name}; $Phone = $record->{Phone}; $BillingCity = $record->{BillingCity}; $BillingState = $record->{BillingState}; } echo $Name; echo $BillingCity; echo $BillingState; endwhile; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/ Share on other sites More sharing options...
GKWelding Posted February 13, 2013 Share Posted February 13, 2013 First, CODE tags make things much clearer. Second, just before foreach ($response->records as $record) { Can you do a var_dump($response->records);die(); and post the output here. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412221 Share on other sites More sharing options...
AyKay47 Posted February 13, 2013 Share Posted February 13, 2013 Also, it may help to output your query and check for errors there. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412224 Share on other sites More sharing options...
botlife Posted February 13, 2013 Author Share Posted February 13, 2013 (edited) First, CODE tags make things much clearer. Second, just before foreach ($response->records as $record) { Can you do a var_dump($response->records);die(); This is the output array(1) { [0]=> object(stdClass)#4627 (5) { ["Id"]=> NULL ["BillingCity"]=> string(6) "OXNARD" ["BillingState"]=> string(2) "CA" ["Name"]=> string(28) "AMERICAN TALL SHIP INSTITUTE" ["Phone"]=> string(10) "8059010585" } } Edited February 13, 2013 by botlife Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412225 Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 Ok, turn the fancy editor off by clicking the toggle switch, and try that again. But: foreach ($response->records as $record) { $Name = $record->{Name}; $Phone = $record->{Phone}; $BillingCity = $record->{BillingCity}; $BillingState = $record->{BillingState}; } echo $Name; echo $BillingCity; echo $BillingState; ?> If you don't echo them until after your foreach() loop, you will only ever see the last set. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412226 Share on other sites More sharing options...
GKWelding Posted February 13, 2013 Share Posted February 13, 2013 Well there's your issue, the array has only 1 element. your inner foreach is working fine. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412230 Share on other sites More sharing options...
AyKay47 Posted February 13, 2013 Share Posted February 13, 2013 Well there's your issue, the array has only 1 element. your inner foreach is working fine. What? Jessica pointed out the problem that we both overlooked. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412231 Share on other sites More sharing options...
botlife Posted February 13, 2013 Author Share Posted February 13, 2013 Well there's your issue, the array has only 1 element. your inner foreach is working fine. The array has only one row because the query is focused on only one ID and not all the IDs. The main loop is pulling the Salesforce IDs I have stored in Wordpress. The second loop is then taking that ID and running the second query through Salesforce. So without the second loop it will only return that one result which happened in the Dump and is happening in the second loop currently. Each run through should re-run the Salesforce query to yield a result and it isn't doing that. I can echo all the IDs so that isn't the problem. The problem seems to be that I can only use the second query once. I think I am doing something backwards. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412233 Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 (edited) Echo the query to see if it has the same ID each time or a different ID each time. Really you should write one query to join these tables, if you want to do it the right way, post the tables structures and someone can help you write a JOIN to get all the data the right way. Edited February 13, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412234 Share on other sites More sharing options...
botlife Posted February 13, 2013 Author Share Posted February 13, 2013 Echo the query to see if it has the same ID each time or a different ID each time. Really you should write one query to join these tables, if you want to do it the right way, post the tables structures and someone can help you write a JOIN to get all the data the right way. Okay, when I echo the query, the first entry has results and every other entry is echoing SELECT Name, Phone, BillingCity, BillingState from Account WHERE Id = '003G00000184ong'array(0) { } with no results. The ID though is different on each one so that is working, it just isn't running the second query more than once. Maybe it is because $query is the same variable name each time. Maybe there should be ++ on each $query and $response. I didn't join the tables because one is on a Wordpress database and the second is using Salesforce API. I have joined on the same database before, but not to separate entities. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412242 Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 Since you're sending a raw query, you can at least make just one query instead of many. First, query just wordpress (using RAW SQL) to get all of your IDs and put them in an array. Then use IN to put them in your other system's query. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412243 Share on other sites More sharing options...
botlife Posted February 13, 2013 Author Share Posted February 13, 2013 Since you're sending a raw query, you can at least make just one query instead of many. First, query just wordpress (using RAW SQL) to get all of your IDs and put them in an array. Then use IN to put them in your other system's query. That is a really good idea. Let me whip something up. Quote Link to comment https://forums.phpfreaks.com/topic/274440-inside-api-loop-not-looping-within-main-loop/#findComment-1412249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.