Click to See Complete Forum and Search --> : display results


jrthor2
11-25-2003, 02:00 PM
I wrote a webervice that finds addresses closest to the zip code a person enters. My result is an array of the closest x number of addresses. On my client page, how do I display the results. I am getting "Array" when trying to echo out my $soapresults. Below is my code.

<?
require_once('/home/prgjr1/public_html/ra-new/inc/nusoap.php');
$parameters = array('zip'=>$_GET['zip']);

$soapclient = new soapclient('http://rawebdev.rss.host.com/prgjr1/ra-new/stores/locator/webservice_server.php');

$soapresults = $soapclient->call('rastore',$parameters);
//print_r($soapresults);
/* handle any SOAP faults. */

if ($soapclient->fault) {
echo " Error exist<br>";
echo "FAULT: <p>Code: {$soapclient->faultcode}<br />";
echo "String: {$soapclient->faultactor} <br>";
} else {
//echo "this is the results: <br><br>"; // . $soapresults;
$cnt = count($soapresults);
//print $cnt;
//$cnt = count($store_numbers);
if ($cnt >0 ) {
for ($i=0; $i < $cnt; $i++) {
echo $soapresults[$i] ."<br>";
}
} else {
echo "No results came back";
}
}

//view soap message
//print '<br>Request<xmp>'.str_replace('><', ">\n<", $soapclient->request).'</xmp>';
//print 'Response<xmp>'.str_replace('><', ">\n<", $soapclient->response).'</xmp>';
?>

jrthor2
11-26-2003, 08:36 AM
can anyone help here?

jrthor2
11-26-2003, 01:23 PM
please can someone help me here.

pyro
11-26-2003, 01:25 PM
Why not just step through the array and print the values?

jrthor2
11-26-2003, 01:36 PM
can you tell me how to do that? Here is a sampl of my array:

Array ( [soapVal] => Array ( [0] => Array ( [name] => Rite Aid [store_number] => 00246 [addr1] => 337 West Chocolate Avenue [city] => Hershey [state] => PA [zip] => 17033 [phone_fe] => (717) 533-2941 [phone_ph] => (717) 533-2941 [phone_phf] => (717) 534-0692 [distance] => 2.55 [convenience_foods] => T [rite_express] => F [one_hour_photo] => T [ra_24_hour] => F [drive_thru_rx] => T [rite_rewards] => T [gnc] => T [western_union] => T [hours_fe_mon] => 08:00 AM - 10:00 PM [hours_fe_tue] => 08:00 AM - 10:00 PM [hours_fe_wed] => 08:00 AM - 10:00 PM [hours_fe_thu] => 08:00 AM - 10:00 PM [hours_fe_fri] => 08:00 AM - 10:00 PM [hours_fe_sat] => 08:00 AM - 10:00 PM [hours_fe_sun] => 08:00 AM - 10:00 PM [hours_rx_mon] => 09:00 AM - 09:00 PM [hours_rx_tue] => 09:00 AM - 09:00 PM [hours_rx_wed] => 09:00 AM - 09:00 PM [hours_rx_thu] => 09:00 AM - 09:00 PM [hours_rx_fri] => 09:00 AM - 09:00 PM [hours_rx_sat] => 09:00 AM - 07:00 PM [hours_rx_sun] => 10:00 AM - 06:00 PM [store_date_open] => 1977-01-16 [store_date_relo] => 1999-02-10 [store_date_reopen] => 9999-12-31 [picture_maker] => T [photo_ramp] => F [store_status] => 00 [detail_desc] => Located At 337 West Chocolate Avenue Across From Day's Inn [division] => E )

pyro
11-26-2003, 01:39 PM
What values are you trying to pull out?

jrthor2
11-26-2003, 01:40 PM
Well, essentially all of them

pyro
11-26-2003, 01:43 PM
Post the results of this, please:

echo "<pre>";
print_r($yourarray);
echo "</pre>";

jrthor2
11-26-2003, 01:46 PM
results as requested (only part, it was too long). there were 11 different arrays returned:

Array
(
[soapVal] => Array
(
[0] => Array
(
[name] => Rite Aid
[store_number] => 04260
[addr1] => 4299 Union Deposit Road
[city] => Harrisburg
[state] => PA
[zip] => 17111
[phone_fe] => (717) 564-6750
[phone_ph] => (717) 564-6750
[phone_phf] => (717) 564-4839
[distance] => 1.23
[convenience_foods] => T
[rite_express] => F
[one_hour_photo] => T
[ra_24_hour] => T
[drive_thru_rx] => T
[rite_rewards] => T
[gnc] => T
[western_union] => T
[hours_fe_mon] => OPEN 24 HOURS
[hours_fe_tue] => OPEN 24 HOURS
[hours_fe_wed] => OPEN 24 HOURS
[hours_fe_thu] => OPEN 24 HOURS
[hours_fe_fri] => OPEN 24 HOURS
[hours_fe_sat] => OPEN 24 HOURS
[hours_fe_sun] => OPEN 24 HOURS
[hours_rx_mon] => OPEN 24 HOURS
[hours_rx_tue] => OPEN 24 HOURS
[hours_rx_wed] => OPEN 24 HOURS
[hours_rx_thu] => OPEN 24 HOURS
[hours_rx_fri] => OPEN 24 HOURS
[hours_rx_sat] => OPEN 24 HOURS
[hours_rx_sun] => OPEN 24 HOURS
[store_date_open] => 1996-08-30
[store_date_relo] => 9999-12-31
[store_date_reopen] => 9999-12-31
[picture_maker] => T
[photo_ramp] => F
[store_status] => 00
[detail_desc] => Located At 4299 Union Deposit Road Across From Comfort Inn
[division] => E
)

}

pyro
11-26-2003, 02:01 PM
See if this helps you:

<?PHP
$subarray_1 = array("1", "2");
$subarray_2 = array("a", "b");
$array = array($subarray_1, $subarray_2);

foreach ($array as $array_val) {
foreach ($array_val as $subarray_val) {
echo $subarray_val."<br>";
}
}

echo "<pre style=\"color: #000; background: #eee; border: 1px solid; padding: 3px;\">";
print_r($array);
echo "</pre>";
?>

jrthor2
12-01-2003, 07:12 AM
Uh, that just printed:

1
2
a
b

Array
(
[0] => Array
(
[0] => 1
[1] => 2
)

[1] => Array
(
[0] => a
[1] => b
)

)

pyro
12-01-2003, 07:38 AM
Yep, I was just showing you how to loop through the arrays and print them out.

jrthor2
12-01-2003, 07:42 AM
Ok, but how do I get the results in subarray_1 and subarray_2??

pyro
12-01-2003, 08:00 AM
The inner foreach() loop is doing that. It first loops through subarray_1, then subarray_2.

jrthor2
12-01-2003, 08:03 AM
so how do I get my results in here, instead of 1,2,a,b?? Sorry, I'm not too good with arrays.

pyro
12-01-2003, 08:13 AM
Since it looks like a simple multidimensional array, you could probably use this:

foreach ($soapresults[0] as $value) {
echo $value."<br>";
}
Assuming that $soapresults is the array.

jrthor2
12-01-2003, 08:18 AM
when I try just that, without your rpevious code, I get

Warning: Invalid argument supplied for foreach()

pyro
12-01-2003, 08:21 AM
Perhaps you need this:

foreach ($soapresults[0][0] as $value) {
echo $value."<br>";
}

jrthor2
12-01-2003, 08:24 AM
I still get the same error

pyro
12-01-2003, 08:28 AM
I'm having a hard time telling what the structure of your array is, for some reason. Are you sure $soapresults is an array? If not, swap it out for the array.

jrthor2
12-01-2003, 08:31 AM
Yes, as I posted before, if I do print_r($soapresults) I get the following (this is just the first result in the array, there are others):

Array
(
[soapVal] => Array
(
[0] => Array
(
[name] => Rite Aid
[store_number] => 04260
[addr1] => 4299 Union Deposit Road
[city] => Harrisburg
[state] => PA
[zip] => 17111
[phone_fe] => (717) 564-6750
[phone_ph] => (717) 564-6750
[phone_phf] => (717) 564-4839
[distance] => 1.23
[convenience_foods] => T
[rite_express] => F
[one_hour_photo] => T
[ra_24_hour] => T
[drive_thru_rx] => T
[rite_rewards] => T
[gnc] => T
[western_union] => T
[hours_fe_mon] => OPEN 24 HOURS
[hours_fe_tue] => OPEN 24 HOURS
[hours_fe_wed] => OPEN 24 HOURS
[hours_fe_thu] => OPEN 24 HOURS
[hours_fe_fri] => OPEN 24 HOURS
[hours_fe_sat] => OPEN 24 HOURS
[hours_fe_sun] => OPEN 24 HOURS
[hours_rx_mon] => OPEN 24 HOURS
[hours_rx_tue] => OPEN 24 HOURS
[hours_rx_wed] => OPEN 24 HOURS
[hours_rx_thu] => OPEN 24 HOURS
[hours_rx_fri] => OPEN 24 HOURS
[hours_rx_sat] => OPEN 24 HOURS
[hours_rx_sun] => OPEN 24 HOURS
[store_date_open] => 1996-08-30
[store_date_relo] => 9999-12-31
[store_date_reopen] => 9999-12-31
[picture_maker] => T
[photo_ramp] => F
[store_status] => 00
[detail_desc] => Located At 4299 Union Deposit Road Across From Comfort Inn
[division] => E
)

}

pyro
12-01-2003, 08:34 AM
Hmm... well, what does this give you?

echo $soapresults[soapval][0][name];

jrthor2
12-01-2003, 08:36 AM
same result

pyro
12-01-2003, 08:40 AM
Oops... Those indecies should probably be quoted. Hopefully this will give you at least something...

echo $soapresults['soapval'][0]['name'];

I was assuming that they would not have explicitly set the indecies, but perhaps they did.

jrthor2
12-01-2003, 08:41 AM
same error result

pyro
12-01-2003, 08:45 AM
Well, I'm not sure what to tell you then... Sorry :(

Apparently, I'm just not reading the results of the array you posted correctly.

jrthor2
12-01-2003, 08:53 AM
can anyone help here???

YoN
12-02-2003, 10:32 AM
Originally posted by pyro
Oops... Those indecies should probably be quoted. Hopefully this will give you at least something...

echo $soapresults['soapval'][0]['name'];

I was assuming that they would not have explicitly set the indecies, but perhaps they did.

maybe changing it to echo $soapresults['soapVal'][0]['name'];
as i see it's not soapval but soapVal that should echo somethng.
also try...



<?php
foreach ($soapresults as $arrkey => $arrval) {
echo "<strong>" . $arrkey . "</strong><br />";
foreach ($arrval as $subkey => $subval) {
echo "<strong>" . $subkey . "</strong><br />"
foreach ($subval as $subsubval) {
echo $subsubval . "<br />"
}
}
echo "<br />";
}
?>

try and see if that helps. report any error.

jrthor2
12-02-2003, 10:41 AM
I saw that the soapVal was nto correct, but that didn't work either. BUT, your new code did work :D. Is there a way to print out beside each value, the name of the value? Example, below is what I get now:

Rite Aid
04287
360 East Main Street
Middletown
PA
17057


What I would like is for it to show something like this:

name: Rite Aid
store_num: 04287
address: 360 East Main Street
city: Middletown
state: PA
zip: 17057

these are the name of each item in the array.

Thanks for your help!!!!

YoN
12-02-2003, 10:50 AM
Great! :D

...change these lines
foreach ($subval as $subsubval) {
echo $subsubval . "<br />";
}

to

foreach ($subval as $subsubkey => $subsubval) {
echo $subsubkey . ": " . $subsubval . "<br />";
}
}

jrthor2
12-02-2003, 10:54 AM
Thanks, works like a charm!!! :D

YoN
12-02-2003, 10:56 AM
Oops.. i just noticed i missed some semicolons in the code i posted first :rolleyes: so, the entire code is:

<?php
foreach ($soapresults as $arrkey => $arrval) {
echo "<strong>" . $arrkey . "</strong><br />";
foreach ($arrval as $subkey => $subval) {
echo "<strong>" . $subkey . "</strong><br />";
foreach ($subval as $subsubkey => $subsubval) {
echo $subsubkey . ": " . $subsubval . "<br />";
}
}
echo "<br />";
}
?>

jrthor2
12-02-2003, 10:58 AM
Yeah, I put them in my code already, thanks again!

YoN
12-02-2003, 10:59 AM
You're welcome :)