Re: Logic problem
| From: | John Nichel | Date: | Tue, 20 Apr 2004 15:14:04 +0000 |
| Subject: | Re: Logic problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184008@lists.php.net to get a copy of this message | ||
Alex Hogan wrote:
Hi All, I am having a logic problem. (insert jokes here) I am trying to create a function that will allow me to pass any number of fields and tables into a blanket query. I am getting an error; Unexpected foreach. I have tried placing the loop in several different places and am getting the same error. What should I be doing differently? Here's my code; function myselect($array1, $array2){ $i = 0; $n = 0; $query = "SELECT".foreach($array1 as $flds){ $flds[$i]; }. "FROM".foreach($array2 as $tbls){ $tbls[$n]; }.; $result = mssql_query($query); print "<table width=\"100%\" border=\"1\"> <tr>"; $j = 0; while($row = mssql_fetch_array($result)){ $fields = $row[flds[$j]]; print "<td>".$fields."</td></tr>"; } }You can't concat a construct. Try something like this... $query = "SELECT "; $start = true; foreach ( $array1 as $flds ) { if ( $start ) { $query .= $flds[$i]; $start = false; } else { $query .= ", " . $flds[$i]; } } $query .= " FROM "; $start = true; foreach ( $array2 as $tbls ) { if ( $start ) { $query .= $tbls[$n]; $start = false; } else { $query .= ", " . $tbls[$n]; } } Ugly code, needs tweaking, but you should get the idea. -- *********************************************************************** * _ __ __ __ _ * John Nichel * * | |/ /___ __ \ \ / /__ _ _| |__ ___ __ ___ _ __ * 716.856.9675 * * | ' </ -_) _` \ \/\/ / _ \ '_| / /(_-<_/ _/ _ \ ' \ * 737 Main St. * * |_|\_\___\__, |\_/\_/\___/_| |_\_\/__(_)__\___/_|_|_|* Suite #150 * * |___/ * Buffalo, NY * * http://www.KegWorks.com john@KegWorks.com * 14203 - 1321 * ***********************************************************************