PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label delete-row. Show all posts
Showing posts with label delete-row. Show all posts

Tuesday, November 22, 2022

[FIXED] How can I remove rows on more conditions in R?

 November 22, 2022     delete-row, multiple-conditions, r     No comments   

Issue

I have session id's, client id's, a conversion column and all with a specific date. I want to delete the rows after the last purchase of a client. My data looks as follows:

SessionId       ClientId        Conversion         Date
    1               1                0             05-01
    2               1                0             06-01
    3               1                0             07-01
    4               1                1             08-01
    5               1                0             09-01
    6               2                0             05-01 
    7               2                1             06-01
    8               2                0             07-01
    9               2                1             08-01
    10              2                0             09-01

As output I want:

SessionId       ClientId        Conversion         Date
    1               1                0             05-01
    2               1                0             06-01
    3               1                1             07-01
    6               2                0             05-01 
    7               2                1             06-01
    8               2                0             07-01
    9               2                1             08-01

I looks quite easy, but it has some conditions. Based on the client id, the sessions after the last purchase of a cutomer need to be deleted. I have many observations, so deleting after a particular date is not possible. It need to check every client id on when someone did a purchase.

I have no clue what kind of function I need to use for this. Maybe a certain kind of loop?

Hopefully someone can help me with this.


Solution

If your data is already ordered according to Date, for each ClientId we can select all the rows before the last conversion took place.

This can be done in base R :

subset(df, ave(Conversion == 1, ClientId, FUN = function(x) seq_along(x) <= max(which(x))))

Using dplyr :

library(dplyr)
df %>% group_by(ClientId) %>% filter(row_number() <= max(which(Conversion == 1)))

Or data.table :

library(data.table)
setDT(df)[, .SD[seq_len(.N) <= max(which(Conversion == 1))], ClientId]


Answered By - Ronak Shah
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, October 23, 2022

[FIXED] How do you increment and decrement numbers when new rows are added or removed?

 October 23, 2022     delete-row, javascript, sql-update     No comments   

Issue

I have a button that creates a new element each time it is pressed. Attached to each new element is a numbered label, 1,2,3,4, etc

The numbers increase by 1 each time the button is pressed.

I also have a delete button attached to each element.

Problem = every time I delete an element and add a new element, the numbers are not running in order. for example, if I deleted element 3, the list would be 1,2,4, instead of 1,2,3.

How can you add a function to update the numbered labels so they run in order and not miss out on any numbers? Thanks

/*This is the function that increases the number attached to the Bet string by 1*/

let startingNum = 5; 

function changeNum() {
    ++startingNum;
    return "Bet" + "&nbsp" + startingNum;
}    

/*This is the function to add new row when the add button is pressed*/

function addRow() {

const divBox1 = document.querySelector(".fifth-row");

const labelBox = document.createElement("LABEL");
divBox1.appendChild(labelBox);
labelBox.classList.add('labelBet');
labelBox.innerHTML = changeNum();

const inputBox = document.createElement("INPUT");
divBox1.appendChild(inputBox);
inputBox.classList.add('oddsEntry');

const divBox2 = document.createElement("DIV");
divBox1.appendChild(divBox2);
divBox2.classList.add('stakePreTotal');

const divBox3 = document.createElement("DIV");
divBox1.appendChild(divBox3);
divBox3.classList.add('liaDiv');

const btnDel = document.createElement("BUTTON");
btnDel.innerText = 'Delete';
divBox1.appendChild(btnDel);
btnDel.classList.add('deleteBtn');

/*This is the function to delete the row*/

btnDel.addEventListener("click", deteteRow); 

function deteteRow() {
divBox1.removeChild(labelBox);
divBox1.removeChild(inputBox);
divBox1.removeChild(divBox2);
divBox1.removeChild(divBox3);
divBox1.removeChild(btnDel);
}

}

Solution

There are many ways you can achieve this. Here is a small, modified excerpt of your code as an example with some explanation comments.

The basic idea of this approach is to render elements based on a dynamic state which in this case is just a counter that controls how many children are rendered at a given moment. The add and delete buttons control this counter and call the render function to reflect the updated state of the counter in the view.

// define a static starting number if needed
const startingNum = 5;

// define dynamic counter
let counter = 0;

// get ref of parent rows container
const divBox1 = document.querySelector(".fifth-row");

// increase counter when add button is clicked and render rows
document.querySelector('button')
  .addEventListener('click', function () {
    counter += 1;
    renderRows();
  });

// decrease counter when any of the delete buttons is clicked and render rows again
divBox1
  .addEventListener('click', function (e) {
    if (e.target.classList.contains('deleteBtn')) {
      counter -= 1;
      renderRows();
    }
  });

// render rows based on the state of the counter
function renderRows() {
  // calc total number of rows to render based on current counter value
  const total = (startingNum + counter) - startingNum;
  // clear container by removing children
  divBox1.innerHTML = '';

  // render rows
  for (let i = 0; i < total; i++) {
    addRow(startingNum + i);
  }
}

function addRow(rowNumber) {
  // create a container for each row
  const rowContainer = document.createElement('div');
  rowContainer.classList.add('row-container');
  divBox1.appendChild(rowContainer);

  const labelBox = document.createElement("LABEL");
  rowContainer.appendChild(labelBox);
  labelBox.classList.add('labelBet');
  
  // set the text content including the dynamic row number
  labelBox.textContent = "Bet " + rowNumber;

  const inputBox = document.createElement("INPUT");
  rowContainer.appendChild(inputBox);
  inputBox.classList.add('oddsEntry');

  const divBox2 = document.createElement("DIV");
  rowContainer.appendChild(divBox2);
  divBox2.classList.add('stakePreTotal');

  const divBox3 = document.createElement("DIV");
  rowContainer.appendChild(divBox3);
  divBox3.classList.add('liaDiv');

  const btnDel = document.createElement("BUTTON");
  btnDel.innerText = 'Delete';
  rowContainer.appendChild(btnDel);
  btnDel.classList.add('deleteBtn');
}
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0; 
}

button {
  padding: .3rem .5rem;
}

.rows > div.fifth-row > div {
   display: flex;
}
<div class="rows">
  <div class="fifth-row"></div>
</div>

<button type="button">add row</button>



Answered By - Mauro Aguilar
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, April 19, 2022

[FIXED] Why does delete request return a 404 in Laravel using ajax

 April 19, 2022     ajax, delete-row, laravel, php     No comments   

Issue

This is the route inside my web.php file It says 404 Not found on the route http://127.0.0.1:8000/categories/delete

Route::middleware(["auth"])->group(function () {
    Route::resources([
        'categories' => CategoryController::class,
        'posts' => PostsController::class,
    ]);
    
    // this is the route i am targeting
    Route::delete("/categories/delete", [CategoryController::class, "delete"])->name("categories.delete"); 
});

This is the ajax request to the route inside my index.blade.php file

<button id="deleteAll" class="border-0" type="button">
    <x-heroicon-o-trash class="w-6 h-6 text-red-600" />
</button>

<script>
    $(function(){
        $("#check-all").click(function(){
            $(".item-check").prop("checked", $(this).prop('checked'));
        });

        // This is the click event to delete a category
        $("#deleteAll").click(function(e){
            e.preventDefault();

            let allIds = [];
            $("input:checkbox[name=catId]:checked").each(function(){
                allIds.push($(this).val());
            });

            $.ajax({
                url: "{{ route('categories.delete') }}",
                type: "DELETE",
                data: {
                    _token: $("input[name=_token]").val(),
                    ids: allIds
                },
                success: function(response){
                    $.each(ids, function(key, val){
                        $("#row-"+val).remove(); 
                    })
                }
            });
        });
    });
</script>

Here is the delete function within my CategoryController

public function delete(Request $request)
{
   dd($request->all());
}

Solution

In think you must change your routes order:

your web.php file could be like this:

Route::middleware(["auth"])->group(function () {

    // this is the route i am targeting
    Route::delete("/categories/delete", [CategoryController::class, "delete"])->name("categories.delete"); 

    Route::resources([
        'categories' => CategoryController::class,
        'posts' => PostsController::class,
    ]);
    
});

If you want to add custom route to resource routing, you must use custom ones before resource route. for more information go to resource.



Answered By - Hamid Moladoust
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing