Google Suggest AJAX w/o SQL from ASP to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darryladie
Forum Commoner
Posts: 62
Joined: Thu Mar 02, 2006 6:14 pm
Location: East Sussex, UK

Google Suggest AJAX w/o SQL from ASP to PHP

Post by darryladie »

Hi,

I am trying to create a clone of Google Suggest for my website. I am trying to use the code from w3schools.com to make it.

The tutorial gives details in ASP which I am not very familiar with. My PHP is a little sketcy but I have done my best to try and convert the ASP to PHP but I am stuck was wondering whether there was anyone who could help me?

I have got the XHTML, CSS and Javascript sussed but I cant quite seem to manage this!

I think it's mostly getting the syntax for the while and if statements right but I could be wrong.

Thanks in advance,

Darryl

ASP

Code: Select all

dim a(30)
a(1)="Anna"
a(2)="Brittany"
a(3)="Cinderella"
a(4)="Diana"
a(5)="Eva"

q=request.querystring("q")

if len(q)>0 then
  hint=""
  for i=1 to 30
    x1=ucase(mid(q,1,len(q)))
    x2=ucase(mid(a(i),1,len(q)))
    if x1=x2 then
      if hint="" then
        hint=a(i)
      else
        hint=hint & " , " & a(i)
      end if
    end if
  next
end if 
if hint="" then 
  response.write("no suggestion")
else
  response.write(hint)
end if
PHP... so far!

Code: Select all

<?php $a = array('Fred', 'Bob', 'Bill'); ?>
<?php $q = $_GET['q'] ?>
<?php
$i = '0';
echo $hint;
if (strlen($q) > 0)
  while( $i <= count($a))
  {
  echo $a[$i];
	$x1 = strtoupper(substr($q,'1',strlen($q)));
    $x2 = strtoupper(substr($a[$i],'1',strlen($q)));
	  if ($x1 = $x2)
	  $hint = $hint && $a[$i];
          echo $hint;
	  else
	  echo $hint;
  $i++;
  }
else
$hint = '';
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

darryladie
Forum Commoner
Posts: 62
Joined: Thu Mar 02, 2006 6:14 pm
Location: East Sussex, UK

Post by darryladie »

Thanks very much for the replies guys.

I have read the second of these articles already during my initial research, the first was a new to me I must admit.

However, both still implement SQL, I am keen to replace the use of an SQL database with an array in PHP as shown in the original ASP code that I have tried to convert to PHP.

If someone cannot convert it to PHP then maybe they could give me a better idea of what the string functions are doing in the ASP:

Code: Select all

x1=ucase(mid(q,1,len(q)))
    x2=ucase(mid(a(i),1,len(q)))
I understand that it converts to uppercase and the collects a substring. But I am not sure how it uses this to compare the two strings and then give suggestions afterwards.

Thank you again,

Darryl.
Post Reply