Foreach Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Foreach Problem

Post 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)
    }
}
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Foreach Problem

Post 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.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Foreach Problem

Post 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.".
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Foreach Problem

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply