Стил на API-та

Msecurity

Registered
Здравейте колеги...
Как на резултата от дадено API да му задам стил... и да заприлича на нещо..
Ето примерна информация която се визуализира...


Код:
<result>
<row>
<label>България</label>
<nb_uniq_visitors>34</nb_uniq_visitors>
<nb_visits>38</nb_visits>
<nb_actions>313</nb_actions>
<max_actions>52</max_actions>
<sum_visit_length>16691</sum_visit_length>
<bounce_count>9</bounce_count>
<nb_visits_converted>0</nb_visits_converted>
<code>bg</code>
<logo>plugins/UserCountry/images/flags/bg.png</logo>
<logoWidth>16</logoWidth>
<logoHeight>11</logoHeight>
</row>
</result>
 
C XSL?

http://www.w3schools.com/xsl/xsl_transformation.asp

Мисля, че ефектът е този който търсиш- http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml
 
Отвратителна работа е това с API да се занимаваш - но много ефективна!!! :)

За да зададеш определен стил на API-то трябва да изведеш данните да ги и да ги преведеш в PHP и след това да си ги нагласиш както искаш ти.

Ако не се ориентираш мога да ти извадя един пример който с помощта на колега от форума спретнахме.
Тъй като аз не съм запознат с Back end-а много благодаря за съпорта му.
 
Мога да изведа резултатите и в PHP ... но там ми е още по сложно всичко... ето с PHP ...

PHP:
a:1:{i:0;a:20:{s:5:"label";s:6:"/index";s:9:"nb_visits";i:50;s:16:"nb_uniq_visitors";i:45;s:7:"nb_hits";i:59;s:14:"sum_time_spent";i:2711;s:28:"nb_hits_with_time_generation";s:2:"54";s:19:"min_time_generation";s:4:"0.02";s:19:"max_time_generation";s:6:"17.175";s:22:"entry_nb_uniq_visitors";s:2:"40";s:15:"entry_nb_visits";s:2:"45";s:16:"entry_nb_actions";s:3:"252";s:22:"entry_sum_visit_length";s:4:"9270";s:18:"entry_bounce_count";s:2:"14";s:21:"exit_nb_uniq_visitors";s:2:"19";s:14:"exit_nb_visits";s:2:"19";s:16:"avg_time_on_page";d:54;s:11:"bounce_rate";s:3:"31%";s:9:"exit_rate";s:3:"38%";s:19:"avg_time_generation";d:0.64900000000000002131628207280300557613372802734375;s:3:"url";s:23:

@GOTI ако може да ми дадеш пример ще съм ти много благодарен, поне да се ориентирам :)
 
Ето го кода който извежда резултатите от API-то


PHP:
<?php
header('content-type:text/html;charset=utf-8');
function getApiData($url, $cacheFile) {
	$cacheTime = is_file($cacheFile) ? filemtime($cacheFile) : 0;
	$diff = time() - $cacheTime;
	if($diff < 60*60) {
		$results = file_get_contents($cacheFile);
	} else {
		$results = file_get_contents($url);
		file_put_contents($cacheFile, $results);
	}
	$results = json_decode($results, true);
	if($results['status']) {
		return $results['result'];
	} else {
		return array();
	}
}

function getCollections() {
	$collectionsUrl = "URL-na-API-to";
	$cacheFile = 'collecitons.json';
	return getApiData($collectionsUrl, $cacheFile);
}

function getCollectionProducts(array $collection) {
	return getApiData($collection['api_products'], 'collection_'.$collection['id'].'.json');
}

$_GET['collection'] = 88; // тип на колекцията



$collections = getCollections();

if(isset($_GET['collection']) && $_GET['collection']) {

	$collection = false;
	foreach($collections as $c) {
		if($c['id'] == $_GET['collection']) {
			$collection = $c;
			break;
		}
	}
	$collectionProducts = getCollectionProducts($c);
	foreach($collectionProducts as $product) {

	}
} else {

	foreach($collections as $collection) {
		echo '<a href="?collection='.$collection['id'].'">'.$collection['title'].' discount: '.$collection['discount'].'%</a><br/>';
	}
}
?>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Demo Api</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

</head>

<body>
	<div id="w">
		<h1>Demo API</h1>
		
		<div id="products" class="clearfix">
		<?php
                foreach($collectionProducts as $product) {
                  //var_dump($product);
                  //break;
                        echo "<div class=\"product\">\n";
                        echo "<h2><a href=\"".$product['url']."\" target=\"_blank\">".$product['product_name']."</a></h2>\n";
                        echo "<center><a href=\"".$product['url']."\" target=\"_blank\"><img src=\"".$product['main_image']['thumbnail']."\" alt=\"".$product['product_name']." featured image\" class=\"thumb\"></a></center>\n";
                        echo "<p>Цена: <span class=\"price\">$".$product['regular_price']."</span></p>\n";
                        echo "<p>Количество: ".$product['quantity']."</p>\n";
                        echo "</div>\n";
                }
				
                ?>
		
		</div>
	</div>
</body>
</html>

Ето и стилизирането



[css]html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
outline: none;
}
html { height: 101%; }
body { font-size: 62.5%; line-height: 1; font-family: Arial, Tahoma, Verdana, sans-serif; }

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }

blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }

table { border-collapse: collapse; border-spacing: 0; }
img { border: 0; max-width: 100%; }

h1 { font-family: "Galindo", Tahoma, Arial, sans-serif; font-weight: normal; font-size: 2.6em; line-height: 1.4em; color: #565656; margin-bottom: 10px; }
h2 { font-family: "Galindo", Tahoma, Arial, sans-serif; font-weight: normal; }

p { font-size: 1.1em; color: #555; line-height: 1.25em; margin-bottom: 11px; }

#w { max-width: 950px; min-width: 720px; padding-top: 15px; margin: 0 auto; background: #fff; }

#products { display: block; padding: 5px 9px; }
#products .product {
display: block;
float: left;
box-sizing: border-box;
border: 1px solid #fff;
padding: 4px 5px;
width: 220px;
margin-right: 6px;
margin-bottom: 20px;
height: 260px;
overflow: hidden;
}
#products .product:hover { border-color: #bcbcbc; background: #f5f5f5; }

#products .product h2 { font-size: 1.4em; line-height: 1.25em; margin-bottom: 3px; text-shadow: 1px 1px 0px #fff; }
#products .product h2 a { display: block; text-decoration: none; color: #536ba8; }
#products .product h2 a:hover { background: #d2dcf6; }

#products .product p { font-weight: bold; color: #666; margin-bottom: 3px; }
#products .product .price { color: #629c5f; }


#products .product .thumb {
display: block;
padding: 2px;
margin-bottom: 5px;
background: #fff;
box-shadow: 1px 1px 1px rgba(0,0,0,0.4);
-moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);
-webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);
}

.clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; }
.clearfix { display: inline-block; }

html[xmlns] .clearfix { display: block; }
* html .clearfix { height: 1%; }[/css]

Не е нещо голямо, но пак казвам - Back end-a за мен е сложно :)
Успех :)
 
Код:
<pre>
Каквото искаш тук
</pre>
Не разбрах много идеята, но пробвай това? На мен лично доста ми помага.
 

Back
Горе