0

I have container with div like this:

<div class="container">
    <div class="step" id="1">
        <h2 class="title">Step 1</h2>
        <div class="image" id="1">Item 1</div>
        <div class="image" id="2">Item 2</div>
        <div class="image" id="3">Item 3</div>
    </div>
    <div class="step" id="2">
        <h2 class="title">Step 2</h2>
        <div class="image" id="4">Item 4</div>
        <div class="image" id="5">Item 5</div>
        <div class="image" id="6">Item 6</div>
    </div>
    <div class="step" id="3">
        <h2 class="title">Step 3</h2>   
        <div class="image" id="7">Item 7</div>
        <div class="image" id="8">Item 8</div>
        <div class="image" id="9">Item 9</div>
    </div>
</div>

Basically I have script that is taking note of drag and drop and notes changes in div.step class. See Image for better Understanding

enter image description here

$.ajax({
        type: 'POST',
        url: 'process.php',
        data: {json: JSON.stringify(myArguments)},
        dataType: 'json'            
    });

I don't know how to take this further in process.php

Any way to separate step id and class id as shown in image above so i can insert in database

Thank You.

Edit: Update process.php (Working Now Thanks To Ofir Baruch)

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$json = $_POST['json'];
$organize = json_decode($json);

foreach($organize->{1} as $pos => $div){

$pos1 = 1;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
//Insert to the Database:
// Step: 1
// image_id: $div
// position: $pos

}

foreach($organize->{2} as $pos => $div){
//Insert to the Database:
// Step: 2
// image_id: $div
// position: $pos
    $pos1 = 2;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
}

foreach($organize->{3} as $pos => $div){
//Insert to the Database:
// Step: 3
// image_id: $div
// position: $pos
    $pos1 = 3;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
    if ($conn->query($sql) === TRUE) {

    } 
}

?>

1 Answer 1

2

Start by decoding the JSON string

$json = $_POST['json'];
$organize = json_decode($json);

$organize structure: (in case of: '{"1":["1","2"], "2":["3","4"]}';)

> object(stdClass)#1 (2) {
> 
> ["1"]=>   array(2) {
>     [0]=> string(1) "1"
>     [1]=> string(1) "2"   }
> ["2"]=>   array(2) {
>     [0]=> string(1) "3"
>     [1]=> string(1) "4"   }
> }

Now, the properties of the $organize class are the steps which are also numbers, so in order to access them you should use:

//$organize->{1} //Access step 1 arrays of "divs"

foreach($organize->{1} as $pos => $div){
//Insert to the Database:
// Step: 1
// image_id: $div
// position: $pos
}

foreach($organize->{2} as $pos => $div){
//Insert to the Database:
// Step: 2
// image_id: $div
// position: $pos
}

foreach($organize->{3} as $pos => $div){
//Insert to the Database:
// Step: 3
// image_id: $div
// position: $pos
}

Please notice you're calling a variable which isn't exist in the 2,3 loops.

foreach($organize->{2} as $pos => $div){
    $pos1 = 2; //change to $pos2 = 2;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos2)


....
....

foreach($organize->{3} as $pos => $div){

    $pos1 = 3; //change to $pos3 =3 ;
    $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos3)
Sign up to request clarification or add additional context in comments.

8 Comments

I will try it but is there a way to automate to access all step instead of specifying $organize->(1). and creating "n" foreach. Thanks.
Sure, just use a foreach loop as you've mentioned.
It's almost seem to be working can you tell me how to access step_id inside for each $sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,HOW TO GET STEP ID)."','".mysqli_real_escape_string($conn,$div)."')"; Thank You!
step_id is basically the "{$n}".
Ok So it is working partially as (in case of: '{"1":["1","2"], "2":["3","4"]}';) it is running only the first foreach loop and skipping rest so i database it is saving 1.[1,2] any idea ? Thank you very much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.