Join two columns

Join two columns

tumbero_xtumbero_x Posts: 63Questions: 0Answers: 0
edited July 2011 in General
hello Good Day!
server_processing'm working with and I can not show me 2 fields in a single column mysql html
there is a way?
Greetings and Thanks!

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    why can't you? please elaborate. and why do you want to?
  • tumbero_xtumbero_x Posts: 63Questions: 0Answers: 0
    hello Good Day!
    I need to make a column with two records left me for example
    if from the database get two fields, I need these two fields are in the same column, but always using server side processing
    such records provider and need proof that they are in one column together
    leave such an image is understood better and I'm using a translator to establish a conversation
    Greetings from Argentina!
    http://www.sincroniavirtual.com/table.png
  • cdaiglecdaigle Posts: 57Questions: 0Answers: 0
    In your php processing file, you want to combine the contents when you retrieve them. Can you show us your server-side code?

    If you can't, what you want to do is loop through your aaData array that you are passing to dataTables. When you do so, create a new array that copies each element, and combine the 2 columns then.

    For example,
    [code]
    $new_aaData = array();
    foreach($aaData as $index => $row) {
    $new_aaData[$index] = array($ow[0], $row[1] . " " . $row[2], $row[3], $row[4], $row[5], $row[6]);
    }
    [/code]
    Here, I combined columns 2 and 3 (1 and 2 if using 0 as base index). Instead of passing $aaData to datatables, now you want to pass $new_aaData from the server-side.

    You will also need to change the number of columns that dataTables thinks it is getting, since you combined two.

    Hope this helps,
    Chris.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    your database will have functions to combine/concatenate values. in mysql, use CONCAT()

    [code]
    SELECT CONCAT(firstname, ' ', middleinitial, '. ', lastname) AS fullname FROM person;
    [/code]

    this returns 3 columns (firstname, middleinitial, lastname) as one single column
  • genealogy_guygenealogy_guy Posts: 10Questions: 0Answers: 0
    edited July 2011
    I had a need to combine two columns from the database as well as some text - full_name, "my text", aka_name. But I only wanted this combined when there was data in the aka_name field.

    [code]
    "aoColumnDefs": [
    /* Name */
    { "sName": "name", "aTargets": [ 2 ] },
    /* AKA_name */
    { "sName": "aka_name", "aTargets": [ 3 ] },
    { "bVisible": false, "aTargets": [ 3 ] },
    /* Combine Name & AKA_name as needed */
    { "fnRender": function ( oObj ) {
    if ( oObj.aData[3] != "" ) {
    return oObj.aData[2] +'
    a.k.a.
    '+ oObj.aData[3];
    }
    else {
    return oObj.aData[2];
    }
    },
    "aTargets": [ 2 ]
    },],
    [/code]

    So for persons without aka_name, table cell contained "John Smith"
    For persons with aka_name, table cell contained "John Smith
    a.k.a.
    Johnny Smyth"
    And as aka_name column was being combined into name column, I could make the aka_name column invisible.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    mysql also has an IF() function for conditional stuff: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
    [code]
    SELECT IF(aka != "", CONCAT(fullname, " a.k.a. ", aka), fullname) AS name FROM table;
    [/code]
  • tumbero_xtumbero_x Posts: 63Questions: 0Answers: 0
    edited August 2011
    Hello Friends!
    Thank you very much everyone for your answers but try everything and not be able to put it
    I leave my code to see if anyone can help me
    greetings and thank you very much!

    [code]<?php
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Easy set variables
    */

    /* Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)

    */
    require_once('class/class.php');


    $aColumns = array('compra_id','razon_social','compra_fc','compra_num_suc','compra_num_com','moneda_id','compra_coti','compra_total','compra_usuario','compra_fecha');

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "compra_id";

    /* DB table to use */
    $sTable = "compras";



    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
    $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
    mysql_real_escape_string( $_GET['iDisplayLength'] );
    }


    /*
    * Ordering
    */
    $sOrder = "";
    if ( isset( $_GET['iSortCol_0'] ) )
    {
    $sOrder = "ORDER BY ";
    for ( $i=0 ; $i $iFilteredTotal,
    "aaData" => array()
    );

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $row = array();
    for ( $i=0 ; $i[/code]





    ?>
  • tumbero_xtumbero_x Posts: 63Questions: 0Answers: 0
    ya esta solucionado !!! agrego la linea que hay que modificar por si alguien tiene el mismo problema
    Saludos y muchas gracias
    [code] $aColumns = array('compra_id','razon_social','compra_fc','CONCAT(compra_num_suc,"-",compra_num_com)','moneda_id','compra_coti','compra_total','compra_usuario','DATE_FORMAT(compra_fecha,"%d-%m-%Y")'); [/code]
  • carrguerrcarrguerr Posts: 1Questions: 0Answers: 0
    <?php
    include( "../../db.php" );
    /* Database connection information */
    $gaSql['user'] = $user;
    $gaSql['password'] = $pswd;
    $gaSql['db'] = $bd;
    $gaSql['server'] = $localhost;

    /* * MySQL connection*/
    $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
    die( 'Could not open connection to server' );

    mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
    die( 'Could not select database '. $gaSql['db'] );


    /* * Paging */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' ){
    $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
    mysql_real_escape_string( $_GET['iDisplayLength'] );
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* ARREGLO PARA LAS BUSQUEDAS Y ORDERBY*/
    $aColumns = array( 'alumnos.id','alumnos.matricula','alumnos.curp','alumnos.nombre','alumnos.nivel',"CONCAT_WS(',',alumnos.grupo,alumnos.anio)",'colegiatura.cuota',
    "CONCAT_WS(',',padres.padre,padres.madre)","CONCAT_WS(',',padres.correo_p,padres.correo_m)", 'padres.rfc' );
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /** Ordering*/
    $sOrder = "";
    if ( isset( $_GET['iSortCol_0'] ) ){
    $sOrder = "ORDER BY ";
    for ( $i=0 ; $i $iFilteredTotal,
    "aaData" => array()
    );

    $nivel=array("1"=>"Preescolar","2"=>"Primaria","3"=>"Secundaria");

    /* ARREGLO PARA USAR SOLO ABAJO */
    $aColumnsX = array( 'id','matricula','curp','nombre','nivel','gruposx','cuota','padresn','padrese', 'rfc' );

    while ( $aRow = mysql_fetch_array( $rResult ) ){
    $row = array();
    for ( $i=0 ; $i
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    For an example script which joins two tables, have a look at this: http://datatables.net/dev/server_processing.txt

    Allan
This discussion has been closed.