Insert line break in field text in database

5

I have a table with a field type text , the field stores a string string imported via file .csv , it was inserted without a line break and I'm trying to insert it, that the line break should happen after the "-", hyphen hyphen space, I tried this code snippet:

<?php

include('Connections/conexao.php');

mysql_select_db($database_conexao, $conexao);
$query_rsPesquisa = "SELECT * FROM  'produtos' WHERE status = 1";
$rsPesquisa = mysql_query($query_rsPesquisa, $conexao) or die(mysql_error());
$row_rsPesquisa = mysql_fetch_assoc($rsPesquisa);
$totalRows_rsPesquisa = mysql_num_rows($rsPesquisa);


do {

    $id_produto = 10;

    $string = $row_rsPesquisa['detalhes'];  

        if( strstr($string," - ")){         
            $novaString = wordwrap($string, 20, "<br />\n");            
            $UpDetalhe ="UPDATE produtos SET detalhes = $novaString WHERE id_produto = $id_produto;";
            $sucesso = mysql_query($UpDetalhe) or die(mysql_error("Erro ao alterar registro")); 

            if ($sucesso > 0) {
                echo "Próximo registro";
            }

        }

} while ($row_rsPesquisa = mysql_fetch_assoc($rsPesquisa));

?>

Plain text looks like this:

"Measures of the products of the photo (s): - 3 Places + Chaise: - Width: 281 - Depth: 161 - Height: 94 - 3 Places: - Width: 225 - Depth: 94 - Height: 94 - 2 Places: - Width: 170 - Depth: 94 - Height: 94 - Modulated 2 Places + Corner + 3 Places: - Width (2 Places + Corner): 246 - Width (3 Places + Depth: 94 - Height: 94 - Size Options: - 1 Place - 2 Places - 3 Places - Modulated (seat, chaise and corner) - Model also has puff option - Coating: - Various options in fabric and synthetic leather . "

After my attempt it looked like this:

Medidas dos<br />
produtos da(s)<br />
foto(s) (cm):2<br />
Lugares: -  <br />
Largura: 212 -  <br />
Profundidade: 90 -  <br />
Altura: 86 - 3<br />
Lugares + Chaise: - <br />
 Largura: 212 -  <br />
Profundidade: 155 - <br />
 Altura: 86 -<br />
Opções de tamanho:<br />
-   2 Lugares -   3<br />
Lugares -   Modulado<br />
(assento, chaise e<br />
canto). -<br />
Revestimento: -  <br />
Várias opções em<br />
couro natural e<br />

I would like it to look like this:

Medidas dos produtos da(s) da(s) foto(s) (cm):
2 Lugares: 
- Largura: 212 
- Profundidade: 90 
- Altura: 86 
3 Lugares + Chaise: 
- Largura: 212 
- Profundidade: 155 
- Altura: 86 
- Opções de tamanho:
- 2 Lugares 
- 3 Lugares 
- Modulado (assento, chaise ecanto). 
- Revestimento: 
- Várias opções em couro natural e couro sintético.

In the variable view on my page I'm using this, unsuccessfully:

echo nl2br($row_rsProdutos['detalhes']);
    
asked by anonymous 08.02.2015 / 14:17

3 answers

2

If you have a standard give it to make it easy. If it does not have a clear pattern, it gets tricky or impossible. If it was not a posting error its (seems to be, after all it's impossible for the data source to give the result you said you want, the data does not hit, it's different things) can not do exactly how you posted, or the pattern is very complicated and the algorithm would have to be equally complicated.

If I understand the pattern you can do this:

<?php
include('Connections/conexao.php');

mysql_select_db($database_conexao, $conexao);
$query_rsPesquisa = "SELECT detalhes FROM  'produtos' WHERE status = 1";
$rsPesquisa = mysql_query($query_rsPesquisa, $conexao) or die(mysql_error());
$row_rsPesquisa = mysql_fetch_assoc($rsPesquisa);
$totalRows_rsPesquisa = mysql_num_rows($rsPesquisa);

do {
    $id_produto = 10;
    $string = $row_rsPesquisa['detalhes'];  

        if( strstr($string," - ")){         
            do {
                $posicao = strpos($texto, "-", 1);
                $linhaQuebrada .= ($posicao ? substr($texto, 0, $posicao) : $texto) . "\r\n";
                $texto = substr($texto, $posicao);
            } while ($posicao);

            $UpDetalhe ="UPDATE produtos SET detalhes = $linhaQuebrada WHERE id_produto = $id_produto;";
            $sucesso = mysql_query($UpDetalhe) or die(mysql_error("Erro ao alterar registro")); 

            if ($sucesso > 0) {
                echo "Próximo registro";
            }
        }
} while ($row_rsPesquisa = mysql_fetch_assoc($rsPesquisa));

See the relevant part working on ideone .

Note that I've just gotten the detalhes field, if you're only going to manipulate it, you do not have to use * in select , this creates a huge overhead .

If the pattern is not this well, explain it better. I have seen that there is a difference in some lines that can be worked otherwise. That is, it is possible to separate the way of treating the products since they have an extra spacing. But you need to ensure that there is such a pattern. Your post should not show a clear pattern.

And you need to decide whether to use <br> or \r\n . The code indicates that it is <br> and originally did so. Your edit says it will use normal text line wrap and then will convert to HTML line wrap when submitting. Without consistency it becomes difficult to understand the problem.

See the alternative working on ideone . It could have used two-thirds but it would have been less legible.

Remembering that if some data is badly formatted, it will not work.

    
08.02.2015 / 16:59
1

nl2br: Inserts HTML line breaks before all newlines in a string. Syntax: nl2br ($ string);

Operation of the thing: By giving "enter" to break line in the textarea field, these "enters" are preserved and inserted, along with the data, into the database.

Example:

print "<p>".nl2br($produto['detalhes_do_produto'])."</p>";

RESULT

Estou testando esta área com quebra de linha
acabei de dar um enter
mais um
e mais um

source: link

    
08.02.2015 / 15:23
1

The best way to do this is through a regular expression. Using the preg_split() function, you can separate the string with - . It works like this:

$string = $row_rsPesquisa['detalhes'];  
$id_produto = 10;

    if( strstr($string," - ")){ 
        $novaString = preg_split("/( - )/",$string); //vai retornar um array            
        foreach ($novaString as $pedaco){ //loop em todos os pedaços da string
            $UpDetalhe ="UPDATE produtos SET detalhes = $pedaco WHERE id_produto = $id_produto;";
            $sucesso = mysql_query($UpDetalhe) or die(mysql_error("Erro ao alterar registro")); 
        }
     }
    
08.02.2015 / 14:52