searching an array
Posted by: Rick.Haden (---.range86-158.btcentralplus.com)
Date: January 09, 2009 09:28PM

Hi all...

I am a newbie, but am managing to get there. However, it appears I to still have some basics to get my head round. I have an SQL query and placed the resultant dataset into an array. How do I then search that array for a particular row? I can loop through and get the whole thing, but I need to be able to take a user input and use that to find a value in the array.

I bet it is real simple, but I am getting no where...

Rick

Options: ReplyQuote
Re: searching an array
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: January 09, 2009 09:45PM

well you shouldnt take all of it if you only want some of it. so search the database first and take the result u need

if u need help just ask

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: searching an array
Posted by: Rick.Haden (---.range86-158.btcentralplus.com)
Date: January 09, 2009 09:55PM

Good point. However, my thinking is this...at the beginning of the code I draw in a dataset, which I understand is stored in memory. I then use this for a number of individual queries throughout the application rather than keep querying the database each time. Or is this wrong thinking?

Bestest

Rick

Options: ReplyQuote
Re: searching an array
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: January 09, 2009 10:16PM

thats ok but mysql is very quick

anyway id use

while ($row = mysql_fetch_array($result))
{

if (in_array($keyword, $row, true))
{
echo "found";
}
else
{
echo "not found";
}
}




$result is the whole table in an array
$row is the current row it is processing as an array
$keyword is the users input

please not that it may find more then one row with a true state if the user input correspondes to more than one row

also this is actually slower then connecting to the database with the user input and searching as mysql is quicker then php at searching. php needs to go through each row and each key in the array

if you connect to mysql u could search an exact column(field) if you know what your looking for

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]



Edited 1 time(s). Last edit at 01/09/2009 10:21PM by stevenmartin99.

Options: ReplyQuote
Re: searching an array
Posted by: Rick.Haden (---.range86-158.btcentralplus.com)
Date: January 09, 2009 10:47PM

Thanks Steven,

That makes a whole lot of sense...I have taken a note of your code, but also taken you advice and used database query each time and it works a dream...thank you...

While I am writing, there is something else you may have an opinion on...can you insert more than one row into a table from a single page?

Regards

Rick

Options: ReplyQuote
Re: searching an array
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: January 09, 2009 10:58PM

from a single page?

you mean a script?

yes of course. you can do it as a loop or as seperate INESRT/UPDATE statements/

just dont use the mysql close statement on the page or make sure u establish a connection each time or you will get an error

you may want to research persistent connections.

[ie2.php.net] pconnect

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]



Edited 1 time(s). Last edit at 01/09/2009 11:02PM by stevenmartin99.

Options: ReplyQuote
Re: searching an array
Posted by: Rick.Haden (---.range86-158.btcentralplus.com)
Date: January 09, 2009 11:08PM

Let me explain more fully...just before I dive in and find a deadend:

There is a club which takes members...they can either join singly or as a couple. WHen they join as a couple, both become members, but there is a join. The idea is to have two forms on screen for the couple and have each insert a record.

Oh, by the way, I promise not to stalk you now you answered my question so fully...lol

Regards

Rick

Options: ReplyQuote
Re: searching an array
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: January 09, 2009 11:44PM

yes in english coding


have a form, for singles and couples

have a hidden field called couple


if hiddenfield=true

then loop through the Insert statement again with the second set of details

if your giving the members an id number then in the table have a "linked to"

field and fill in the id number of the person they are linked to

no ---- name -- surname -- linked to
01 ----- jojo ---- blog -----
02 ----- jo ----- bloggs ----03
03 ----- joanne --bloggs ---- 02

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]



Edited 2 time(s). Last edit at 01/09/2009 11:46PM by stevenmartin99.

Options: ReplyQuote
Re: searching an array
Posted by: Rick.Haden (---.range86-158.btcentralplus.com)
Date: January 10, 2009 02:32PM

Thank you so much for that Steven...not saying that I will be able to put it into action without some further explanation, though, but I will try...I understand the process, but not too clear on how you replace the values on the second loop...

I will experiment..

Best regards

Rick

Options: ReplyQuote
Re: searching an array
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: January 10, 2009 03:03PM

well you could take an array from the form , name(1) name(2) etc

but id usually use this for mutiple passes 10, 100, 1000 times

for just two i use it hard coded into the loop

if you want help email the form and script and ill look at it.

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote


Sorry, only registered users may post in this forum.