Jump to content

Auto Complete Ajax php strange bug :confused: :confused:


Recommended Posts

i have the following script from a demo that works:

if i change the query to my db it only works with city_id as a number (response is [] for city_name

//sql:
CREATE TABLE `sks_color` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=14 ;




<?php

$link = mysql_connect('localhost', 'dbUsername', 'dbPassword');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("database")) {
   echo "Unable to select mydbname: " . mysql_error();
   exit;
}

$result = mysql_query("SELECT name FROM  sks_color");
while ($row = mysql_fetch_assoc($result)) {
   		$colors[]=$row['name'];
}
mysql_free_result($result);
mysql_close($link);

// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
// initialize the results array
$results = array();

// search colors
foreach($colors as $color)
{
	// if it starts with 'part' add to results
	if( strpos($color, $_GET['part']) === 0 ){
		$results[] = $color;
	}
}

// return the array as json with PHP 5.2
echo json_encode($results);
}
?>

 

my chamges

//sql
CREATE TABLE `citylist` (
  `city_id` int(11) NOT NULL AUTO_INCREMENT,
  `country_id` int(11) NOT NULL,
  `city_name` varchar(200) NOT NULL,
  PRIMARY KEY (`city_id`),
  KEY `city_id` (`city_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=616 ; 

<?php

$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("international")) {
   echo "Unable to select mydbname: " . mysql_error();
   exit;
}

$result = mysql_query("SELECT city_name FROM citylist");
while ($row = mysql_fetch_assoc($result)) {
   		$colors[]=$row['city_name'];
}
mysql_free_result($result);
mysql_close($link);

// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
// initialize the results array
$results = array();

// search colors
foreach($colors as $color)
{
	// if it starts with 'part' add to results
	if( strpos($color, $_GET['part']) === 0 ){
		$results[] = $color;
	}
}

// return the array as json with PHP 5.2
echo json_encode($results);
}
?>

:confused: :confused:

Is it the $_GET['part'] you are searching for inside your loop for every iteration? Add the regexp inside the loop where you loop through the citites. And better option for what I suggested earlier is to use the $_GET['part'] in incasesensitive match.

 

if (preg_match('/'. $_GET['part'] .'/i', $stringToSearch))
{
    // found
}

Is it the $_GET['part'] you are searching for inside your loop for every iteration? Add the regexp inside the loop where you loop through the citites. And better option for what I suggested earlier is to use the $_GET['part'] in incasesensitive match.

 

if (preg_match('/'. $_GET['part'] .'/i', $stringToSearch))
{
    // found
}

 

adding it to the check parameter part doesn't work?

// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '' and preg_match('/'. $_GET['part'] .'/i', $row['city_name']))

This is code I just tested out, works just fine. I added also '^' in the start of the pattern and '$' sign in the end so it will match exactly to a string like 'Amsterdam' but not to string 'XAmsterdam' etc.

 

$test = array('city_name' => 'AMSterdam');

if(isset($_GET['part']) && $_GET['part'] != '' && preg_match('/^'. $_GET['part'] .'$/i', $test['city_name']))
{
echo 'TRUE';
}
else
{
echo 'FALSE';
}

echo '<a href="?part=amsterdam">TEST</a>';

This is code I just tested out, works just fine. I added also '^' in the start of the pattern and '$' sign in the end so it will match exactly to a string like 'Amsterdam' but not to string 'XAmsterdam' etc.

 

$test = array('city_name' => 'AMSterdam');

if(isset($_GET['part']) && $_GET['part'] != '' && preg_match('/^'. $_GET['part'] .'$/i', $test['city_name']))
{
echo 'TRUE';
}
else
{
echo 'FALSE';
}

echo '<a href="?part=amsterdam">TEST</a>';

 

yep your test works fine but i still get nothing with the part i added  to the check parameter , empty array

You should not be retrieving all the rows from your database table and then loop through the data trying to match it. You should be doing this in your query using a WHERE clause with a LIKE comparison.

You should not be retrieving all the rows from your database table and then loop through the data trying to match it. You should be doing this in your query using a WHERE clause with a LIKE comparison.

 

could you guide me on how to implement it with the ajax?

You are already using ajax to request and display the information. What I suggested was how to implement the server side query so that you only retrieve the data you want in the most efficient manor.

 

Untested (of course), but should work -

<?php
if(isset($_GET['part']) and $_GET['part'] != '')
{
mysql_connect('localhost', 'root', 'root') or die('Could not connect: ' . mysql_error());
mysql_select_db("international") or die('Unable to select mydbname: ' . mysql_error());
$results = array();
$part = mysql_real_escape_string($_GET['part']);
$result = mysql_query("SELECT city_name FROM citylist WHERE city_name LIKE '$part%'");
while ($row = mysql_fetch_assoc($result)) {
   		$results[]=$row['city_name'];
}
echo json_encode($results);
}
?>

You are already using ajax to request and display the information. What I suggested was how to implement the server side query so that you only retrieve the data you want in the most efficient manor.

 

Untested (of course), but should work -



great thanks
<?php
if(isset($_GET['part']) and $_GET['part'] != '')
{
mysql_connect('localhost', 'root', 'root') or die('Could not connect: ' . mysql_error());
mysql_select_db("international") or die('Unable to select mydbname: ' . mysql_error());
$results = array();
$part = mysql_real_escape_string($_GET['part']);
$result = mysql_query("SELECT city_name FROM citylist WHERE city_name LIKE '$part%'");
while ($row = mysql_fetch_assoc($result)) {
   		$results[]=$row['city_name'];
}
echo json_encode($results);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.