Merging two PHP scripts puzzler
Posted: Sun Jan 04, 2009 9:59 am
I'm fairly new to PHP.
I've set up an AJAX autocomplete using jQuery. Currently this looks up the info from a php script (below) the array is entered directly in the PHP script - it works but is not ideal.
So I did a bit of research and figured out how I can extract the info that I need from my MySQL db and present this in an array.
Both work fine separately but when I try and merge the two I can't seem to get it to work. I want the array in the first script to be provided from the MySQL table.
Here is my latest non functioning attempt...
I've been semi-blindly twiddling stuff about and thinking I've got it but am having no joy
I really don't have enough experience to know if I'm just a small syntax wrong or are completely out of the ballpark.
If anyone spots an obvious mistake (or 2 or probably more!) I'd really appreciate the help/feedback.
Many thanks in advance
I've set up an AJAX autocomplete using jQuery. Currently this looks up the info from a php script (below) the array is entered directly in the PHP script - it works but is not ideal.
Code: Select all
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(
"Quentin <em>Tarantino</em>"=>"1",
"Christopher <em>Nolan</em>"=>"2",
"Joel <em>Coen</em>"=>"3",
"Ethan <em>Coen</em>"=>"4"
);
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
?>Code: Select all
<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT first_names, surname, id FROM writers";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['first_names']} <br>" .
"Surname : {$row['surname']} <br>" .
"ID : {$row['id']} <br><br>";
}
include 'closedb.php';
?>Here is my latest non functioning attempt...
Code: Select all
<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT first_names, surname, id FROM writers";
$result = mysql_query($query);
if (!$query) return;
while($items = array(
"{$row['first_names']}"."{$row['surname']}"=>"{$row['id']}",
));
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $query) !== false) {
echo "$key|$value\n";
}
include 'closedb.php';
}I really don't have enough experience to know if I'm just a small syntax wrong or are completely out of the ballpark.
If anyone spots an obvious mistake (or 2 or probably more!) I'd really appreciate the help/feedback.
Many thanks in advance