Search Array() in variable and database

Asked

Viewed 153 times

2

I have the following fields in Array():

# Tratamento das variaveis para substituição da mensagem
$campos = array(
    0 => '#celular',
    1 => '#ddd',
    2 => '#cpf_cnpj',
    3 => '#nome_razaosocial',
    4 => '#mensagem',
    5 => '#cod_cliente',
    6 => '#cod_contrato',
    7 => '#cod_barras',
    8 => '#valor_divida',
    9 => '#telefone_r_1',
    10 => '#telefone_r_2',
    11 => '#telefone_r_3'
);

And I also have a variable:

$dados['mensagem'] = "Aqui mensagem #celular #ddd #cod_barras";

And I have the value of these tags, in the fingers bank, on the table files_fields

I need to check in $message if any of these available tags exist, and replace.

I tried it this way:

<?php
    $sql_dados = mysqli_query($conn, "SELECT * FROM files_fields LIMIT 5");
    while($row_dados=mysqli_fetch_assoc($sql_dados)){
?>
<tr>
    <td><?php echo $row_dados['celular']; ?></td>
    <td>
        <?php 
            //foreach($campos_arr as $valor){ 
                if(in_array($campos, $dados['mensagem'])){ 
                    $mensagem = str_replace($campos, $row_dados[$valor], $dados['mensagem']); 
                } else {
                    $mensagem = $_GET['mensagem'];
                }
            //  echo $mensagem;
        //  } 
        ?>
    </td>

</tr>
<?php } ?>

But returns the following error:

Warning: in_array() expects Parameter 2 to be array, string Given in D: Sites Localhost Easyphp-Devserver-14.1VC11 data localweb dashboard-Wallace php gerarAmostra.php on line 46

How can I elaborate in the right way?

  • to check with in_array the second parameter must also be a array

  • $data['message'] = $_GET['message'];

  • I edited, the right way

  • It seems that call from in_array() is reversed anyway I think it will not give the expected result.

  • $mensagem has only hashtags?

  • No $message can have message and hastag in between

Show 1 more comment

1 answer

1


I thought of something like this:

<?php

$campos = [
   0  => '#celular',
   1  => '#ddd',
   2  => '#cpf_cnpj',
   3  => '#nome_razaosocial',
   4  => '#mensagem',
   5  => '#cod_cliente',
   6  => '#cod_contrato',
   7  => '#cod_barras',
   8  => '#valor_divida',
   9  => '#telefone_r_1',
   10 => '#telefone_r_2',
   11 => '#telefone_r_3'
];

$valor = [
   0  => '(19) 99999-9999',
   1  => '999',
   2  => '999.999.999-99',
   3  => 'Teste de Razão Social',
   4  => 'Obrigado por me testar',
   5  => '99',
   6  => '999',
   7  => '99999999.99999999.99999999.9999999.9.9999999',
   8  => '9.99',
   9  => '(19) 99999-9999',
   10 => '(19) 99999-9999',
   11 => '(19) 99999-9999'
];

$dados['mensagem'] = 'Meu celular é #celular e meu código do cliente é #cod_cliente. Segue o código de barras: #cod_barras';
$mensagem = $dados['mensagem'];

foreach($campos as $key => $item){
   if(strpos($dados['mensagem'], $item)){
      $mensagem = str_replace($item, $valor[$key], $mensagem);
   }
}

echo $mensagem;

See working on the sites:

Sandbox PHP

Ideone

Explanation

strpos() - Function that searches the word in a given string. If it exists, the return is the position of the word in the string. When it does not find it returns -1.

I did the foreach to search in the array $campos if one or more fields exist in the variable $mensagem. If it exists, replace it with the value of the same array position $valor, which in your case is a variable that comes from the database.


In your case if the name of the fields is equal to those of the array it can be done so:

$column = str_replace('#', '', $item);
$mensagem = str_replace($item, $row_dados[$column], $mensagem);

Cop # from the field with the str_replace().

Or

$mensagem = str_replace($item, $row_dados[$key + 1], $mensagem);

$key + 1 why you have the table ID field probably (I don’t know your table)

  • $value is $Row, from the database in this case

  • Yeah. I just set an example for you to see how I would...

  • But can you modify it for me to see how the return coming from the database would look? inside the while, in this case, as I posted above?

  • will be able to edit?

  • Table fields are in the sequence of Array fields $campos ?

  • Yes you are. ....

  • 1

    I updated my post... http://www.ecrau.com/wp-content/uploads/2016/02/poste.jpg

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.