Asked  1 Year ago    Answers:  5   Viewed   7 times

In a CodeIgniter app, I've a textarea for content entry. I'd like to retain new lines but I'm having the issue that when the data is saved, an extra new line is added with each new line, which then displays in the textarea next time I edit.

How can I get rid of this extra new line, preferably before the data is saved?

mysql_real_escape_string shows the data as follows:

this is the first linernnthis is the second linernnthis is the third line

Which displays in the textarea like so:

this is the first line

this is the second line

this is the third line

I just want this:

this is the first line
this is the second line
this is the third line

 Answers

4

This is a known bug in the Reactor version of CI 2.0 This was driving me nuts all week. I'm not sure if there has been an official patch yet, but you can find a workaround here:

http://codeigniter.com/forums/viewthread/180078/

More info:

https://bitbucket.org/ellislab/codeigniter/issue/332/newlines-in-textareas-are-duplicated

Saturday, May 29, 2021
 
Jubair
 
2

I would say just build it yourself. You can set it up like this:

$query = "INSERT INTO x (a,b,c) VALUES ";
foreach ($arr as $item) {
  $query .= "('".$item[0]."','".$item[1]."','".$item[2]."'),";
}
$query = rtrim($query,",");//remove the extra comma
//execute query

Don't forget to escape quotes if it's necessary.

Also, be careful that there's not too much data being sent at once. You may have to execute it in chunks instead of all at once.

Thursday, April 1, 2021
 
4

Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.

Saturday, May 29, 2021
 
2

The function you're looking for is find_in_set:

 select * from ... where find_in_set($word, pets)

for multi-word queries you'll need to test each word and AND (or OR) the tests:

  where find_in_set($word1, pets) AND find_in_set($word2, pets) etc 
Saturday, May 29, 2021
 
ammezie
 
5

Here's an example with a table of mine:

<table>
    <tr>

<?php
$mysqli = new mysqli("localhost", "user", "pass", "test");
/*
 * CREATE TABLE `test` (
 `col1` int(11) NOT NULL,
 `col2` int(11) NOT NULL
... // until col5
 */
$query = "SELECT column_name FROM information_schema.columns 
    WHERE table_name = 'test' AND table_schema = 'test'";
$result = $mysqli->query($query);

// INSERT INTO `test`.`test` (`col1`, `col2`, `col3`, `col4`, `col5`)
// VALUES ('10', '11', '12', '13', '14'), ('15', '16', '17', '18', '10');

$query2 = "SELECT * FROM test";
$result2 = $mysqli->query($query2);


while ($row = $result->fetch_assoc()) {

    ?>
        <td><?=$row['column_name'];?></td>

<?php
}
?>
        </tr>
        <tr>

        <?php
 while ($res = $result2->fetch_assoc()) {

    $count_columns = $result->num_rows;

        for ($i = 1; $i <= $count_columns; $i++) {
     ?>
        <td><?=$res['col'.$i.''];?></td>
        <?php
        }
        ?>
        </tr>
        <tr>
<?php

 }

Output:

col1    col2    col3    col4    col5
 10     11      12       13     14
 15     16      17       18     10

That's without knowing the column names and their count. It's enough they have the same prefix "col".

After adding one new column:

    ALTER TABLE `test` ADD `col6` INT NOT NULL 
    UPDATE test SET col6 = col1+1

The same code (without any change) produces:

col1    col2    col3    col4    col5    col6
 10     11      12       13     14      11
 15     16      17       18     10      16

P.S.: I'm still not encouraging this table structuring, where you need to add columns dynamicly

Saturday, May 29, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :