RE: [PHP] Building a query on multiple variables, how to test for which variable

From: Date: Thu, 29 Aug 2002 17:20:18 +0000
Subject: RE: [PHP] Building a query on multiple variables, how to test for which variable
Groups: php.general 
Request: Send a blank email to php-general+get-114525@lists.php.net to get a copy of this message
Does this fit your idea of a "more elegant solution"? This is just an enhanced and tested version of the one I posted earlier (somehow I had time and was curious about it). <?php if (isset($_POST['var'])){ // if POSTed // initial query // make sure "WHERE" not included when none is selected $qpon = "SELECT BillDate, StateInd, BAN, Type, PON, Phrase,"; $qpon .= " PhraseLine1, USOC, Description, RateZone, Rate"; $qpon .= " FROM tblUSOCChargesDetail"; $its_set = 1; // my super "switch" foreach ($_POST['var'] as $key => $value){ // walk if ($value != 0){ // make sure the "--- Select ---" is not included
     if ($its_set == 1){
       $qpon .= " WHERE USOC = '" . $value . "'";
       $its_set = 2; // just to make sure "WHERE" is only added once
     } else {
       $qpon .= " AND USOC = '" . $value . "'";
     }
} } // end of foreach } $qpon .= " ORDER BY BillDate"; echo "$qpon"; // Check the generated query ?> And this is what I used for testing: // html headers here <form action="testpulldown.php" method="post"> First option <select name="var[]"> <option value="0">--- Select ---</option> <option value="1a">1a</option> <option value="1b">1b</option> <option value="1c">1c</option> </select> <br /> Second option <select name="var[]"> <option value="0">--- Select ---</option> <option value="2a">2a</option> <option value="2b">2b</option> <option value="2c">2c</option> </select> <br /> Third option <select name="var[]"> <option value="0">--- Select ---</option> <option value="3a">3a</option> <option value="3b">3b</option> <option value="3c">3c</option> </select> <br /> Fourth option <select name="var[]"> <option value="0">--- Select ---</option> <option value="4a">4a</option> <option value="4b">4b</option> <option value="4c">4c</option> </select> <br /> <input type="submit" name="submit" value="Submit" /> </form> // end of html Put them in the same file and name the file "testpulldown.php". (Of course, you know...) The "beauty", I thought is, the query would still work even though none of the "pull-downs" were selected--of course, I don't how much data will be shown... NOTE: You can even have MORE pull-downs with this kind of approach. (...and less code...) BTW, who is "Todd"? I didn't see his post... Just curious... - E
Todd came in with a good suggestion which I had started towards, so this is
what I have arrived at for the moment. I will be looking to make this more elegant soon, but since they are screaming for the report now this is what I
did; <?php if($usoc1 <> "--- Select ---"){ $usoc[] = $usoc1; } if($usoc2 <> "--- Select ---"){ $usoc[] = $usoc2; } if($usoc3 <> "--- Select ---"){ $usoc[] = $usoc3; } if($usoc4 <> "--- Select ---"){ $usoc[] = $usoc4; } $usoc_count = count($usoc); // query details $qpon = "SELECT BillDate, StateInd, BAN, Type, PON, Phrase, PhraseLine1, USOC, Description, RateZone, Rate "; $qpon .= "FROM tblUSOCChargesDetail WHERE "; $first = 0; for($i = 0; $i < $usoc_count; $i++){ if($first == 0){ $qpon .= "USOC = '" . $usoc[$i] . "' "; $first = 1; } else { $qpon .= "AND USOC = '" . $usoc[$i] . "' "; } } $qpon .= "ORDER BY BillDate "; if(!($dbpon = mysql_query($qpon, $dbconnect))){ print("MySQL reports: " . mysql_error() . "\n"); exit(); } ?> I am sure that there is a more elegant solution, if I find it I will let you
know. Thanks! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________ かわいくて愉快なイラスト満載 MSN キャラクター http://character.msn.co.jp/

« previous php.general (#114525) next »