HighDots Forums  

PHP and searching thru an array

Macromedia Dreamweaver Macromedia Dreamweaver Discussions (macromedia.dreamweaver)


Discuss PHP and searching thru an array in the Macromedia Dreamweaver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jacob 99
 
Posts: n/a

Default PHP and searching thru an array - 07-20-2004 , 12:11 PM






Hi Again,
Im a newbie at PHP, so please have patience with another PHP problem. Here
is the problem portion of the code:

$vendorList = array('Cisco','SUN','Juniper','IBM RS/6000','IBM AS/400'); //
array of available vendors
if (isset($_POST['brandInquiry'])) { // if form has been refreshed
for errors, re- check those items previously selected
foreach ($vendorList as $value) { // go through all vendors
if ($brandInquiry['value']) { // if this vendor is located
in the array, re-check it
echo "<input type=\"checkbox\" name=\"brandInquiry[]\"
value=\"selling\" checked=\"checked\"> $value \n";
} else { // else it was never
checked, so leave blank
echo "<input type=\"checkbox\" name=\"brandInquiry[]\"
value=\"selling\"> $value \n";
}
}

What I want to happen :::
$brandInquiry is an array of selected vendors. What I'd like to do is create
a "sticky" form that remembers which vendors the user checked, even if the
page needs to refresh to hilite errors in the rest of the form. What I
thought to do is run a comparison between $brandInquiry and $vendorList.
Whichever items in $vendorList that match up to $brandInquiry should be
checked, and any other values in $vendorList should be unchecked.

What actually happens::::
on refresh ALL the items in $vendorList show up as checked off, no matter
which ones I originally checked.

Any ideas? Thanks very much!
Jacob



Reply With Quote
  #2  
Old   
Jacob 99
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 12:19 PM






OK!
so far....mistake number 1 is I forgot to change the "selling" value to
$value......
Im sure there are plenty more errors


"Jacob 99" <jboyo (AT) yorku (DOT) ca> wrote

Quote:
Hi Again,
Im a newbie at PHP, so please have patience with another PHP problem. Here
is the problem portion of the code:

$vendorList = array('Cisco','SUN','Juniper','IBM RS/6000','IBM AS/400');
//
array of available vendors
if (isset($_POST['brandInquiry'])) { // if form has been
refreshed
for errors, re- check those items previously selected
foreach ($vendorList as $value) { // go through all vendors
if ($brandInquiry['value']) { // if this vendor is
located
in the array, re-check it
echo "<input type=\"checkbox\" name=\"brandInquiry[]\"
value=\"selling\" checked=\"checked\"> $value \n";
} else { // else it was
never
checked, so leave blank
echo "<input type=\"checkbox\" name=\"brandInquiry[]\"
value=\"selling\"> $value \n";
}
}

What I want to happen :::
$brandInquiry is an array of selected vendors. What I'd like to do is
create
a "sticky" form that remembers which vendors the user checked, even if the
page needs to refresh to hilite errors in the rest of the form. What I
thought to do is run a comparison between $brandInquiry and $vendorList.
Whichever items in $vendorList that match up to $brandInquiry should be
checked, and any other values in $vendorList should be unchecked.

What actually happens::::
on refresh ALL the items in $vendorList show up as checked off, no matter
which ones I originally checked.

Any ideas? Thanks very much!
Jacob





Reply With Quote
  #3  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 01:04 PM



.oO(Jacob 99)

Quote:
OK!
so far....mistake number 1 is I forgot to change the "selling" value to
$value......
Im sure there are plenty more errors
A slightly different approach:

$vendorList = array('Cisco', 'SUN', 'Juniper', 'IBM RS/6000', 'IBM AS/400');

// $brandInquiry contains the submitted values if available, else an empty array
$brandInquiry = isset($_POST['brandInquiry']) ? $_POST['brandInquiry'] : array();

// Now simply loop through the vendor list, print out the checkboxes and
// test if a box was previously checked, i.e. if its value can be found
// in the array $brandInquiry.
for ($i = 0; $i < count($vendorList); $i++) {
printf('<label><input type="checkbox" name="brandInquiry[]" value="%u"%s> %s</label>'."\n",
$i,
in_array($i, $brandInquiry) ? ' checked="checked"' : '',
$vendorList[$i]);
}

http://www.php.net/printf
http://www.php.net/sprintf
http://www.php.net/in_array

Maybe it's of some help.

Micha


Reply With Quote
  #4  
Old   
Jacob 99
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 02:08 PM



Once again you have the answer
.......although, I dont understand how you did this....in fact, being very
fresh to PHP, I dont understand most of the symbols at all. You replaced my
20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts.

I gues I have my read! :P

Thank very much!
Jacob



"Michael Fesser" <netizen (AT) gmx (DOT) net> wrote

Quote:
.oO(Jacob 99)

OK!
so far....mistake number 1 is I forgot to change the "selling" value to
$value......
Im sure there are plenty more errors

A slightly different approach:

$vendorList = array('Cisco', 'SUN', 'Juniper', 'IBM RS/6000', 'IBM
AS/400');

// $brandInquiry contains the submitted values if available, else an empty
array
$brandInquiry = isset($_POST['brandInquiry']) ? $_POST['brandInquiry'] :
array();

// Now simply loop through the vendor list, print out the checkboxes and
// test if a box was previously checked, i.e. if its value can be found
// in the array $brandInquiry.
for ($i = 0; $i < count($vendorList); $i++) {
printf('<label><input type="checkbox" name="brandInquiry[]"
value="%u"%s> %s</label>'."\n",
$i,
in_array($i, $brandInquiry) ? ' checked="checked"' : '',
$vendorList[$i]);
}

http://www.php.net/printf
http://www.php.net/sprintf
http://www.php.net/in_array

Maybe it's of some help.

Micha



Reply With Quote
  #5  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 02:53 PM



.oO(Jacob 99)

Quote:
......although, I dont understand how you did this....in fact, being very
fresh to PHP, I dont understand most of the symbols at all. You replaced my
20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts.
;D

I used only two "special" features to keep the code small:


1) The printf() function
This is very useful to output strings which contain variables. The first
parameter is the string itself, containing placeholders (these %s and %u
things). The remaining parameters are the variables, which are then used
instead of the placeholders in the order they appear. So the first %u is
replaced with $i, the next %s is replaced with either checked="checked"
or nothing (see below), the last %s is replaced with the vendor name.

See the manual on sprintf() for details.

http://www.php.net/sprintf


2) The ternary operator ?:
This nice little thingy works like an if-else statement, but is more
flexible. It acts more like a function with a return value (returns one
of two possible values, dependent on a condition), so it can be used in
places where if-else is not allowed, for example to calculate a value in
a printf() call.

It's described in the chapter "Comparison Operators".

http://www.php.net/manual/en/language.operators.comparison.php

Micha


Reply With Quote
  #6  
Old   
Jacob F.
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 06:57 PM



Well, it works great for display, but I've found a large problem: instead of
the "value" attribute being the name of the vendor, it is the index# where
that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says
'1','2')....since im still anayzing the code, do you know how I can get the
"real" value for the value attribute?
Thanks
Jacob


"Michael Fesser" <netizen (AT) gmx (DOT) net> wrote

Quote:
.oO(Jacob 99)

......although, I dont understand how you did this....in fact, being very
fresh to PHP, I dont understand most of the symbols at all. You replaced
my
20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts.

;D

I used only two "special" features to keep the code small:


1) The printf() function
This is very useful to output strings which contain variables. The first
parameter is the string itself, containing placeholders (these %s and %u
things). The remaining parameters are the variables, which are then used
instead of the placeholders in the order they appear. So the first %u is
replaced with $i, the next %s is replaced with either checked="checked"
or nothing (see below), the last %s is replaced with the vendor name.

See the manual on sprintf() for details.

http://www.php.net/sprintf


2) The ternary operator ?:
This nice little thingy works like an if-else statement, but is more
flexible. It acts more like a function with a return value (returns one
of two possible values, dependent on a condition), so it can be used in
places where if-else is not allowed, for example to calculate a value in
a printf() call.

It's described in the chapter "Comparison Operators".

http://www.php.net/manual/en/language.operators.comparison.php

Micha



Reply With Quote
  #7  
Old   
Jacob F.
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 07:04 PM



....and another strange thing happens. When the user submits the form, no
matter what is checked before they submit it it will reset to check only the
first item.

Michael, can I send you the link by email so you can see the behaviour
yourself?

Thanks very much,
Jacob



"Jacob F." <jboyo (AT) yorku (DOT) ca> wrote

Quote:
Well, it works great for display, but I've found a large problem: instead
of
the "value" attribute being the name of the vendor, it is the index# where
that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it
says
'1','2')....since im still anayzing the code, do you know how I can get
the
"real" value for the value attribute?
Thanks
Jacob


"Michael Fesser" <netizen (AT) gmx (DOT) net> wrote in message
news:jcpqf0tgip8ckf3hjla1ilogcooc7dnspl (AT) 4ax (DOT) com...
.oO(Jacob 99)

......although, I dont understand how you did this....in fact, being
very
fresh to PHP, I dont understand most of the symbols at all. You
replaced
my
20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts.

;D

I used only two "special" features to keep the code small:


1) The printf() function
This is very useful to output strings which contain variables. The first
parameter is the string itself, containing placeholders (these %s and %u
things). The remaining parameters are the variables, which are then used
instead of the placeholders in the order they appear. So the first %u is
replaced with $i, the next %s is replaced with either checked="checked"
or nothing (see below), the last %s is replaced with the vendor name.

See the manual on sprintf() for details.

http://www.php.net/sprintf


2) The ternary operator ?:
This nice little thingy works like an if-else statement, but is more
flexible. It acts more like a function with a return value (returns one
of two possible values, dependent on a condition), so it can be used in
places where if-else is not allowed, for example to calculate a value in
a printf() call.

It's described in the chapter "Comparison Operators".

http://www.php.net/manual/en/language.operators.comparison.php

Micha





Reply With Quote
  #8  
Old   
Michael Fesser
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-20-2004 , 08:34 PM



.oO(Jacob F.)

Quote:
Well, it works great for display, but I've found a large problem: instead of
the "value" attribute being the name of the vendor, it is the index# where
that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says
'1','2')
Yep, whenever possible I try to use numeric IDs instead of literal
values, IMHO they are easier to handle, more reliable and save
resources. So I used them and it works, at least in this short
out-of-context checkbox snippet. But OK, it's quite possible that it
causes problems with the rest of your script.

Quote:
....since im still anayzing the code, do you know how I can get the
"real" value for the value attribute?
Three changes to the printf() call:

value="%u" --> value="%s"
$i, --> $vendorList[$i],
in_array($i, --> in_array($vendorList[$i],

The first two changes affect the output of the checkboxes, the value
will now be the vendor name instead of the ID. The third modifies the
test whether a box was checked or not, it now tests on the vendor name.
That should do it.


To your other problem:

Quote:
...and another strange thing happens. When the user submits the form, no
matter what is checked before they submit it it will reset to check only the
first item.
Probably a "conflict" between the checkbox snippet and the rest of your
form script, as mentioned above.

Quote:
Michael, can I send you the link by email so you can see the behaviour
yourself?
Yep. But I would have to look at the source code if possible, the output
in the browser is not really helpful. You can either mail me the source
or (better) store it under a different name on the server (preferred
extension is .phps), so that the server won't interpret it.

Micha


Reply With Quote
  #9  
Old   
Jacob 99
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-21-2004 , 01:50 PM



Thank you for replying.
There are 2 parts so far;
the php form handler
http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/getquote.phps
and the form itself
http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/_content/getquote.inc

Im still cleaning them up, so I realize there are loads of inefficiencies,
but Im still learning so I wrote everything in an easy to understand
fashion.
Thanks so much for looking through this and helping me understand,
Jacob


"Michael Fesser" <netizen (AT) gmx (DOT) net> wrote

Quote:
.oO(Jacob F.)

Well, it works great for display, but I've found a large problem: instead
of
the "value" attribute being the name of the vendor, it is the index#
where
that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it
says
'1','2')

Yep, whenever possible I try to use numeric IDs instead of literal
values, IMHO they are easier to handle, more reliable and save
resources. So I used them and it works, at least in this short
out-of-context checkbox snippet. But OK, it's quite possible that it
causes problems with the rest of your script.

....since im still anayzing the code, do you know how I can get the
"real" value for the value attribute?

Three changes to the printf() call:

value="%u" --> value="%s"
$i, --> $vendorList[$i],
in_array($i, --> in_array($vendorList[$i],

The first two changes affect the output of the checkboxes, the value
will now be the vendor name instead of the ID. The third modifies the
test whether a box was checked or not, it now tests on the vendor name.
That should do it.


To your other problem:

...and another strange thing happens. When the user submits the form, no
matter what is checked before they submit it it will reset to check only
the
first item.

Probably a "conflict" between the checkbox snippet and the rest of your
form script, as mentioned above.

Michael, can I send you the link by email so you can see the behaviour
yourself?

Yep. But I would have to look at the source code if possible, the output
in the browser is not really helpful. You can either mail me the source
or (better) store it under a different name on the server (preferred
extension is .phps), so that the server won't interpret it.

Micha



Reply With Quote
  #10  
Old   
Jacob 99
 
Posts: n/a

Default Re: PHP and searching thru an array - 07-22-2004 , 09:38 AM



Well, it turns out by switching some of those variables I was able to solve
all the problems.
Thanks again very much Michael!
Jacob

"Jacob 99" <jboyo (AT) yorku (DOT) ca> wrote

Quote:
Thank you for replying.
There are 2 parts so far;
the php form handler

http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/getquote.phps
and the form itself

http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/_content/getquote.inc

Im still cleaning them up, so I realize there are loads of inefficiencies,
but Im still learning so I wrote everything in an easy to understand
fashion.
Thanks so much for looking through this and helping me understand,
Jacob


"Michael Fesser" <netizen (AT) gmx (DOT) net> wrote in message
news:spcrf0d40ktelhhqrfe86631p8rr2tie11 (AT) 4ax (DOT) com...
.oO(Jacob F.)

Well, it works great for display, but I've found a large problem:
instead
of
the "value" attribute being the name of the vendor, it is the index#
where
that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it
says
'1','2')

Yep, whenever possible I try to use numeric IDs instead of literal
values, IMHO they are easier to handle, more reliable and save
resources. So I used them and it works, at least in this short
out-of-context checkbox snippet. But OK, it's quite possible that it
causes problems with the rest of your script.

....since im still anayzing the code, do you know how I can get the
"real" value for the value attribute?

Three changes to the printf() call:

value="%u" --> value="%s"
$i, --> $vendorList[$i],
in_array($i, --> in_array($vendorList[$i],

The first two changes affect the output of the checkboxes, the value
will now be the vendor name instead of the ID. The third modifies the
test whether a box was checked or not, it now tests on the vendor name.
That should do it.


To your other problem:

...and another strange thing happens. When the user submits the form,
no
matter what is checked before they submit it it will reset to check
only
the
first item.

Probably a "conflict" between the checkbox snippet and the rest of your
form script, as mentioned above.

Michael, can I send you the link by email so you can see the behaviour
yourself?

Yep. But I would have to look at the source code if possible, the output
in the browser is not really helpful. You can either mail me the source
or (better) store it under a different name on the server (preferred
extension is .phps), so that the server won't interpret it.

Micha





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.