RE: MultSelect ListBox hell!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 23 September 2004 20:53, Stuart Felenstein wrote:

> So here is what I have:
> 
> //Here is defines the field variable and uses
> //CodeCharge function to grab the variable
> $s_Industry = CCGetParam("s_Industry", "");
> $s_LocationState = CCGetParam("s_LocationState", "");
> $s_TaxTerm = CCGetParam("s_TaxTerm", "");
> 
> //Here is the parsing of the array.  I'm not sure
> what the $proj variable is for.  I gather it's a
> holder for the array values
> 
> if (count($s_Industry) > 0 AND is_array($s_Industry)) {
> foreach ($s_Industry as $key => $value) {
> if ($Proj1 != "") $Proj1 = $Proj1.",";
> $Proj1 = $Proj1."'".$value."'";
> }
> }

$Proj1 now contains a comma-separated list of the values passed to
$s_Industry, which was filled by the CCGetParam() above.  The above code
block is equivalent to (and would be better coded as):

  if (count($s_Industry) > 0 AND is_array($s_Industry)) {
     $Proj1 = "'".implode("','", $s_Industry)."'";
  }

which may look somewhat familiar to you!

> //Parsing next array
> if (count($s_LocationState) > 0 AND
> is_array($s_LocationState))
> {
> foreach ($s_LocationState as $key => $value) {
> if ($Proj2 != "") $Proj2 = $Proj2.",";
> $Proj2 = $Proj2."'".$value."'";
> }
> }
> //Parsing file array
> if (count($s_TaxTerm) > 0 AND is_array($s_TaxTerm)) {
> foreach ($s_TaxTerm as $key => $value) {
> if ($Proj3 != "") $Proj3 = $Proj3.",";
> $Proj3 = $Proj3."'".$value."'";
> }
> }

These two blocks are similar for the other two parameters, $s_LocationState
and $s_TaxTerm

> Here is what will be the death of me :)
> First the where condition below is being appended to
> anoher file in my main sql statement (don't ask:))
> CC keeps their code in various sections and files.
> Anyway , if you remember yesterday, it was determined
> that I needed the where condition to be dynamically
> created based on the user input.
> Obviously the code below does nothing of that sort.

Obviously it does, using the lists of values built above.

> So as a start, I'm trying to figure out what I can do
> with this section here (since this is really what
> needs to be changed, I think) to make it all right.
> 
> if ($Proj1)
> $VendorJobs->ds->SQL.= " AND (`VendorJobs`.Industry IN (".$Proj1."))";

Plug the value for $Proj1 built above into this, and again you have
something that may look very familiar to you.  It's the very same IN clause
I was urging you to use yesterday! ;)

> if ($Proj2)
> $VendorJobs->ds->SQL.= " AND
> (`VendorJobs`.LocationState IN (".$Proj2."))";
> if ($Proj3)
> $VendorJobs->ds->SQL.= " AND (VendorJobs.TaxTerm IN (".$Proj3."))";
> echo "SQL:". $VendorJobs->ds->SQL."<br>";

So you now have a dynamically built portion of SQL, in $VendorJobs->ds->SQL,
that has a clause like "AND x IN ('a','b','c')" for each input field that
has any values set.  According to your specs, this could be anywhere up to
six separate clauses.  This seems to be exactly what you wanted, so your
code looks good to go.

To help you understand exactly what's going on here, or in any script or
program you're struggling to interpret, I would recommend two time-honoured
techniques:

(i) Dry running: get out your genuine old-fashioned pen and paper, pretend
you're the PHP interpreter processing this program, and write down the value
of each variable as you work through the code.  Yes, it can be quite
long-winded and tedious, but you really get a tremendous feel for exactly
what's going on.  The number of times I've done this and cursed myself as
I've watched a value going out of range, or not getting set at all, I
couldn't begin to count.

(ii) As a variation of the above, put *lots* of debugging echos in your
script: echo the value of every variable frequently and redundantly -- it
can help sometimes just to have the reassurance that a value really hasn't
changed, even though you know it absolutely shouldn't!!  Again, if you're
struggling with how a script operates this can help you see how values are
built up, and can often show you exactly where a wrong value gets calculated
(and how, and maybe even why).  It's especially important to echo your final
complete SQL statement just before it's executed, so that if it produces an
error message you've got the actual relevant SQL right in front of you.

Hope this helps.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux