Jump to content


Photo

foreach hisown


  • Please log in to reply
18 replies to this topic

#1 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 03:57 PM

Okay, here's what I'm trying to do...
I'm trying to insert multiple arrays into the same mysql table at once. 
That works fine using foreach()...but only inserts the correct data for the array listed as the parameter in the foreach() loop.
The other arrays get inserted, but with incorrect data, like the proper index for the array isn't being assigned or something...

Here is my code:

<?php
	$myChildren = $_POST["myChildren"];
	$myHours = $_POST["myHours"];
        $myRate = $_POST["myRate"];

	##for each instance in our items array (checked items)...insert a new record
	foreach($_POST["myItems"] AS $key => $myItems){
	

  		$insertSQL = sprintf("INSERT INTO drs_invoices_items (invoiceID, itemID, childID, hours_logged, billable_rate) 
							VALUES (%s, '$myItems', 
							'$myChildren[$key]', 
							'{$_POST["myHours"][$key]}', 
							'{$_POST["myRate"][$key]}')",
						#used for invoiceID	
					   GetSQLValueString($_POST['invoiceID'], "int"));
					   
               mysql_select_db($database_drs_database, $drs_database);
               $Result1 = mysql_query($insertSQL, $drs_database) or die(mysql_error());
               

}
?>

So the proper number of rows are inserted based on the $myItems array...
But the other arrays ($myChildren, $myHours, $myRate), all get populated with data from other idexes in the array, and not from the same
array index as $myItems is pulled from.

I've been trying everything, to no avail.
I don't think I know enough about this stuff...

Can somebody please help me?

Thanks much in advance.

Josiah



#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 04:15 PM

I think it would be helpful if we could see the data you are working with. Try putting the following ont he page and post the response:

echo "<pre>";
print_r($_POST["myItems"]);
print_r($_POST["myChildren"]);
print_r($_POST["myHours"]);
print_r($myRate = $_POST["myRate"]);
echo "</pre>";

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 05:43 PM

Okay, they are...in this order...

myItems, myChildren, myHours, myRate

<?php
Array
(
    [0] => 42
    [1] => 41
    [2] => 40
    [3] => 39
    [4] => 38
    [5] => 37
    [6] => 36
    [7] => 35
    [8] => 34
    [9] => 33
    [10] => 32
    [11] => 31
    [12] => 30
    [13] => 29
    [14] => 28
    [15] => 27
    [16] => 26
    [17] => 25
    [18] => 21
    [19] => 22
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 25
    [13] => 26
    [14] => 26
    [15] => 26
    [16] => 0
    [17] => 0
    [18] => 0
    [19] => 0
)
Array
(
    [0] => 0.02
    [1] => 0.62
    [2] => 1.99
    [3] => 0.66
    [4] => 1.02
    [5] => 0.34
    [6] => 0.04
    [7] => 0.08
    [8] => 0.02
    [9] => 0.08
    [10] => 0.03
    [11] => 9.02
    [12] => 0.68
    [13] => 0.79
    [14] => 1.14
    [15] => 1.25
    [16] => 1.15
    [17] => 1.00
    [18] => 2.00
    [19] => 1.00
)
Array
(
    [0] => 100
    [1] => 100
    [2] => 100
    [3] => 100
    [4] => 100
    [5] => 100
    [6] => 100
    [7] => 100
    [8] => 100
    [9] => 100
    [10] => 100
    [11] => 100
    [12] => 100
    [13] => 100
    [14] => 100
    [15] => 100
    [16] => 100
    [17] => 100
    [18] => 100
    [19] => 100
)
?>


If I insert all of these arrays everything works fine...but if I select only a few in the middle (based on check boxes from a form) and then try to insert just those items, all of the correct $myItems come through, but the others iterate from 0, and the data doesn't match up.

What can you make of this?
Thanks for your help!

Josiah

#4 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 06:37 PM

So, the arrays are passed from the form? Is the myItems array the list of checkbox items that were checked? This should be pretty easy to sort out, just need to understand how the data is passed to this page.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 06:44 PM

Yes, the arrays are passed from the form.
myItems[] is the array of all checkboxes that are checked
myRate, myChildren and myHours are all hidden fields residing in the same row as the checkbox, and are made into arrays as well.

Thank you for checking back frequently!



#6 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 07:07 PM

OK, that won't work. When you use checkboxes, only the checkboxes that are checked get passed. So, if you have 20 checkboxes and give them the same name (myItems[]) so that they are passed as an array, if you only check 5 items then the passed array only has 5 items in it, with indexes of 0-4.

In your example you should be getting values, but they should be the wrong values.

Can you put the myItems array in a hidden field as well? Then create your checkboxes with another array name (let's say checkedItems) and give them the values of the indexes (0-19). Then you could get your values like this:

<?php
$myChildren = $_POST["myChildren"];
$myHours = $_POST["myHours"];
$myRate = $_POST["myRate"];
$myItems = $_POST["myItems"];
$checkedItems = $_POST["checkedItems"];

##for each instance in our items array (checked items)...insert a new record
foreach($checkedItems as $key){

	$insertSQL = sprintf("INSERT INTO drs_invoices_items (invoiceID, itemID, childID, hours_logged, billable_rate) 
				VALUES (%s, '$myItems[$key]', 
				'$myChildren[$key]', 
				'{$_POST["myHours"][$key]}', 
				'{$_POST["myRate"][$key]}')",
				#used for invoiceID	
				GetSQLValueString($_POST['invoiceID'], "int"));
					   
	mysql_select_db($database_drs_database, $drs_database);
	$Result1 = mysql_query($insertSQL, $drs_database) or die(mysql_error());
}
?>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 07:46 PM

Thanks so much for your help on this....I think we are ALMOST there.

I implemented what you suggested, and here are my results:

I checked the following items on my form:

<?php
Array - myItems
(
 
    [14] => 28
    [16] => 26
    [18] => 21
    [19] => 22
)
Array - myChildren
(
    
    [14] => 26
    [16] => 0
    [18] => 0
    [19] => 0
)
Array - myHours
(
    
    [14] => 1.14
    [16] => 1.15
    [18] => 2.00
    [19] => 1.00
)
Array - myRate
(
  
    [14] => 100
    [16] => 100
    [18] => 100
    [19] => 100
)
?>

However, the following information went into my database:

<?php
Array - myItems - ALL DATA CORRECT
(
 
    [14] => 28
    [16] => 26
    [18] => 21
    [19] => 22
)
Array - myChildren - DATA INCORRECT
(
    
    [14] => 0  --- should be 26
    [16] => 0
    [18] => 0
    [19] => 0
)
Array - myHours
(
    
    [14] => .02 -- should be 1.14
    [16] => .62 -- should be 1.15
    [18] => 1.99 -- should be 2.00
    [19] => .66 -- should be 1.00
)
Array - myRate
(
  
    [14] => 100
    [16] => 100
    [18] => 100
    [19] => 100
)
?>


So what i don't understand is why the myItems value is being carried over correctly and the rest are not??



#8 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 07:56 PM

So as you can see...after it properly selects myItems, it then adds these next indexes into the database:

Example of array data entered into database (from myHours array)

<?php
Array
(
    [0] => 0.02
    [1] => 0.62
    [2] => 1.99
    [3] => 0.66

)
?>


So it's not properly selecting the index from checkedItems, rather iterating through $key, which starts at zero.

Any idea how to fix this?

Thanks again

#9 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 08:14 PM

I just realized that my checkedItems array is iterating backwards...

so where myRow[0]::checkedItems[19]

could this be my problem?

#10 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 08:25 PM

or it could be that now all 20 items from each array is being inserted into the database instead of just the items i've checked.

#11 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 11:02 PM

I think you need to put in some debugging code so that when you submit the form it prints out

1) The checked values
2) The complete arrays
3) The sql query

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#12 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 27 October 2006 - 11:22 PM

you have problems with your form
can we see form code

#13 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 01:52 PM

Okay, the form gets a bit confusing because i'm generating it on the fly with Javascript DOM, but I will post to see if you guys can make heads or tails of it.

So here is the main form, that lists all projects associated with a client, currently there is a checkbox next to each project (this is not the checkbox i'm having trouble with).
This form lists projects...if you press a button within this form, it drills down and lists all ITEMS associated with that project.  This is all done dynamically with javascript.  PHP passes values to my javascript function, which then generates a row for each item and lists the items under each project.  Next to each item is a check box, these are the checkboxes i'm having problems with.  If you check several boxes and hit submit, it grabs the correct values for the checkbox and the $myItems, but for nothing else. 

I will start by posting the main projects form here:


<form action="<?php echo $editFormAction; ?>" method="POST" name="projects">
  <table id="myTable" width="775" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr class="columnHeaders">
  <td width="25">&nbsp;</td>
  <td width="250">project</td>
  <td width="300">description</td>
  <td width="75">hours</td>
  <td width="75">rate</td>
  <td width="150">total</td>
  </tr>

	<?php 
	#alternate row colors
	$current_row = 0;
 	$uniqueID = 0;
	do { 
	$current_row = 1 - $current_row; 
	$uniqueID++;
	
	?>
    <tr id="row<?php echo $row_selectProjects['projectID']; ?>" class="row<?php echo $current_row; ?>">
	  <td><input name="projects" type="checkbox" value="1">
	  <input name="myRowID" type="hidden" id="row<?php echo $row_selectProjects['projectID']; ?>" value="row<?php echo $row_selectProjects['projectID']; ?>"><input name="projectID" type="hidden" value="<?php echo $row_selectProjects['projectID']; ?>"></td>
      <td height="25"><a href="javascript:showItems('<?php echo $myID; ?>','<?php echo $row_selectProjects['projectID'] ; ?>', '<?php echo $uniqueID; ?>');"><img src="images/carrot.gif" width="10" height="15" border="0"></a><?php echo $row_selectProjects['project_name']; ?></td>
      <td><?php echo $row_selectProjects['project_desc']; ?></td>
      <td><?php echo $row_selectProjects['total_hours_converted']; ?></td>
      <td><?php echo $row_selectProjects['billable_rate']; ?></td>
      <td>$<?php echo $row_selectProjects['total_billable']; ?></td>
    </tr>
	<?php $cellID = $row_selectItems['itemID'] ; ?>
	

	<?php } 
	while ($row_selectProjects = mysql_fetch_assoc($selectProjects)); ?>
	<tr>
	<td colspan="6"><img src="images/horz_line.gif" width="100%" height="7">
	</td>
	</tr>
	
	<tr>
	<td colspan="6"><input name="clientID" type="hidden" value="<?php echo $myID; ?>">
	<input name="invoiceID" type="hidden" value="<?php echo $myInsertID; ?>">
	
	  <input name="myDate" type="hidden" value="<?php echo $myDate; ?>">
<input name="" type="submit" value="Add to Invoice">
	</td>
	</tr>
	
</table>
  <input type="hidden" name="MM_insert" value="projects">
</form>





#14 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 01:55 PM

So as you can see, there is a link to a javascript function within that form, here is the code pulled out for your reference:

<a href="javascript:showItems('<?php echo $myID; ?>','<?php echo $row_selectProjects['projectID'] ; ?>', '<?php echo $uniqueID; ?>');"><img src="images/carrot.gif" width="10" height="15" border="0"></a>

and here is the showItems() function


function showItems(myID, itemID, rowID){

	location.href  = 'add_invoices10.php?recordID='+myID+'&itemID='+itemID+'&rowID='+rowID+'&step=3';

	
}





#15 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 02:01 PM

It's not letting me paste rest of code...i will post a preview online.

#16 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 02:30 PM

Okay, go to this page to see the form and the code:

http://www.drs-flash..._invoices10.php

user:  test
pass: test

In STEP 1, select DV TIME STUDIOS

In STEP 2, press the Generate Invoice Button

In STEP 3, click the blue carrot next to DV Time Studios to drill down

Select some items, and then press Add to Invoice

Thanks

Josiah

#17 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 30 October 2006 - 02:54 PM

There is no "Generate Invoce" button, only an "Add to Invoice" and it generates errors.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#18 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 02:58 PM

Make sure you select a CLIENT from the drop down in step 1 first...

Select DVTIME STUDIOS...then the GENERATE INVOICES button will appear

#19 jwilliams

jwilliams
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 30 October 2006 - 04:27 PM

Okay guys, thanks for all of your help...I've figured it out...it was a problem with my form.

It was a careless overlook...my checkbox field was receiving the wrong value..
instead of receiving the $checkedItems value it was receiving the $myItems value.

When i fixed this, the correct number of rows are inserted every time with the correct data!

Thank you for all of your help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users