-1

I'm having some trouble solving a nested loop. I have a form that sends a series of data to the mysql database. I use a while function and it works correctly. But now I have an checkbox option for each entry, and that's my problem.

The checkbox should be sent to another table, with the id, the series ID and a checkbox per row. But all I can do is make the answer from the first series repeat through the others.

This is what I want:

Lance 1
 Checkbox 1
 checkbox 3
 checkbox 4
Lance 2
 checkbox 2
 checkbox 3
Lance 3
 checkbox 1
 checkbox 2
 checkbox 3

This is what I get
Lance 1
 Checkbox 1
 checkbox 3
 checkbox 4
Lance 2
 Checkbox 1
 checkbox 3
 checkbox 4
Lance 3
 Checkbox 1
 checkbox 3
 checkbox 4

My loop code so far:

$i=0;
$elements = count($lance);
while ($i < $elementss) {
$query = "INSERT INTO 
    mapa_bordo_lance 
        (id_mb, lance, data_lance, lat, lon, anzol, isca, hora_lan, hora_rec, ave_capt, mm_uso)
    VALUES 
        ('$id_mb', '$lance[$i]', '$data_lance[$i]', '$lat[$i]', '$lon[$i]', '$anzol[$i]', '$isca[$i]', '$hora_lan[$i]', '$hora_rec[$i]',
        '$ave_capt[$i]', '$mm_uso[$i]')";
$result = mysql_query($query, $link);

$y = 0;
$elementss2 = count($checkbox);
while ( $y < $elements2) {
    $query = "INSERT INTO mapa_bordo_mm (id_mb, lance, mm)
            VALUES ('$id_mb', '$lance[$y]', '$medida_metiga[$y]')";
    $result = mysql_query($query, $link);

$y++;
}
$i++;
}
summea
  • 7,390
  • 4
  • 32
  • 48
aoceano
  • 85
  • 1
  • 13
  • Thanks for providing the above information! I do have a question, though: where does your `$checkbox` variable get set? Also, for the future, you may want to use something called "[prepared statements](http://us3.php.net/pdo.prepared-statements)" for your SQL queries _(for safety reasons, at least.)_ – summea May 12 '14 at 16:40
  • 1
    The $checkbox comes from the form, where 4 choices are presented, and is given from $_POST["checkbox"];. Is this what you asked? By the way, I'll take a look and consider you advice on the prepared statements. Thanks a lot. – aoceano May 12 '14 at 16:50
  • No problem! Thanks for the extra info; I'm just trying to pinpoint why the results are not looking the way you want. Another question for you: what does the `$lance` variable represent? _(Or in other words, if you only have four choices for the checkbox options, how can these values be different for a single form submission?)_ – summea May 12 '14 at 16:54
  • I'm using a dynamic form. The $lance represents an event, and inside this event there is 4 options. One form submission has an ID where any given numbers of events could happen, and each event could have up to 4 options. In the form, I use Javascript to add and remove events. – aoceano May 12 '14 at 17:07
  • Ah, I see; I'm still not sure how `$lance` and `$checkbox` are being set... is there any way you could post a little more code detail _(or an example)_ of what `$lance` and `$checkbox` look like? – summea May 12 '14 at 17:16
  • 1
    Sure, i used the paste code to make it easier. The checkbox real name is medida_metiga. I changed here so it would be easier for everybody to read. I posted some code from tehe form. The link is http://paste.ideaslabs.com/show/yUhMStlnZ1 – aoceano May 12 '14 at 18:50
  • Would it be possible to see a variable "dump" of your `$_POST` after an example form has been submitted? – summea May 13 '14 at 16:57
  • 1
    Yeah sure, the first line is the common info, and below is 3 events that occurred during a fishing boat trip. "medida_metiga" is the var from the checkbox. As I click the button to add a new event, this var is add a id in front, to differenciate from the others. So its event 1, "medida_metiga", event 2, "medida_metiga0", event 3, "medida_metiga1" and so on. I think thats where the problem lies. But I still haven figure it out. http://paste.ideaslabs.com/show/mdpDmTbR5b – aoceano May 14 '14 at 17:49

1 Answers1

0

About the Checkboxes

According to the information presented in your question, comments, and pasted snippets, I have the feeling that you are getting the same Checkbox values for each Lance because in this code:

$y = 0;
$elementss2 = count($checkbox);
    while ( $y < $elements2) {
    $query = "INSERT INTO mapa_bordo_mm (id_mb, lance, mm)
              VALUES ('$id_mb', '$lance[$y]', '$medida_metiga[$y]')";
    $result = mysql_query($query, $link);
    $y++;
}

it appears that the $medida_metiga[$y] part is only calling the same $medida_metiga (and its array items...) rather than calling the next $medida_metiga0 and $medida_metiga1 parts in your example array (given in this link.)

You may need to find a way to concatenate your $medida_metiga array/key with a number (like 0, 1, ... etc.) kind of like this:$medida_metiga[$y.$some_number_here] in order to find the matching checkboxes. But you may also want to rethink the naming for the $medida_metiga, $medida_metiga0, $medida_metiga1 approach.

For example, instead of:

$medida_metiga

What if you began with:

$medida_metiga0

This would allow you to more easily iterate through the array/keys without having to check if this item has a number at the end or not.


About the SQL

When you have a chance, it would probably be a good idea to check out how to use prepared statements in PHP if for no other reason than to prevent SQL injection. This other answer may give you some tips on getting started with prepared statements.


About the Looping

While while loops can technically work for your purposes, it might be worth using either for or foreach loops in this case, as those sorts of loops can help keep code clean and improve code readability. It might be worth taking a look at this other answer for tips on selecting loops for different applications.


About the Variable Naming

You may have already seen this, but do make sure that your variable names ($elements or $elementss?) match:

$elements = count($lance);
while ($i < $elementss) { ... }

// ...

$elementss2 = count($checkbox);
while ( $y < $elements2) {
Community
  • 1
  • 1
summea
  • 7,390
  • 4
  • 32
  • 48
  • 1
    I don't even know how to thank you. That's very helpful. I was thinking of something similar, but I had no idea how to implement. I'll try to change and make it work. I'll sure take a good look in your tips and take in great consideration to improve my work. Once again, thank you for taking your time to help me. I'll post here the solution when I get it working. Best regards. – aoceano May 14 '14 at 21:38
  • @aoceano Glad to be able to help! :) – summea May 14 '14 at 21:53