I'm trying to create a string that will be put into a foreach statement. The string will determine which fields will appear from the database. Since I cannot determine which fields will be chosen from the form in advance, I thought this would be the best way to deal with the unknown and show the results of the query.
I built the query the same way and it worked. I've tried double and single quotes and I'm just not understanding why it doesn't want to build this string. Please help (and excuse me if this is a stupid question as I'm new to PHP - I could find an answer that matched in the similar searches). THANK YOU! :-)
<?php
include('database.php');
function show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb)
{
$list = "";
global $db;
$theQuery = 'select ';
if($productIDcb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productID, ';
$list .=' $products['productID']'; //THIS IS LINE 17
}
if($categoryIDcb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'categoryID, ';
$list .=' $products['categoryID']';
}
if($productCodecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productCode, ';
$list .=' $products['productCode']';
}
if(isset($_POST['productNamecb']))
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'productName, ';
$list .=' $products['productName']';
}
if(isset($_POST['listPricecb']))
{
$theQuery .= 'listPrice, ';
}
else
{
$theQuery .= 'listPrice, ';
$list .=' $products['listPrice']';
}
$theQuery .=' "" from ' .$table;
echo($theQuery);
$rSet = $db -> query($theQuery);
foreach($rSet AS $products)
{
$list .= "<br>";
}
echo($list);
}
?>