...

View Full Version : help with php asscocative array



dajohnson1s
03-08-2009, 08:09 PM
I am wanting to have a file that contains an associative array that another file uses as a data source. The data will pull from the database then build the array file.

This is what I would like to make:



<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(
"1"=>"Algeria",
"2"=>"Argentina",

//more data here//

"98"=>"Yemen",
"99"=>"Yugoslavia",
"100"=>"Zimbabwe"
);

foreach ($items as $key=>$value) {
if (strpos(strtolower($value), $q) !== false) {
echo "$value\n";
}
}


?>



I am new to php, and am not used to all the mysql functions. I think if I can get that part down, I should easily be able to just write it all to a file.

Thanks

sea4me
03-08-2009, 08:38 PM
Your trying to intergrate that into the autocomplete thing right?

I will give you the code right now if I was home.... I it already made for my website..

dajohnson1s
03-08-2009, 08:47 PM
sea4me,

Yes, its for autocomplete.

Its going to allow our buildings at the University to be searchable and display on google maps. Otherwise I will have to make a list, and that seemed boring.

If you could help me out, I would appreciated it.

As of right now, I am just echoing everything to see it on the screen. I am running into trouble echoing the first part.



<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(



I figured I could make it into a string, but I get an error when the $ is encountered.

Thanks

sea4me
03-09-2009, 12:43 AM
Here:


<?php
mysql_connect("localhost", "USERNAME HERE", "PASSWORD HERE") or die(mysql_error());
mysql_select_db("INSERT YOUR DB HERE") or die(mysql_error());

$q = strtolower($_GET["q"]);
if (!$q) return;
$query = "SELECT * FROM product";
$sql = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_row($sql)) {
$name = $row[1];

$name = str_replace (" ", "", $name);

if (strpos(strtolower($name), $q) !== false) {
echo "$name\n";
}
}

?>

This assumes that your table is product and your echoing row (2nd field on mySQL table)...

BTW I'm home :)

dajohnson1s
03-09-2009, 02:29 AM
sea4me,

I had tried the code, but can't seem tell if it is working.

Also, it appears to me that this would query the database each time I would like to use the autocomplete, is this true?

Thanks

sea4me
03-09-2009, 11:31 PM
yes...

In case you add something....

sea4me
03-09-2009, 11:33 PM
You can always do a hard-coded one :rolleyes:

Example:


Option 1
Option 2
Option 3
Timesf
DFSFSDF
SDfsdfdsaf
fasdfasf
asdfasdfas
asdfasdfasd
asdfasdf

You would have the page exactly like that if you want a hard-coded one.....

Each new line is a new option :thumbsup:

cmancone
03-10-2009, 02:18 AM
I'm kinda confused... It sounds like you want to use the database to build a list of options, which are saved to a file. Later you would use that file (rather than the database) to print out your options for an autocomplete form. Is that about right? If so, I don't see why you have fetched the query from the 'get' string:


$q = strtolower($_GET["q"]);

If you only need to fetch it from the database once, then it is much simpler to just hard-code the query in. Also, here's a problem:


if (!$q) return;

This is in a file, not a function, so it makes no sense to "return" a value. I would suspect that this would give an error. Even if it doesn't, it's still bad practice. Instead try:



if (!$q)
exit;


Still, the whole point of an autocompleter is to have a different return value depending on the input of the user. So, I'm trying to figure out what you're trying to do here. Do you want a file of all values output from the database, and then everytime the autocompleter asks for its options, do you intend to load those values from the file into php, and search inside those values for the ones that the autocompleter asks for? If so, I would point out that this is the wrong way to do it. Using your database to do the searching will be much more efficient than loading the values into php, and then using php to search for the matching values in the array. Just use your database - that's what its there for. In that case you want to use something like what sea4me posted.

dajohnson1s
03-10-2009, 02:43 AM
To clarify, what I am doing is generating the file from my first post dynamically. Not sure if that is the best way, but most of the data will not be changed for years at a time.

I currently have an autocomplete demo that pulls directly from the database, but it is wicked slow. Turns out, the department server was my boss's old desktop and they just installed ubuntu server on it (not to mention this also serves out 80gb of image to the University's photo sharing app).

So I was doing a little research on speeding up the autocomplete, and it was suggested/ I had read somewhere that I could pull the data from an array, hence the first post.

That array in my first post is queried for the items to complete what the user enters into the textbox.

One of my classes had been canceled today, so I had messed around with it, and this is what I had come up with:


<?php

$conn = mysql_connect("localhost", "", "");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("map")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT common_name
FROM buildings";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus

$File = "country.php";
$Handle = fopen($File, 'w') or die("can't open file");

$Data = "<?php \n";
fwrite($Handle, $Data);
$Data = "\$q = strtolower(\$_GET[\"q\"]); \n";
fwrite($Handle, $Data);

$Data = "if (!\$q) return; \n";
fwrite($Handle, $Data);

$Data = "\$items = array( \n";
fwrite($Handle, $Data);

$i = 1;
while ($row = mysql_fetch_assoc($result)) {
$Data = " \"$i\"=>\"$row[common_name]\" \n";
fwrite($Handle, $Data);
$i++;
}

$Data = "); \n";
fwrite($Handle, $Data);
$Data = "foreach (\$items as \$key=>\$value) { \n";
fwrite($Handle, $Data);
$Data = " if (strpos(strtolower(\$value), \$q) !== false) { \n";
fwrite($Handle, $Data);
$Data = " echo \"\$value\n\"; \n";
fwrite($Handle, $Data);
$Data = " } \n";
fwrite($Handle, $Data);
$Data = "} \n";
fwrite($Handle, $Data);
$Data = "?> \n";
fwrite($Handle, $Data);



print "Data Written";

fclose($Handle);
mysql_free_result($result);

?>



Basically as it stands currently, is I can go to this page, then it generates the info that I need.

Like I said, I am not sure if this is the best way to do this, but I was told that it would be wasteful to query the database each time. But I am open to suggestions.

My plan is to make button or something to have a function do this.

Hope that clears things up.

Edit:
This works, but I have to manually add the comments after each row, is there something I could use to check if there is a last row, then don't add a comma? I have been looking at php functions for a couple of hours now.

Any suggestions would be helpful.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum