You are here: Re: Customizing unserialize output. « PHP Programming Language « IT news, forums, messages
Re: Customizing unserialize output.

Posted by petersprc on 12/25/06 06:21

Hi,

You can use the script below to do this. Pass the serialized data to
the method printResults:

--- Begin Text ---

<?

class YahooResultsView
{
function printResults($rawResponse)
{
$response = unserialize($rawResponse);

$startHtml = $this->htmlEncode(
$response['ResultSet']['firstResultPosition']);
echo "<ol start=\"$startHtml\">";

foreach ($response['ResultSet']['Result'] as $result) {
$clickUrlHtml = $this->htmlEncode($result['ClickUrl']);
$titleHtml = $this->htmlEncode($result['Title']);
$summaryHtml = $this->htmlEncode($result['Summary']);
$displayUrlHtml = $this->htmlEncode(
rtrim($result['DisplayUrl'], '/'));
$sizeHtml = $this->htmlEncode(
$this->formatMemorySize($result['Cache']['Size']));
$cacheUrlHtml = $this->htmlEncode($result['Cache']['Url']);

echo <<<end
<li><div><a class=title href="$clickUrlHtml">$titleHtml</a>
</div>
<div class=summary>$summaryHtml</div>
<span class=url>$displayUrlHtml</span> -
<span class=size>$sizeHtml</span> -
<a class=cache href="$cacheUrlHtml">Cached</a>
end;
}

echo '</ol>';
}

// Convert bytes to the best unit of measurement

function formatMemorySize($bytes)
{
if ($bytes < 0x3E8) {
return $bytes . 'b'; // bytes
} elseif ($bytes < 0xF4240) {
return round($bytes / 0x3E8) . 'k'; // kilobytes
} elseif ($bytes < 0x3B9ACA00) {
return round($bytes / 0xF4240) . 'M'; // megabytes
} elseif ($bytes < 0x3B9ACA00 * 0x3E8) {
return round($bytes / 0x3B9ACA00) . 'G'; // gigabytes
}
return round($bytes / 0x3B9ACA00 * 0x3E8) . 'T'; // terabytes
}

function htmlEncode($str)
{
return htmlentities($str, ENT_QUOTES);
}
}

?>

<html>
<head>
<style>
body {font: 83%/1.2em arial,helvetica,clean,sans-serif;}
#results {width: 600px;}
#results a {color: #0000de;}
#results a:visited {color: #639}
#results a:active {color: #f00;}
#results li {margin: 0 0 17px 21px;}
#results .title {font-size: 120%;}
#results .summary {color: #000;}
#results .url {color: #008000;}
#results .size {color: #8284cc;}
#results .cache {color: #8284cc;}
</style>
</head>
<body>
<div id=results>

<?

$view = new YahooResultsView;
$view->printResults(file_get_contents('response.txt'));

?>

</div>
</body>
</html>

--- End Text ---

thecoolone wrote:
> I am trying to implement the yahoo search api using php.
> I prefer to use PHP serialized to get the result of the search
> displayed.


> I am trying to format this using 2 foreach loop. One for Array second
> for ResultSet and Third for individual Result.
> Example of what im trying:
> foreach($display as $disp)
> {
> $output['title']=$disp;
> echo "$output";
> foreach($disp as $disp2)
> {
> $newobj=$disp2;
> echo "<br>$newobj";
> foreach($disp2 as $key=>$value)
> {
> //$newobj2=$value; // displays [moresearch]
> $newobj3=$value[2];
> echo "<br>$newobj3";
> }}}
> But i am having a hard time getting the output that i want.
>
> Can someone help me format the output got from unserialize() in the
> same way as yahoo displays its result on its main page.
>
> thanks in advance

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация