PHP and searching thru an array

message from Jacob 99 on 20 Jul 2004
Hi Again,
$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
 
Jacob 99 replied to Jacob 99 on 20 Jul 2004
OK!
Im sure there are plenty more errors :)

"Jacob 99" <jboyo@yorku.ca> wrote in message
news:cdjg40$aug$1@forums.macromedia.com...
 
Michael Fesser replied to Jacob 99 on 20 Jul 2004
.oO(Jacob 99)

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
 
Jacob 99 replied to Michael Fesser on 20 Jul 2004
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@gmx.net> wrote in message
news:bojqf0pq2lj94ert92sgv1k59vfep99smd@4ax.com...
value="%u"%s> %s</label>'."\n",
 
Michael Fesser replied to Jacob 99 on 20 Jul 2004
.oO(Jacob 99)

;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
 
Jacob F. replied to Michael Fesser on 20 Jul 2004
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@gmx.net> wrote in message
news:jcpqf0tgip8ckf3hjla1ilogcooc7dnspl@4ax.com...
 
Jacob F. replied to Jacob F. on 20 Jul 2004
...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@yorku.ca> wrote in message
news:cdk7vm$a1f$1@forums.macromedia.com...
 
Michael Fesser replied to Jacob F. on 21 Jul 2004
.oO(Jacob F.)

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.

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:

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

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
 
Jacob 99 replied to Michael Fesser on 21 Jul 2004
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@gmx.net> wrote in message
news:spcrf0d40ktelhhqrfe86631p8rr2tie11@4ax.com...
 
Jacob 99 replied to Jacob 99 on 22 Jul 2004
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@yorku.ca> wrote in message
news:cdmaa1$i4$1@forums.macromedia.com...
http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/getquote.phps
http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/_content/getquote.inc
 

Archived message: PHP and searching thru an array (Macromedia Dreamweaver Web Design)