Message from discussion
Populating/Checking Checkbox Values Based on Lookup
Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mail
From: "Thyme" <s...@davidweinberg.net>
Newsgroups: alt.php,comp.lang.php
Subject: Re: Populating/Checking Checkbox Values Based on Lookup
Date: Wed, 17 Jul 2002 21:50:46 -0400
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <ujc7mbk44uj7ab@corp.supernews.com>
References: <JhnZ8.569709$cQ3.52707@sccrnsc01>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4807.1700
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
X-Complaints-To: newsabuse@supernews.com
Lines: 76
This code I wrote might help you, but I don't know if it will do the exact
thing you want. I use it to
//Goes through a string and writes "CHECKED" in to an array with each char
in the string as the identifyer.
$i=0;
while ($i < strlen($myrow[1])){
$key=substr($myrow[1], $i, 1);
$ischecked[$key] = "CHECKED";
$i++;
}
\\ $gname is an array with the name of each of the checkboxes. $ischecked
will either be blank or "CHECKED"
foreach($gname as $key => $groupname){
echo "<br><input type=\"checkbox\" name=\"chk[$key]\" style=\"border:0px\"
$ischecked[$key]>$groupname";
\\ I hope that helps, but you might have to put things into an array.
"Jeff Donnici" <jdonn...@ay-tee-tee-bee-eye-dot.com> wrote in message
news:JhnZ8.569709$cQ3.52707@sccrnsc01...
> I'm sure there's a simple answer but after browsing php.net and google for
a
> while, I can't seem to find the right function to handle this. Assume I've
> got three tables -- USERS, COLORS, and USER_COLOR_MAP. The USERS table
has
> an ID and a user's name. The COLORS table has an ID and a color's name.
The
> USER_COLOR_MAP table has two columns -- user_id and color_id. This lets me
> map N colors to each user.
>
> In a form, I want to display a checkbox for each color and, if I'm
> displaying an existing user, I want to pre-check any boxes that have been
> mapped to that user... the HTML is all squared away, so no worries there.
>
> // grab ALL colors
> $ALL_sql = "SELECT id, name FROM colors;"
>
> // grab colors for a certain user
> $MAPPED_sql = "SELECT color_id FROM user_color_map WHERE user_id = ".
> $a_users_id;
>
> Ok, so assuming I run both queries, I've now got two result-sets. I
iterate
> through the ALL result set and create a checkbox for each color. But, at
> each stop, I'd like to search the MAPPED result set to see if the ID value
> appears in it (in the COLOR_ID column)... If so, add the word 'CHECKED' to
> the HTML to pre-check the box.
>
> This is the part I can't seem to find a simple function for. I know I
could
> loop through the MAPPED result set each time to find a match, but that
seems
> awfully inefficient. The IN_ARRAY function looks like just the ticket
except
> that it doesn't seem to like being given a MySQL result set instead of an
> array. Ok, so maybe I create an array from the MAPPED result set... but it
> doesn't look like EXPLODE will do that for me, so I'm back at looping
> through the MAPPED result set and creating an array of all the values.
> Surely there must be a better way.
>
> I don't need a bunch of code, but a pointer or reference to function(s)
that
> would be applicable would be most appreciated!
>
> Thanks in advance,
>
> -- jeff
>
>