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 [EMAIL PROTECTED] * 14203 - 1321 * ***********************************************************************
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php