0

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);
  }



?>
  • `$list .=" $products['productID']";` – mychalvlcek Nov 23 '13 at 18:01
  • Thank you for marking the effected line. That helps a lot. Whenever you get an error message like that, focus on the line in question or just a line or two up and you will usually find the problem. Start by looking for missing semicolons or unmatched quotes. – TecBrat Nov 23 '13 at 18:03
  • 2
    What are you trying to do in lines like these? `{$theQuery ==$theQuery;}` – TecBrat Nov 23 '13 at 18:09
  • $theQuery == $theQuery; sets theQuery equal to it's current value if the variable is equal to "". But if the variable actually has a string value in it (or any value for that matter), then it adds to $theQuery as indicated in the code. – user3025217 Nov 23 '13 at 18:35
  • 1
    Not that it matters in the current code, because it's basically "do nothing" anyway, but `$theQuery == $theQuery;` is acually a comparison. I think you meant `$theQuery = $theQuery;` but I'd replace that with `//do nothing`. There is no point in assigning a variable its existing value. – TecBrat Nov 23 '13 at 19:24

2 Answers2

2

Well you are using single quotes twice

$list .=' $products['productID']';

Try using different quotes for strings: for example,

$list .=" $products['productID']"; // This will produce a string that looks like

$products['productID']

When the compiler encounters a quote at the start of the string, ', it expects another one to close it. In your original code, the second quote appears prematurely making your string become

' $products['

which makes the next part become incomprehensible to the compiler

productID']'

Also, if you want to include a variable within the string, you need double quotes ", not single '.

Lastly, you can't access arrays directly in a quoted string like that. Try concatenating:

$list .=' ' . $products['productID'];

*edit

A note on @marcelkorpel 's suggestion. You can use curly braces, but there is a slight typo in the comment. Please use

"{$products['productID']}" and not "${products['productID']}"

Jason J. Nathan
  • 7,422
  • 2
  • 26
  • 37
  • Regarding your last statement, you could also use curly braces (`"${foo['bar']}"`). – Marcel Korpel Nov 23 '13 at 18:10
  • Yeap, didn't want to confuse OP with too many options. Just wanted to explain the error a little more and provide at least one solution. – Jason J. Nathan Nov 23 '13 at 18:11
  • Thank you! I had tried double quotes before and received "Undefined variable: products" error. I tired the concatenating you show in your last line of corrections and now I'm back to Undefined variable. But I still get the same error even if I set $products = ""; so that it is set until it reaches for the foreach statement. What do you suggest? I really appreciate your help. – user3025217 Nov 23 '13 at 18:33
  • Well, I don't see `$products` being defined anywhere... :) So it'll throw an undefined error. Also `$products` needs to be an array that contains all the fields you re accessing. `$products = ""` makes it a string. – Jason J. Nathan Nov 23 '13 at 18:35
  • $products isn't defined until for the foreach($rSet AS $products) at the end. What I'm trying to do is build the $list string that will pull from the array $products, but I don't want $products to do anything except hold the place in the $list string until it is called in the foreach loop. Is that even possible? That's why I was trying to include it as text originally - in the hopes that it would not be viewed as a variable until it was in the foreach loop. – user3025217 Nov 23 '13 at 18:39
  • So I guess I need to look up how to set an empty array. I think I get it now. Thanks – user3025217 Nov 23 '13 at 18:42
  • You can't use a variable until it is defined. Try putting the `$list` declaration after you define the `$products` array. – Jason J. Nathan Nov 23 '13 at 18:42
  • I realized it would be better to build the $products array with if statements inside of the foreach instead of trying to build the $list string. That is working now. Thank you so much for your help. I would not have made it to this point if I didn't get over the other issues. – user3025217 Nov 23 '13 at 20:10
2

The error in line 17 is the single quotes:

$list .=' $products['productID']';  //THIS IS LINE 17

should be

$list .=$products['productID'];  //THIS IS LINE 17

Side note: I'd suggest this change as well:

 if(!$productIDcb == "") //notice the negation operator here: '!'
         {
                $theQuery .= 'productID, ';
                $list .=$products['productID'];  //THIS IS LINE 17
         }

And make that same change in the other similar spots.

TecBrat
  • 3,643
  • 3
  • 28
  • 45