Perform a loop from explode function?

Hi,

Have just encountered a problem which I just can’t seem to figure out in my ability.

I have a text area that tracks the x and y position of dragged items, it logs each drop event as:

id,xposition,yposition

So when submitted you end up with something similar too (but longer):

353,563,76
356,231,43
357,348,134

I need to explode each line out and then explode each line to give me the id, x position and y position so I can update these results in the database.

Any help in achieving this will be greatly appreciated.
Thanks!

This should get you started


<?php
$input = $_POST['input'];
$lines = explode("\
", $input);
foreach ($lines as $row){
   $values = explode(",", $row);
   echo 'x=' . $values[0] . ' ' ;
   echo 'y=' . $values[1] . ' ' ;
   echo 'z=' . $values[2] . '<br />';
}
?>
<form action="" method="post">
<textarea name="input">
353,563,76
356,231,43
357,348,134</textarea>
<input type="submit" >
</form>

Thanks for this!
I’m trying to put those values into variables, how can I achieve this?

I need to have $id $xposition and $yposition

Thanks :slight_smile:


.......
   $id = $values[0]; 
   $xposition = $values[1]; 
   $yposition = $values[2];
.......

I did just that myself, but when I try to enter them into an SQL query and echo them it just doesn’t happen?..

echo"Saved! $id $xposition $yposition";

Cant tell without seeing your code

  $id = $values[0];
   $xposition = $values[1];
   $yposition = $values[2];
   
   $update = mysql_query("UPDATE `outfit_items` SET `positionx` = '$xposition', `positiony` = '$yposition' WHERE `id` = '$id' AND `owner` = '$user[id]'");
   
   echo"Saved! $id $xposition $yposition";

but where is that in relation to my code ?

$data= $_POST['data'];
$lines = explode("\
", $data);
foreach ($lines as $row){

   $values = explode(",", $row);
   
   $id = $values[0];
   $xposition = $values[1];
   $yposition = $values[2];
   
   $update = mysql_query("UPDATE `outfit_items` SET `positionx` = '$xposition', `positiony` = '$yposition' WHERE `id` = '$id' AND `owner` = '$logged[id]'");
   
   echo"Saved! $id $xposition $yposition";
   
}

instead of echo"Saved! $id $xposition $yposition";
try this echo “Saved!”.$id.“,”.$xposition.“,”.$yposition;


$data= $_POST[‘data’];
$lines = explode("
", $data);
foreach ($lines as $row){

$values = explode(“,”, $row);

$id = $values[0];
$xposition = $values[1];
$yposition = $values[2];

$update = mysql_query(“UPDATE outfit_items SET positionx = ‘$xposition’, positiony = ‘$yposition’ WHERE id = ‘$id’ AND owner = ‘$logged[id]’”);

echo"Saved! $id $xposition $yposition";

}


invio

Your code is fine if your not getting any results its because $_POST[‘data’] doesnt contain any data to parse.

Am I missing something?..

echo"</div>
<div class=\\"panel\\">
<form action=\\"savedragdrop.php\\" method=\\"POST\\">
<textarea id=\\"data\\" style=\\"width:50px; height:100px; margin-top:280px\\" wrap=\\"off\\"></textarea>
<input type=\\"submit\\" value=\\"Save\\">
</form></div>
</body></html>";

your textarea doesnt have a name property

Ah sorry, haven’t done this in quite a while! Thanks for your help :slight_smile: