homepage Welcome to WebmasterWorld Guest from 107.20.25.215
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
post array vs. query array - comparing
crackpipe




msg:4000364
 9:49 am on Oct 3, 2009 (gmt 0)

Different types of associative arrays apparently arise from POSTing and from mysql queries. Let's say I explode a data set from a POST operation in a form, perhaps a set of tags. My resulting array, after sanitizing them, might be:

[0] => trees
[1] => redwoods
[2] => evergreens

Now I want to compare these POSTed tags with existing tags in a MySQL table. I do a fairly typical operation, selecting the 'Tags' table and making it into an array:

$result= mysql_query ("SELECT * FROM Tags") or die(mysql_error());
for($i = 0; $sqlarray[$i] = mysql_fetch_assoc($result); $i++) ;

This will leave me with a multidimensional array that has both my tag ID's (which I will need later), and the text of tag. This array might look like this:

(
[0] => Array
(
[tag_id] => 35
[tag_text] => lawncover
)

[1] => Array
(
[tag_id] => 36
[tag_text] => redwoods
)

[2] => Array
(
[tag_id] => 37
[tag_text] => tacobell
)
)

And so forth. So I want to compare values in the two arrays and find the common "redwoods" value, but the arrays are in these different formats, as a natural result of a mysql query (multidimensional). This means the comparison tools array_interesect and array_diff don't seem to work. I've been looking around and wondered what people here have done to overcome this in order to successfully compare values in different array formats. Thanks.

 

jd01




msg:4000400
 11:42 am on Oct 3, 2009 (gmt 0)

There's a number of different ways, but you could probably do something like this fairly efficiently: (Assuming your top array is $needle.)

for($i = 0; $sqlarray[$i] = mysql_fetch_assoc($result); $i++) {
for($ii=0; $ii<count($needle); $ii++) {
if(stripos($sqlarray[$i][tag_text],$needle[$ii])) {
echo "found"; break;
}
}
}

I'm not sure about your need to match case or not, so you could theoretically use == or strpos if you know the case will be the same.

The above should work or be close.

crackpipe




msg:4000496
 5:48 pm on Oct 3, 2009 (gmt 0)

Thanks jd01. Placing a count on the first array, the array you called "needle" was significant. Probably due to noob coding skills, I had been lost in foreach failures for a couple of days. This solution worked quickly and I pretty much see why. I'll attempt to use count in another portion of this project.

I didn't appear to need strpos as I had already lowercased. So, the entire solution, coming from the the posted tags and displaying any matches, worked something like this:

//from POST action
$formarray=strtolower ($_POST['tags']);
$formarray=ltrim ($formarray);
$formarray=explode (" ", $formarray);

//from MySQL query
$dbhandle = mysql_connect(localhost, foo)or die("Unable to connect to MySQL");
$selected = mysql_select_db("foo",$dbhandle) or die("Could not select database");
$result= mysql_query ("SELECT * FROM Tags") or die(mysql_error());

// Put MYSQL tags in array, commpare with POSTed tags
for($i = 0; $sqlarray[$i] = mysql_fetch_assoc($result); $i++) {
for ($ii=0;$ii<count($formarray); $ii++){
if ($sqlarray[$i]['tag_text'] == $formarray[$ii]){
// print
echo '<pre>';
print_r ($sqlarray[$i]['tag_text']);
echo '</pre>';
}
}
}

Many thanks. I'll eventually advance to retrieve tag_id from matches and INSERT new tag_texts for new tags. This was a large help.

jd01




msg:4000519
 6:19 pm on Oct 3, 2009 (gmt 0)

Glad it worked for you...
Good luck on the rest and let us know if you need any more help.

IMO: Always avoid foreach, unless you absolutely need it for some reason.

Also: Make sure you are either 'scrubbing' your POSTed variables with a regular expression, or at the very least using addslashes()...

$formarray=strtolower (addslashes($_POST['tags']));

crackpipe




msg:4000534
 7:07 pm on Oct 3, 2009 (gmt 0)

Hey thanks. I'm about 5/9 done with figuring my data entry forms, then comes retrieval, then comes security ,etc,etc. Eventually, I may learn enough to be able to add a metadata layer and quick retreivability for scanned documents and photos using tags. Became so diasappointed with premade solutions that seems worth time and effort. Paperlessness and retreiveability appear to be either very expensive and/or privacy compromising if relying on proprietary solutions. If not willing to spend big or put it all on a third party server, the average home user appears left with their simple folder and directory (non)system. So, putting in the time to develop a workable LAMP on my system for paperlessness -- perhaps will be a year's work during free time -- feels satisfying. Thanks again for the help.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved