Solved

For Inside ForEach Only Inserting First Array Value

Posted on 2012-03-27
12
280 Views
Last Modified: 2012-08-14
Hi Experts,

Thanks for reason. The code below works, excepts it only inserts the first array first with $id=1 into the database and then reports a duplicate value for 1 in the database, so it's not getting to 2 or 3. Can you help me identify why?

The database insert code is Zend and is valid. Thanks!

$info = array(
    1 => array( 5, 'Name' ),
    2 => array( 5, 'Name' ),
    3 => array( 3, 'Name 2' )
    );

foreach( $info as $id => $data )
{
    
    for( $id_value_id = 0; $id_value_id < 3; $id_value_id++ )
    {
    
        $data = array(
            'id'            => $id,
            'user_id'       => $data[0],
            'name'          => $data[1],
            );
        
        $db->insert(
            'title',
            $data
            );
        
    }
    
}

Open in new window

0
Comment
Question by:SevenAteAnthony
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 37772138
You are putting the original array into a variable called $data which you then overwrite in the loop.


foreach( $info as $id => $data )
{
       ....

        $data = array(
0
 
LVL 1

Author Comment

by:SevenAteAnthony
ID: 37772184
Thanks for finding that. I actually messed up when I renamed the variables to place on here to public eyes. Excluding that find, do you see anything else wrong with the idea behind the code why it would loop to the first one and keep looping to the first one until it reports the duplicate error?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37772574
Is that the actual code or did you modify it at all to show on here?
0
 
LVL 1

Author Comment

by:SevenAteAnthony
ID: 37772590
Actual code. Variable names were modified. Thanks.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37772633
How heavily modified? More specifically, what were the original variable names on each of the loop lines?
0
 
LVL 1

Author Comment

by:SevenAteAnthony
ID: 37772671
There's nothing wrong with the variables names minus the double $data. They were changed for legality reasons. Please assume the above are the variables names if you're willing to help. Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37774036
Variable names that are changed for any reason have no place in the debugging process.

Please do one of the following: (1) Post the actual code, along with the CREATE TABLE statements and enough data to illustrate the issue, so we can copy it and run it on our own servers, of (2) Create a replica that exactly illustrates the problem, and post all of the actual code, CREATE TABLE statements and enough test data to load the tables and illustrate the issue.  

Good test data and access to the actual code are kind of the foundation of programming.

Thanks, ~Ray
0
 
LVL 1

Accepted Solution

by:
SevenAteAnthony earned 0 total points
ID: 37774056
You guys are being too dramatic. I figured out the issue anyway and it was that I didn't even need a for statement to access the array in the array. Just needed a quick pair of developer eyes on the code. All is fixed now.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37774074
Well, the reason I ask is because I'm trying to figure out why you're looping 3 times with no difference in the data:

for( $id_value_id = 0; $id_value_id < 3; $id_value_id++ )
{
... query in here ...
}

You're running the same query 3 times, so I figured there may have been something changed in the variable names to explain why and maybe the original had something changing. As of right now, the only variable that changes is $id_value_id but it's never used inside your loop.

The simple fact that I'm asking should indicate that I'm willing to help. I'm just trying to figure out the problem.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37774105
Umm... okay... I don't think it has anything to do with being dramatic. We just don't have any information other than what you give us. Most of us are pretty experienced programmers - we don't really ask for information unless it's relevant to understanding the problem and the solution.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37774743
Going forward, SevenAteAnthony, you'll get quicker results if you can create a test case that demonstrates the issues.  Often you will find that just the act of creating the test case will cause you to think about the issues in ways that will lead you to the solution.  The creation of a test case is a useful exercise and one worthy of your time.  Changing the variable names or obscuring the data base details is not worthy of your time.  In fact, these kinds of changes often introduce more errors into the code.  So when you're wondering what you can produce for "public eyes" try your best to isolate the issues and demonstrate them separately.  It is only easy to eat an elephant if you do it a bite at a time.
0
 
LVL 1

Author Closing Comment

by:SevenAteAnthony
ID: 37792756
Chosen because I fixed it myself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now