7 Replies Latest reply on Jul 27, 2006 12:21 PM by idiaz06

    foreach alternative

    idiaz06
      I have a script written in php and an trying to find the alternative in coldfusion and don't seem to be getting anywhere, I have googled and googled, it may just be I am looking in the wrong place. Your help in greatly welcomed!

      THis is the php version
      $product_options=$row["product_options"];
      $product_options=split(",",$product_options);


      foreach($product_options as $assign_id)
      {
      //get info
      $result=mysql_query("SELECT *FROM options_bot WHERE assign_id='$assign_id'");

      while($row=mysql_fetch_array($result))
      {
      $option_id=$row["option_id"];
      $value_id=$row["value_id"];
      $option_price=$row["option_price"];
      $option_symbol=$row["option_symbol"];

      if($option_symbol=="+")
      {
      $opt_price=$opt_price+$option_price;
      }
      if($option_symbol=="-")
      {
      $opt_price=$opt_price+$option_price;
      }
      //find the name of options and values
      //get the name of the option
      $result=mysql_query("SELECT * FROM options_top WHERE option_id='$option_id'");

      while($row=mysql_fetch_array($result))
      {
      $option_name=$row["option_name"];
      }
      //get the name of the value
      $result=mysql_query("SELECT * FROM options_mid WHERE value_id='$value_id'");

      while($row=mysql_fetch_array($result))
      {
      $value_name=$row["value_name"];
      }
      echo"<b>$option_name : </b>$value_name<br>";

      $options=array($option_name,$value_name);

      }

      Here is what I have no far in Coldfusion

      <cfset product_options=#product_options#>
      <cfset assign_id=ListToArray(#product_options#, ",")>
      <cfdump var="#assign_id#">

      <cfquery name="optinfo" datasource="mydatabase">
      SELECT * FROM options_bot WHERE assign_id='#assign_id#'
      </cfquery>

      I get this error message

      Complex object types cannot be converted to simple values.

      THank You Sooooo Much!
        • 1. Re: foreach alternative
          Level 7
          To be fair, your PHP code is looping over an array and using the values
          in each element of the array. On the other hand your ColdFusion code is
          trying use the entire array object (a complex variable) in a place that
          needs a single simple value (a simple variable).

          To match the PHP code you need to add a loop over the array.

          <cfset product_options=#product_options#>
          <cfset assign_id=ListToArray(#product_options#, ",")>
          <cfdump var="#assign_id#">

          <cfloop from="1" to="#arrayLen(assign_id)#" index="row"
          <cfquery name="optinfo" datasource="mydatabase">
          SELECT * FROM options_bot WHERE assign_id='#assign_id[row]#'
          </cfquery>
          </cfloop>

          idiaz06 wrote:
          > I have a script written in php and an trying to find the alternative in
          > coldfusion and don't seem to be getting anywhere, I have googled and googled,
          > it may just be I am looking in the wrong place. Your help in greatly welcomed!
          >
          > I get this error message
          >
          > Complex object types cannot be converted to simple values.
          >
          > THank You Sooooo Much!
          >
          • 2. Re: foreach alternative
            Dan Bracuk Level 5
            It looks like you are starting with a list and, at the end of the day, your php code is looping though the list and running a query each time.

            If all you want to do is query your db for items that match any of your list items, why don't you just use the list in your query and forget all that unnecessary complicated code. Also, make sure you change your query so that it expects a list and not a single value.
            • 3. Re: foreach alternative
              idiaz06 Level 1
              I think I follow but don't fully understand, I don't expect you guys to write the code for me, but if you could give me a little harder push in the right direction. thank you so very much!
              • 4. Re: foreach alternative
                Level 7
                It depends on what your ultimate goal is.

                If you just want to use a list of values as part of a where clause you
                can do something like this.

                WHERE assign_id IN (<cfqueryparam value="#product_options#" list="yes"
                cfsqltype="cf_sql_varchar">)

                If you want to run a different query for each item in the list, you can
                loop over the list without turning it into an array.

                <cfloop list="product_options" index="option">
                <cfquery name="optinfo" datasource="mydatabase">
                SELECT * FROM options_bot WHERE assign_id='#option#'
                </cfquery>

                DO STUFF WITH optinfo QUERY

                </cfloop>

                Also, this line makes very little sense to me. Without some kind of
                scoping on one side or the other, you are just setting a variable to its
                current value?

                <cfset product_options=#product_options#>



                idiaz06 wrote:
                > I think I follow but don't fully understand, I don't expect you guys to write the code for me, but if you could give me a little harder push in the right direction. thank you so very much!
                • 5. Re: foreach alternative
                  Stressed_Simon Level 1
                  idiaz06 what Ian Skinner showed you you is the way to do it!

                  the PHP split() function turns a list into an array, to loop through the array using tags you do exactly what Ian showed you, if you are more comfortable with cfscript then do the following:-
                  • 6. foreach alternative
                    Dan Bracuk Level 5
                    quote:

                    Originally posted by: idiaz06
                    I think I follow but don't fully understand, I don't expect you guys to write the code for me, but if you could give me a little harder push in the right direction. thank you so very much!

                    If you have this list:
                    1,2,3,4,5,7,14,89

                    and you want to know how many records in a table (call it myTable) where a numeric field (call it theField) matches any number in that list, do you know how to do it?
                    • 7. Re: foreach alternative
                      idiaz06 Level 1
                      I will try this as soon as I can, thank you guys very much and I will come back and help when I know the answer to some ones question.