Jump to content

Efficient way to add / remove new textarea data


Cordion

Recommended Posts

Hello everyone!

I've been on a problem a few hours now and I've managed to finally find the solution however I feel it's very inefficient and am looking for a way to do this in a better way.

I have a textarea where each line is inserted into MySQL database table "lines" and I have the columns "line_number" and "row".

The textarea initial value on page load is simply a list of all lines ordered by line_number in ASC order.

If I modify/add/remove entries from the textarea it will remove data changed and add new data accordingly.

But I am making a lot of queries to do this and I'm not sure if there is a more efficient way than querying each line in the database to find the new value/modified value.

Here's my php script, hopefully this makes sense:

 

if (isset($_POST['lines'])) {
    
    //get lines posted from textarea
    $lines_posted = $_POST['lines'];
    
    //trim all white spaces and remove empty lines as they are not valid for mysql insert
    $lines = trim($lines_posted);
    $lines = explode("\n", str_replace("\r", "", trim($lines_posted)));
    $lines = array_map('trim', $lines_posted);
    $lines = array_filter($lines_posted);
    
    $line_count = count($lines);
    //output how many valid lines were posted
    echo "Posted Lines: $line_count<br>";
    
    //loop through all lines posted, query all rows in database and insert if lines row value does not exist
    for ($i = 0; $i < $line_count; $i++) {
        if (isset($lines[$i])) {
            $find_line_in_db = $db->query("SELECT row FROM lines WHERE row=?", $lines[$i]);
            if ($find_line_in_db->numRows() == 0) {
                $db->query("INSERT INTO lines (line_number, row) VALUES (?,?)", $i, $lines[$i]);
                echo 'Added the line: ', htmlspecialchars($lines[$i]), '<br>';
            }
        }
    }
    
	//query all rows in the database and check if posted lines array value is in the [row] column, delete rows changed/not found accordingly
    $rows             = "";
    $fetch_all_lines  = $db->query("SELECT row FROM lines");
    while ($all_lines = $fetch_all_lines->fetchRow()) {
        if (!in_array($all_lines['row'], $lines)) {
            $rows = $all_lines['row'];
            $db->query("DELETE FROM lines WHERE row=?", $all_lines['row']);
            echo 'Deleted the line: ', htmlspecialchars($lines[$i]), '<br>';
        }
    }
}

 

So to visualize what I'm doing imagine two arrays:

 

$lines = ["line1", "line2", "line3changed"] //textarea lines posted
$db_lines = ["line1", "line2", "line3"] 
//$db_lines = the current rows in lines db table
// line_number | row
// 0           | line1
// 1           | line2
// 2           | line3

//outputs 
//Added the line: line3changed
//Deleted the line: line3

 

So basically what I'm asking is, am I doing this the right way?

I would have over 10,000 lines that would be altered every hour in this table and am unsure if I'm doing it the correct way

Link to comment
Share on other sites

Why do you need to track each line individually? It seems like overkill to me, but maybe the business logic has a reason for it... I mean, honestly, a database of 10,000 records really isn't that big a deal, but I think it would be more efficient to track versions of the full text area contents. If you need to you can check the differences in PHP on display using something like xdiff_string_diff(), or you can track the character numbers changed in a separate table and highlight those changes by using those character offset values and substr() or something.

Edited by maxxd
Link to comment
Share on other sites

+1 to the full contents. Does each line have a specific meaning? Is the textarea just an easy way to enter all of them at once? Is the user copy/pasting stuff?

In general, unless you have to come up with a particularly efficient solution, DELETEing everything and then INSERTing it (preferably with a prepared statement) all anew is easiest.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.