help with using CONCAT in php

1

I'm making a query and I want the ID to tell me with the ID and name in the interface and I'm using this code

<?php
include('conexion.php');
$paginaActual = $_POST['partida'];

$nroProductos = mysql_num_rows(mysql_query("SELECT * FROM reporte"));
$nroLotes = 10;
$nroPaginas = ceil($nroProductos/$nroLotes);
$lista = '';
$tabla = '';

if($paginaActual > 1){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual-1).');">Anterior</a></li>';
}
for($i=1; $i<=$nroPaginas; $i++){
    if($i == $paginaActual){
        $lista = $lista.'<li class=""><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }else{
        $lista = $lista.'<li><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }
}
if($paginaActual < $nroPaginas){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual+1).');">Siguiente</a></li>';
}

if($paginaActual <= 1){
    $limit = 0;
}else{
    $limit = $nroLotes*($paginaActual-1);
}

$registro = mysql_query("SELECT * FROM reporte LIMIT $limit, $nroLotes ");


$tabla = $tabla.'<table class="table table-bordered table-inverse">
                    <tr class="bg-primary">
                      <th width="150">emergencia</th>
                        <th width="150">descripcion emergencia</th>
                        <th width="200">nombre del reportante</th>
                        <th width="150">telefono</th>
                        <th width="150">ubicacion</th>
                        <th width="150">nivel de la emergencia</th>
                         <th width="150">opciones</th>

                    </tr>';

while($registro2 = mysql_fetch_array($registro)){
    $tabla = $tabla.'<tr>
    <td>'.$registro2['nombre'].'</td>
                        <td>'.$registro2['desc_emergencia'].'</td>
                        <td>'.$registro2['nombre_reportante'].'</td>
                        <td> '.$registro2['telefono_reportante'].'</td>
                        <td> '.$registro2['ubicacion'].'</td>

                                <td> '.$registro2['nivel_emergencia'].'</td>

                        <td><a href="javascript:editarProducto('.$registro2['id'].');" class="glyphicon-pencil"></a> <a href="javascript:eliminarProducto('.$registro2['id'].');" class="glyphicon glyphicon-trash"></a></td>
                      </tr>';       
}


$tabla = $tabla.'</table>';



$array = array(0 => $tabla,
               1 => $lista);

echo json_encode($array);

? >

Where do I use the concat so that in my interface I get the ID as ID and name together? Greetings.

    
asked by Juan Lozano 13.02.2017 в 19:05
source

1 answer

4

Good,

In your query I would add the CONCAT_WS of mysql function. It would stay like this.

$registro = mysql_query("SELECT CONCAT_WS(' ', id, nombre) as codigo, * FROM reporte LIMIT $limit, $nroLotes ");

Where the first parameter is the string separator between the selected fields.

Then in PHP you get it as:

while($registro2 = mysql_fetch_array($registro)){
    $tabla = $tabla.'<tr>
            <td>'.$registro2['campoconcatenado'].'</td>
            <td>'.$registro2['nombre'].'</td>
            <td>'.$registro2['desc_emergencia'].'</td>
            <td>'.$registro2['nombre_reportante'].'</td>
            <td> '.$registro2['telefono_reportante'].'</td>
            <td> '.$registro2['ubicacion'].'</td>
            <td> '.$registro2['nivel_emergencia'].'</td>
            <td><a href="javascript:editarProducto('.$registro2['id'].');" class="glyphicon-pencil"></a> <a href="javascript:eliminarProducto('.$registro2['id'].');" class="glyphicon glyphicon-trash"></a></td>
            </tr>';       
}

Regarding PHP

It would only suffice to concatenate the two strings in this way, in case you want to separate them by some string, but omit the ""

$registro2['id']."".$registro2['nombre']

I hope it serves you, you tell me Greetings,

    
answered by 13.02.2017 в 19:31