Foros del Web » Programando para Internet » PHP »

Consultar a 2 tablas

Estas en el tema de Consultar a 2 tablas en el foro de PHP en Foros del Web. Hola Amigos!!! estoy trabajando con datatables y viene con un script para trabajar del lado del servidor pero estoy tratando de modificarlo para llamar a ...
  #1 (permalink)  
Antiguo 12/07/2011, 13:05
 
Fecha de Ingreso: octubre-2010
Ubicación: Buenos Aires
Mensajes: 557
Antigüedad: 13 años, 6 meses
Puntos: 4
Consultar a 2 tablas

Hola Amigos!!!
estoy trabajando con datatables y viene con un script para trabajar del lado del servidor
pero estoy tratando de modificarlo para llamar a otra tabla ademas de la que estoy usando y no lo puedo lograr
dejo el codigo para ver si alguien me puede ayudar a decirme que parte del codigo tengo que modificar y hacer la consulta a varias tablas y asi poder relacionarlas
saludos y muchas gracias por cualquier ayuda que puedan brindarme
Código PHP:
Ver original
  1. require_once('class/class.php');
  2.      
  3.      
  4.    
  5.     $aColumns = array( 'articulo_id', 'descripcion',  'detalle', 'proce' , 'moneda' );
  6.    
  7.     /* Indexed column (used for fast and accurate table cardinality) */
  8.     $sIndexColumn = "articulo_id";
  9.    
  10.     /* DB table to use */
  11.     $sTable = "articulos";
  12.    
  13.    
  14.      
  15.     $sLimit = "";
  16.     if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
  17.     {
  18.         $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  19.             mysql_real_escape_string( $_GET['iDisplayLength'] );
  20.     }
  21.    
  22.    
  23.     /*
  24.      * Ordering
  25.      */
  26.     $sOrder = "";
  27.     if ( isset( $_GET['iSortCol_0'] ) )
  28.     {
  29.         $sOrder = "ORDER BY  ";
  30.         for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
  31.         {
  32.             if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  33.             {
  34.                 $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
  35.                     ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  36.             }
  37.         }
  38.        
  39.         $sOrder = substr_replace( $sOrder, "", -2 );
  40.         if ( $sOrder == "ORDER BY" )
  41.         {
  42.             $sOrder = "";
  43.         }
  44.     }
  45.    
  46.    
  47.     /*
  48.      * Filtering
  49.      * NOTE this does not match the built-in DataTables filtering which does it
  50.      * word by word on any field. It's possible to do here, but concerned about efficiency
  51.      * on very large tables, and MySQL's regex functionality is very limited
  52.      */
  53.     $sWhere = "";
  54.     if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
  55.     {
  56.         $sWhere = "WHERE (";
  57.         for ( $i=0 ; $i<count($aColumns) ; $i++ )
  58.         {
  59.             $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  60.         }
  61.         $sWhere = substr_replace( $sWhere, "", -3 );
  62.         $sWhere .= ')';
  63.     }
  64.    
  65.     /* Individual column filtering */
  66.     for ( $i=0 ; $i<count($aColumns) ; $i++ )
  67.     {
  68.         if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
  69.         {
  70.             if ( $sWhere == "" )
  71.             {
  72.                 $sWhere = "WHERE ";
  73.             }
  74.             else
  75.             {
  76.                 $sWhere .= " AND ";
  77.             }
  78.             $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  79.         }
  80.     }
  81.    
  82.    
  83.     /*
  84.      * SQL queries
  85.      * Get data to display
  86.      */
  87.     $sQuery = "
  88.         SELECT  SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
  89.         FROM   $sTable
  90.         $sWhere
  91.         $sOrder
  92.         $sLimit
  93.     ";
  94.     $rResult = mysql_query( $sQuery,Conectar::con() ) or die(mysql_error());
  95.    
  96.     /* Data set length after filtering */
  97.     $sQuery = "
  98.         SELECT FOUND_ROWS()
  99.     ";
  100.     $rResultFilterTotal = mysql_query( $sQuery, Conectar::con() ) or die(mysql_error());
  101.     $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  102.     $iFilteredTotal = $aResultFilterTotal[0];
  103.    
  104.     /* Total data set length */
  105.     $sQuery = "
  106.         SELECT COUNT(".$sIndexColumn.")
  107.         FROM   $sTable
  108.     ";
  109.     $rResultTotal = mysql_query( $sQuery, Conectar::con() ) or die(mysql_error());
  110.     $aResultTotal = mysql_fetch_array($rResultTotal);
  111.     $iTotal = $aResultTotal[0];
  112.    
  113.    
  114.     /*
  115.      * Output
  116.      */
  117.     $output = array(
  118.         "sEcho" => intval($_GET['sEcho']),
  119.         "iTotalRecords" => $iTotal,
  120.         "iTotalDisplayRecords" => $iFilteredTotal,
  121.         "aaData" => array()
  122.     );
  123.    
  124.     while ( $aRow = mysql_fetch_array( $rResult ) )
  125.     {
  126.         $row = array();
  127.         for ( $i=0 ; $i<count($aColumns) ; $i++ )
  128.         {
  129.             if ( $aColumns[$i] == "version" )
  130.             {
  131.                 /* Special output formatting for 'version' column */
  132.                 $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  133.             }
  134.             else if ( $aColumns[$i] != ' ' )
  135.             {
  136.                 /* General output */
  137.                 $row[] = $aRow[ $aColumns[$i] ];
  138.             }
  139.         }
  140.         $output['aaData'][] = $row;
  141.     }
  142.    
  143.     echo json_encode( $output );
  #2 (permalink)  
Antiguo 12/07/2011, 16:54
Avatar de maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 9 meses
Puntos: 1532
Respuesta: Consultar a 2 tablas

SQL INNER JOIN, left join ó right join.
__________________
¡Por favor!: usa el highlight para mostrar código
El que busca, encuentra...
  #3 (permalink)  
Antiguo 12/07/2011, 17:00
 
Fecha de Ingreso: octubre-2010
Ubicación: Buenos Aires
Mensajes: 557
Antigüedad: 13 años, 6 meses
Puntos: 4
Respuesta: Consultar a 2 tablas

buenisimo!!! Graciias
  #4 (permalink)  
Antiguo 20/10/2011, 09:56
Avatar de ale80262  
Fecha de Ingreso: junio-2008
Mensajes: 66
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: Consultar a 2 tablas

Genial gracias

Etiquetas: consultar, mysql, tabla, tablas
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:40.