Het werkt toch niet op een of andere manier. Komen ze op deze manier precies zo onder elkaar te staan?
Misschien was het toch niet zo eenvoudig als ik dacht.. dit is het hele script:
<?php
/*
note:
this is just a static test version using a hard-coded countries array.
normally you would be populating the array out of a database
the returned xml has the following structure
<results>
<rs>foo</rs>
<rs>bar</rs>
</results>
*/
include "../connect.php";
$sql = "SELECT * FROM artists";
$query = mysql_query($sql);
$aUsers = array ();
while($data = mysql_fetch_array($query)){
array_push ($aUsers, $data['first_name'] . ' ' . $data['last_name']);
}
/*
$aUsers = array(
"Voornaam Achternaam",
"Voornaam Achternaam",
"Voornaam Achternaam",
"Voornaam Achternaam",
"Voornaam Achternaam",
"Voornaam Achternaam",
"Voornaam Achternaam"
);
*/
$input = strtolower( $_GET['input'] );
$len = strlen($input);
$aResults = array();
if ($len)
{
for ($i=0;$i<count($aUsers);$i++)
{
// had to use utf_decode, here
// not necessary if the results are coming from mysql
//
if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
//if (stripos(utf8_decode($aUsers[$i]), $input) !== false)
// $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
}
}
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json']))
{
header("Content-Type: application/json");
echo "{\"results\": [";
$arr = array();
for ($i=0;$i<count($aResults);$i++)
{
$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
}
echo implode(", ", $arr);
echo "]}";
}
else
{
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
for ($i=0;$i<count($aResults);$i++)
{
echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
}
echo "</results>";
}
?>
Er zit nog een .js bestand bij maar volgens mij heeft die hier geen invloed op. Op een andere pagina heb ik gewoon dit staan:
<input type="text" id="testinput" />
EDIT
En dit staat onderaan die pagina
<script type="text/javascript">
var options = {
script:"artiesten.php?json=true&",
varname:"input",
json:true,
callback: function (obj) { document.getElementById('testid').value = obj.id; }
};
var as_json = new AutoSuggest('testinput', options);
var options_xml = {
script:"artiesten.php?",
varname:"input"
};
var as_xml = new AutoSuggest('testinput_xml', options_xml);
</script>
Wanneer ik niets wijzig werkt het allemaal. Dus typ ik een A dan geeft hij de rest van de naam. Maar wanneer ik het uit de database probeer te halen doet hij niets meer. Ook niet meer 'no result'.
Als je iets print (echo mysql_num_rows....) en je roept daarna de header(...) functie aan, krijg je inderdaad de foutmelding headers already send.
De headers worden namelijk verzonden naar de browser wanneer je iets output.
Als je je test echo mysql_num_rows uitcomment, zijn de overige foutmeldingen qua headers weer verdwenen.