• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

foreach alternative

New Here ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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!
TOPICS
Advanced techniques

Views

634

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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!
>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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:-

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 27, 2006 Jul 27, 2006

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation