Page 1 of 1

Confusion about AJAX and OOP php5 code

Posted: Wed Jun 20, 2007 10:12 am
by christus
Okay, I've been trying to build a (relatively) simple ajax app that simply returns values when someone types in a box. There are five files that I use to build this functionality. I'll post the snippets from each file that serve the functionality:

From startPage.php:

Code: Select all

<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>

<p>Suggestions: <span id="txtHint"></span></p>
This goes to the javascript file, which I know is in working order; it returns a value to the html page.

The javascript file references search.ajax.php, which executes this code (a snippet of the main code):

Code: Select all

<?php
$sqlDb->query("query is here");
	
	$ajax = new ajaxScript;
	
	$array = $sqlDb->parseArraySingleField("name");
	$count = $sqlDb->countParseArraySingle("name");
	
//get the q parameter from URL
	$query = $ajax->setParamFromUrl("query");
	
	echo $ajax->returnArrayValuesAsString($array, $query, $count);
?>
I know the query works. This file references two class files, with the following functionality:
ajax.class.php:

Code: Select all

<?php
	include "mssql.class.php";
	
	class ajaxScript {
	
	//get the q parameter from URL
		function setParamFromUrl($param) {
			$get = $_GET[$param];
			return $get;
		}
		
		function returnArrayValuesAsString($array, $query, $count) {
	
			if (strlen($query) > 0) {
  				$hint = "";
				//$hint = "$array[0]";
				//$hint = "$count";
				$count = 30;

  				for($i=0; $i < $count; $i++) {
  					if (strtolower($query) == strtolower(substr($array[$i], 0, strlen($query)))) {
    					if ($hint == "") {
     						$hint = $array[$i];
      					} else {
      						$hint = $hint." , ".$array[$i];
      					}
    				}
  				}
  				
  				if ($hint == "") {
					$response = "no user by this login";
				} else {
					$response = $hint;
				}
			}
			
			return $response;
		}
	}
?>
and mssql.class.php (all class mssql)

Code: Select all

function parseArraySingleField($field) {
			$counter = 0;
			while ($results = $this->fetchArray()) {
				$array[$counter] = $results[$field];
				$counter++;
				
			}
			return $array;
		}
		
		function countParseArraySingle($field) {
			$counter = 0;
			while ($results = $this->fetchArray()) {
				$array[$counter] = $results[$field];
				$counter++;
			}
			return $counter;
		}
All I get is "no user by this login" I'm completely stumped as to what I'm doing wrong here... I'm about to go insane! I've been trying to figure it out for the past day or so. I know this is a long post, but if someone could help me, I would be extremely grateful. I hope I included enough information. Thank you in advance.

Jimmy

Re: Confusion about AJAX and OOP php5 code

Posted: Wed Jun 20, 2007 10:14 am
by christus
Accidentally posted twice

Posted: Wed Jun 20, 2007 12:25 pm
by arturm
what is the purpose of this line:

Code: Select all

if (strtolower($query) == strtolower(substr($array[$i], 0, strlen($query)))) {
?