I'm adding data to a database and I need a new ID for each element, the element's ID gets passed into ajax data...
$( ".silly-string" ).draggable({
containment: '#_moon_static_bg_status',
stop: function(event, ui) {
var pos_x = ui.position.left;
var pos_y = ui.position.top;
var divid = ui.helper.attr("id");
jQuery.ajax({
type: "POST",
url: window.ajaxurl,
data: { "action": "myAjax", id: divid, x: pos_x, y: pos_y }}).done(function( msg ) {
$('.silly-string').text(msg)
});
}
});
The data is only sent when the drag function happens (so I think maybe the data needs to be created somewhere else?)... Let me show you an example of the HTML
echo '<div class="silly-string draggable" id="silly"style="left: '.$x.'px; top: '.$y.'px;"></div>';
echo '<div class="silly-string draggable" id="silly2"style="left: '.$x.'px; top: '.$y.'px;"></div>';
echo '<div class="silly-string draggable" id="silly3"style="left: '.$x.'px; top: '.$y.'px;"></div>';
I am going to make those IDS dynamic so when user clicks add new element the ID will increment ex. every time add new is clicked a new div will be created the id will be silly + 1, so if there are 10 elements the tenth will have an ID of silly10, hope that makes sense?
Take a look at the .draggable/ajax code above you see the var divid, that takes the id of each div with the class .silly-string so looking at the console log it does have an out put each ID..
What I am trying to achieve is adding the data to a database, where I create a new row for each ID, but overwrite each row that has already been created with that ID...Let me show an example to make some sense of what I just said...
https://i.sstatic.net/rIPQM.jpg
You see I have a new row for silly, and silly2... the only reason I do is because I manually changed the ID 100 & 101.. but I need to change the id to be dynamic, let me show you an example..
global $wpdb;
//The data here comes from an ajax call
$_POST['x'];
$_POST['y'];
$_POST['id'];
$term_id = 100;
$name = $_POST['id'];
$slug = $_POST['x'];
$term_group = $_POST['y'];
//Im adding into wp_terms a default table (for testing)...
$query = "INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES (%s, %s, %s, %s)";
//Here I insert the ajax data into the columns
$wpdb->query($wpdb->prepare($query, $term_id, $name, $slug, $term_group));
$wpdb->update("$wpdb->terms", array('slug' => $_POST['x'], 'term_group' => $_POST['y']), array('name' => $_POST['id']));
die();
Here I am adding data to a database... Notice the variables $name and $slug are dynamic based on the ajax data... and when I run the update $wpdb->update("$wpdb->terms", array('slug' => $_POST['x'], 'term_group' => $_POST['y']), array('name' => $_POST['id'])); it works fine for one element, I overwrite the slug, with the new data... if I make the $term_id = 100; dynamic then it defeats my purpose of not creating a new row everytime the x & y values change, because YES it does create a new ID but it changes the id to a new id each time...
Now that you're up to speed with what I have going on (hope that wasn't to long and made sense) would I need to create a new ajax variable to pass, or would I need to create a foreach statement? ex.
foreach($_POST['id'] as $key => $value) {
//Stuff
}
or auto increment the variable in the ajax.. I mean I really am lost at this part and am open to some more experienced folks opinions and solutions.. could save me a lot of headache...
Let me know if you need me to specify anything its hard to print out what exactaly I see in my code and my logic, but I did the best I could
Following @MH- solution the table looks like this
https://i.sstatic.net/PO0AM.jpg
It still adds a new row, when the element is dragged... here is how I implemented the code in my script, perhaps I did it wrong?
global $wpdb;
//The data here comes from an ajax call
$_POST['x'];
$_POST['y'];
$_POST['id'];
$name = $_POST['id'];
$slug = $_POST['x'];
$term_group = $_POST['y'];
$myrows = $wpdb->get_row( "SELECT id FROM {$wpdb->terms} WHERE name LIKE '{$name}'" );
if( $myrows != null ){
$term_id = $myrows->id;
$wpdb->update("$wpdb->terms", array('slug' => $_POST['x'], 'term_group' => $_POST['y']), array('name' => $_POST['id']));
} else {
//Im adding into wp_terms a default table (for testing)...
$query = "INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES (%s, %s, %s, %s)";
//Here I insert the ajax data into the columns
$wpdb->query($wpdb->prepare($query, $term_id, $name, $slug, $term_group));
}
die();
Looking at the image... where silly and silly 2 is.. there should only be one row for each.