twsowerby Posted February 15, 2009 Share Posted February 15, 2009 Hey guys, I would like to take an array returned from an access class and style the data so it looks good. Currently I get this: Array ( [576113096] => Array ( [name] => Nobues [skillintraining] => Fighters [skilllevel] => IV [skillTime] => Array ( [note] => [days] => 2 [hours] => 5 [minutes] => 53 [seconds] => 40 ) [WalletAmount] => 65,508,812.57 ISK ) [1404986087] => Array ( [name] => Lireil Crow [skillintraining] => Astrometric Triangulation [skilllevel] => IV [skillTime] => Array ( [note] => [days] => 1 [hours] => 3 [minutes] => 21 [seconds] => 32 ) [WalletAmount] => 9,177,531.37 ISK ) [217317000] => Array ( [name] => Hansoloo [skillintraining] => Amarr Carrier [skilllevel] => V [skillTime] => Array ( [note] => [days] => 60 [hours] => 22 [minutes] => 26 [seconds] => 33 ) [WalletAmount] => 65,100.15 ISK ) [689240734] => Array ( [name] => Mung Lore [skillintraining] => Torpedoes [skilllevel] => V [skillTime] => Array ( [note] => [days] => 8 [hours] => 9 [minutes] => 30 [seconds] => 3 ) [WalletAmount] => 194,399.22 ISK ) ) Obviously that isnt very nice to look at, how would i go about separating each bit of data to style and position? Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/ Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 People usually loop through array items and display appropriate HTML for each. Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762757 Share on other sites More sharing options...
twsowerby Posted February 15, 2009 Author Share Posted February 15, 2009 I thought that too but keep getting an error. The code im using to connectt o the access class and return the array is as follows: <?php $vars = array('user'=>'demo', 'password'=>'demo', 'skey'=>'abc123', 'request'=>'Dashboard', 'userID'=>'2'); $data = apiGetD($vars); function apiGetD($vars) { $accessURL = 'www.eve-tool.com'; $apiURL = '/api/class/class.access.php'; $poststring = http_build_query($vars, '', '&'); $fp = fsockopen($accessURL, 80); fputs($fp, "POST " . $apiURL . " HTTP/1.0\r\n"); fputs($fp, "Host: " . $accessURL . "\r\n"); fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); fputs($fp, "User-Agent: PHPApi\r\n"); fputs($fp, "Content-Length: " . strlen($poststring) . "\r\n"); fputs($fp, "Connection: close\r\n\r\n"); if (strlen($poststring) > 0) fputs($fp, $poststring . "\r\n"); $contents = ""; while (!feof($fp)) { $contents .= fgets($fp); } fclose($fp); $start = strpos($contents, "\r\n\r\n"); if ($start !== false) { return substr($contents, $start + strlen("\r\n\r\n")); } } foreach($data as $key=>$value) { echo $key.": ".$value; } Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\test.php on line 50 is what i get. If I remove the foreach at the bottom and just print $data i get the array data back fine. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762759 Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 The apiGetD() function returns a string not an array... Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762763 Share on other sites More sharing options...
twsowerby Posted February 15, 2009 Author Share Posted February 15, 2009 Ok, how would i change it to return an array? I know that the function that it connects to with fsockopen returns an array so somewhere along the line its becoming a string but im not sure where. Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762768 Share on other sites More sharing options...
twsowerby Posted February 15, 2009 Author Share Posted February 15, 2009 This is the function that apiGetD() connects to: Function GetDashboard($sessionID, $user, $dbc, $api, $dataBase){ $userChars = GetAPIKeys($sessionID, $user, $dbc); if(!count($userChars) < 1){ foreach($userChars as $key => $value){ if($value["info"]["skilllevel"] > 0){ $value["info"]["skilllevel"] = SkillLevel($value["info"]["skilllevel"]); $tqTime = strtotime($value["info"]["evetime"]); $endTime = strtotime($value["info"]["skillTime"]); $timeLeft = TimeLeft($endTime, $tqTime); $value["return"]["name"] = $value["details"]["name"]; $value["return"]["skillintraining"] = $api->GetTypeName($value["info"]["skillintraining"], $dataBase); $value["return"]["skilllevel"] = $value["info"]["skilllevel"]; if($ft < 0){ $value["return"]["skillTime"]["note"] = "Finished"; $value["return"]["skillTime"]["days"] = abs($timeLeft['days']); $value["return"]["skillTime"]["hours"] = abs($timeLeft['hours']); $value["return"]["skillTime"]["minutes"] = abs($timeLeft['minutes']); $value["return"]["skillTime"]["seconds"] = abs($timeLeft['seconds']); } else { $value["return"]["skillTime"]["note"] = ""; $value["return"]["skillTime"]["days"] = $timeLeft['days']; $value["return"]["skillTime"]["hours"] = $timeLeft['hours']; $value["return"]["skillTime"]["minutes"] = $timeLeft['minutes']; $value["return"]["skillTime"]["seconds"] = $timeLeft['seconds']; } $value["return"]["WalletAmount"] = number_format($value["details"]["balance"],2)." ISK"; $returnArray[$key] = $value["return"]; } } } return $returnArray; } Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762784 Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 I think you should modify your API (if it's yours off course ) so that it sends data in JSON or XML. These are easier to parse. Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762798 Share on other sites More sharing options...
sasa Posted February 15, 2009 Share Posted February 15, 2009 try <?php $test = 'Array ( [576113096] => Array ( [name] => Nobues [skillintraining] => Fighters [skilllevel] => IV [skillTime] => Array ( [note] => [days] => 2 [hours] => 5 [minutes] => 53 [seconds] => 40 ) [WalletAmount] => 65,508,812.57 ISK ) [1404986087] => Array ( [name] => Lireil Crow [skillintraining] => Astrometric Triangulation [skilllevel] => IV [skillTime] => Array ( [note] => [days] => 1 [hours] => 3 [minutes] => 21 [seconds] => 32 ) [WalletAmount] => 9,177,531.37 ISK ) [217317000] => Array ( [name] => Hansoloo [skillintraining] => Amarr Carrier [skilllevel] => V [skillTime] => Array ( [note] => [days] => 60 [hours] => 22 [minutes] => 26 [seconds] => 33 ) [WalletAmount] => 65,100.15 ISK ) [689240734] => Array ( [name] => Mung Lore [skillintraining] => Torpedoes [skilllevel] => V [skillTime] => Array ( [note] => [days] => 8 [hours] => 9 [minutes] => 30 [seconds] => 3 ) [WalletAmount] => 194,399.22 ISK ) )'; $test = preg_replace('/\( *\[/','([' ,$test); $test = preg_replace('/=> ([^\[)]*) *([\[)])/','=> \'\1\' \2', $test); $test = str_replace("'Array (' ", 'Array (', $test); $test = preg_replace('/ \[/', ', [',$test); $test = preg_replace('/[\[\]]/','\'',$test); eval('$out = '.$test.';'); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145296-styling-an-array-returned-from-an-access-class/#findComment-762925 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.