function query_bm($ref)
{
    $textRefs = array();
    $db = sparql_connect("http://collection.britishmuseum.org/sparql");
    if (!$db) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/");
    sparql_ns("ecrm", "http://erlangen-crm.org/current/");
    sparql_ns("object", "http://collection.britishmuseum.org/id/object/");
    $sparql = 'SELECT DISTINCT ?coin ?text WHERE {
  ?coin ecrm:P70i_is_documented_in <REF> . FILTER regex(str(?coin), "C[A-Z]{2}[0-9]+") .
  ?coin a ecrm:E22_Man-Made_Object ;
        ecrm:P50_has_current_keeper <http://collection.britishmuseum.org/id/thesauri/department/C> ;
        bmo:PX_display_wrap ?text . FILTER regex(?text, "Bibliograpic\\\\sreference")
  } LIMIT 10';
    $result = sparql_query(str_replace('REF', $ref, $sparql));
    if (!$result) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    $fields = sparql_field_array($result);
    while ($row = sparql_fetch_array($result)) {
        $textRefs[$row['coin']][] = $row['text'];
        /*foreach( $fields as $field )
        		{
        			$textRefs
        		}*/
    }
    return $textRefs;
}
function query_bm($writer, $uri)
{
    $db = sparql_connect("http://collection.britishmuseum.org/sparql");
    if (!$db) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    sparql_ns("thesDimension", "http://collection.britishmuseum.org/id/thesauri/dimension/");
    sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/");
    sparql_ns("ecrm", "http://erlangen-crm.org/current/");
    sparql_ns("object", "http://collection.britishmuseum.org/id/object/");
    $sparql = "SELECT ?image ?weight ?axis ?diameter ?objectId ?hoard WHERE {\n  OPTIONAL {<OBJECT> bmo:PX_has_main_representation ?image }\n  OPTIONAL { <OBJECT> ecrm:P43_has_dimension ?wDim .\n           ?wDim ecrm:P2_has_type thesDimension:weight .\n           ?wDim ecrm:P90_has_value ?weight}\n  OPTIONAL {\n     <OBJECT> ecrm:P43_has_dimension ?wAxis .\n           ?wAxis ecrm:P2_has_type thesDimension:die-axis .\n           ?wAxis ecrm:P90_has_value ?axis\n    }\n  OPTIONAL {\n     <OBJECT> ecrm:P43_has_dimension ?wDiameter .\n           ?wDiameter ecrm:P2_has_type thesDimension:diameter .\n           ?wDiameter ecrm:P90_has_value ?diameter\n    }\n  OPTIONAL {\n     <OBJECT> ecrm:P1_is_identified_by ?identifier.\n     ?identifier ecrm:P2_has_type <http://collection.britishmuseum.org/id/thesauri/identifier/codexid> ;\n        rdfs:label ?objectId\n    }\n  OPTIONAL {\n     <OBJECT> bmo:PX_display_wrap ?hoard . FILTER regex(?hoard, 'IGCH')\n    }\n  }";
    $result = sparql_query(str_replace('OBJECT', $uri, $sparql));
    if (!$result) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    $fields = sparql_field_array($result);
    while ($row = sparql_fetch_array($result)) {
        foreach ($fields as $field) {
            if (strlen($row[$field]) > 0) {
                if ($field == 'hoard') {
                    preg_match('/IGCH\\s([0-9]+)/', $row[$field], $matches);
                    if (isset($matches[1])) {
                        $num = str_pad($matches[1], 4, "0", STR_PAD_LEFT);
                        $hoardURI = 'http://coinhoards.org/id/igch' . $num;
                        echo "Found hoard {$hoardURI}\n";
                        $writer->startElement('dcterms:isPartOf');
                        $writer->writeAttribute('rdf:resource', $hoardURI);
                        $writer->endElement();
                    }
                } else {
                    switch ($field) {
                        case 'image':
                            $writer->startElement('foaf:depiction');
                            $writer->writeAttribute('rdf:resource', $row[$field]);
                            $writer->endElement();
                            break;
                        case 'objectId':
                            $writer->startElement('foaf:homepage');
                            $writer->writeAttribute('rdf:resource', "http://www.britishmuseum.org/research/collection_online/collection_object_details.aspx?objectId={$row[$field]}&partId=1");
                            $writer->endElement();
                            break;
                        case 'axis':
                            $writer->startElement('nmo:hasAxis');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#integer');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                        case 'weight':
                            $writer->startElement('nmo:hasWeight');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                        case 'diameter':
                            $writer->startElement('nmo:hasDiameter');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                    }
                }
            }
        }
    }
}
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css"  href="css/style.css" />
		<title>Estados Brasileiros</title>
	</head>

	<body>
        <h1>Estados Brasileiros</h1>
        <h3>Dados trazidos de <a href="http://dbpedia.org/">dbpedia.org</a></h3>
		<?php 
require_once "sparqllib.php";
$db = sparql_connect("http://dbpedia.org/sparql/");
if (!$db) {
    print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>";
    exit;
}
sparql_ns("foaf", "http://xmlns.com/foaf/0.1/");
$sparql = "select distinct ?Estado ?Populacao where {\n                ?estados <http://dbpedia.org/ontology/type> <http://dbpedia.org/resource/States_of_Brazil>. \n                ?estados <http://dbpedia.org/property/name> ?Estado. \n                ?estados <http://dbpedia.org/property/populationTotal> ?Populacao.\n                } ORDER BY DESC(?Populacao)";
$result = sparql_query($sparql);
if (!$result) {
    print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>";
    exit;
}
$fields = sparql_field_array($result);
print "<table>";
print "<caption>População dos Estados brasileiros</caption>";
print "<tr>";
foreach ($fields as $field) {
    print "<th scope=\"col\">{$field}</th>";
}
while ($row = sparql_fetch_array($result)) {
    print "<tr>";
/**** GET ALL NUMISMATIC REFERENCE WORKS FROM THE BM VIA SPARQL *****/
require_once "sparqllib.php";
//initiate fiel
$fp = fopen('refs.csv', 'w');
//initiate SPARQL query
$db = sparql_connect("http://collection.britishmuseum.org/sparql");
if (!$db) {
    print sparql_errno() . ": " . sparql_error() . "\n";
    exit;
}
sparql_ns("bibo", "http://purl.org/ontology/bibo/");
sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/");
sparql_ns("ecrm", "http://erlangen-crm.org/current/");
sparql_ns("object", "http://collection.britishmuseum.org/id/object/");
sparql_ns("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
sparql_ns("skos", "http://www.w3.org/2004/02/skos/core#");
$sparql = 'SELECT DISTINCT ?ref ?title ?author ?id WHERE {
  ?ref a ecrm:E31_Document ;
         skos:prefLabel ?title ;
         bibo:identifier ?id ;
         ecrm:P94i_was_created_by ?pub .
  ?pub ecrm:P2_has_type <http://collection.britishmuseum.org/id/thesauri/production/authoring> ;
       ecrm:P14_carried_out_by ?auth .
  ?auth rdfs:label ?author .
  ?coin ecrm:P70i_is_documented_in ?ref ;
        a ecrm:E22_Man-Made_Object ;
		bmo:PX_object_type <http://collection.britishmuseum.org/id/thesauri/x6089> ;
        ecrm:P50_has_current_keeper <http://collection.britishmuseum.org/id/thesauri/department/C> 
  } ORDER BY ?title';
$result = sparql_query($sparql);
if (!$result) {