Page 1 of 1

Foreach Problem

Posted: Mon Jun 14, 2010 9:07 am
by nitediver
How to make these code more simple, so I don't have to use foreach() twice?

-first foreach: Have query for selecting data inside foreach, and query for inserting data outside then get the last id.
-second foreach: Both query for selecting and inserting data inside foreach().

I am concern on two of query for inserting not the selecting one.
If I put both insert query inside one foreach(), I can get the last id.

Any idea?

Code: Select all

//first foreach
foreach(){
    foreach(){
    >>query select
    }
}
>>query insert
$foo = msyql_insert_id();

//second foreach
foreach(){
    foreach(){
    >>query select
    >>query insert($foo)
    }
}

Re: Foreach Problem

Posted: Mon Jun 14, 2010 7:20 pm
by JakeJ
I'm not sure just what you're trying to accomplish but using two foreach loops may actually be the best thing for you to do. Something just cannot be simplified any more than they already are.

Re: Foreach Problem

Posted: Tue Jun 15, 2010 9:57 am
by nitediver
Actually I just want to make the code shorter by using only one foreach, what I am trying to do just make it simple.
I wonder if using function, but I think it can't be done, since my code bit complicated by using two query.

Maybe someone can give me advice, but I guess you are right "Something just cannot be simplified any more than they already are.".

Re: Foreach Problem

Posted: Tue Jun 15, 2010 9:59 am
by AbraCadaver
nitediver wrote:Actually I just want to make the code shorter by using only one foreach, what I am trying to do just make it simple.
I wonder if using function, but I think it can't be done, since my code bit complicated by using two query.

Maybe someone can give me advice, but I guess you are right "Something just cannot be simplified any more than they already are.".
It's impossible to tell without seeing the exact code that you are using.