insert an apostrophe before a query to excel

1

I have this result of a query

$consulta .= '<td>' . $v . '</td>';

where the variable ' . $v . ' is a number that contains leading zeros, so that excel recognizes that number in text format, I must add an apostrophe 'before the number the problem is that php separates the variables with apostrophes, like I insert the apostrophe before the variable something like this but it generates an error because the apostrophe does not close in php: ' ' . $v . '

the result of the query should look like this: '000018

while($row = mysqli_fetch_assoc($stmt)){
    $data[$i]['id'] = $i;
    $data[$i]['codigo_op'] = $row['codigo_op'];
    $data[$i]['entrega01_clave'] = $row['entrega01_clave'];
    $data[$i]['descripcion_articulo'] = $row['descripcion_articulo'];
    $i++;
}

only the code_op field must have the apostrophe before the number

    
asked by Ivan Diaz Perez 31.08.2017 в 15:46
source

2 answers

1

According to your code, you could use:

while($row = mysqli_fetch_assoc($stmt)){ 
    $data[$i]['id'] = $i; 
    $data[$i]['codigo_op'] = "'".$row['codigo_op']; 
    $i++; 
}

Consider that if you use $data[$i]['codigo_op'] for other operations you may have errors, then use two variables, one to paint the excel and another for operations:

while($row = mysqli_fetch_assoc($stmt)){ 
    $data[$i]['id'] = $i; 
    //para operaciones
    $data[$i]['codigo_op'] = $row['codigo_op']; 
    //para excel
    $data[$i]['codigo_op_excel'] = "'".$row['codigo_op']; 
    $i++; 
}
    
answered by 31.08.2017 / 16:31
source
2

You have some options:

//segun tu codigo, usar backslah (\) antes del apostrofe
$consulta .= '<td>\'' . $v . '</td>';

or:

//usar comillas ("") para encerrar el codigo html 
$consulta .= "<td>'" . $v . "</td>";

or:

//usar comillas ("") y no usar concatenadores, php detecta que $v es una variable y reemplaza su valor 
$consulta .= "<td>'$v</td>";
    
answered by 31.08.2017 в 16:15