Page 1 of 1

Build a dinamyc code with mysql_field_name query.

Posted: Mon Feb 04, 2008 12:39 pm
by josh27
Hello again, i have this code

Code: Select all

 
 
    $objGrid -> FormatColumn("id", "ID", 10, 10, 0, "10", "center", "integer");
    $objGrid -> FormatColumn("alumno_name", "Nombre", 30, 30, 0, "105", "left");
    $objGrid -> FormatColumn("alumno_apellido", "Apellido", 30, 30, 0, "110", "left");
    $objGrid -> FormatColumn("alumno_ano", "Ano", 3, 3, 0, "30", "center");
    $objGrid -> FormatColumn("alumno_sec", "Secc", 5, 5, 0, "30", "left");
    $objGrid -> FormatColumn("curso01", "curso01", 5, 5, 0, "30", "left");
 
 
This code format my datagrid, column by column, now. the goal is writte a php code that gets new field name from the mysql database and inert this in the end... i think like this

Code: Select all

 
 
$con = mysql_connect("localhost", "db_test", "test");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
$db_selected = mysql_select_db("table_test",$con);
 
$sql = "SELECT * from authors";
 
$result = mysql_query($sql,$con);
 
$name1 = mysql_field_name($result, 2);
 
    $objGrid -> FormatColumn("id", "ID", 10, 10, 0, "10", "center", "integer");
    $objGrid -> FormatColumn("alumno_name", "Nombre", 30, 30, 0, "105", "left");
    $objGrid -> FormatColumn("alumno_apellido", "Apellido", 30, 30, 0, "110", "left");
    $objGrid -> FormatColumn("alumno_ano", "Ano", 3, 3, 0, "30", "center");
    $objGrid -> FormatColumn("alumno_sec", "Secc", 5, 5, 0, "30", "left");
    $objGrid -> FormatColumn("curso01", "curso01", 5, 5, 0, "30", "left");
    [b]$objGrid -> FormatColumn("$name1", "$name1", 5, 5, 0, "30", "left");[/b]
 
Maybe i need a loop that increment "$name1"?... how i can do this. thanks

Re: Build a dinamyc code with mysql_field_name query.

Posted: Mon Feb 04, 2008 6:01 pm
by josh27
I do this...

Code: Select all

 
 
<?php
 
$con = mysql_connect("localhost", "db_test", "test");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
$db_selected = mysql_select_db("db_test",$con);
 
$sql = "SELECT * from authors";
 
$result = mysql_query($sql,$con);
 
$campos = mysql_num_fields($result);
 
$nuevos = $campos - 3 ; // i dont need the last 3 fields....  and the 5 first fields.
 
 
for ( $counter = 5; $counter < $nuevos; $counter += 1) {
    
    $name = mysql_field_name($result, "$counter");
    
    $objGrid -> FormatColumn("id", "ID", 10, 10, 0, "10", "center", "integer");
    $objGrid -> FormatColumn("alumno_name", "Nombre", 30, 30, 0, "105", "left");
    $objGrid -> FormatColumn("alumno_apellido", "Apellido", 30, 30, 0, "110", "left");
    $objGrid -> FormatColumn("alumno_ano", "Ano", 3, 3, 0, "30", "center");
    $objGrid -> FormatColumn("alumno_sec", "Secc", 5, 5, 0, "30", "left");
    //inicio de dinamico
    [b]$objGrid -> FormatColumn("$name", "$name", 5, 5, 0, "30", "left");[/b]
    //fin de dinamico
    $objGrid -> FormatColumn("username", "Usuario", 8, 8, 0, "50", "center");
    $objGrid -> FormatColumn("password", "Clave", 8, 8, 0, "50", "center");
        /*$objGrid -> where ("active = '1'");*/
    /*$objGrid -> FormatColumn("alumno_foto","Foto", "25", "0","0","150","center","image:images/sample/%s");*/
 
    $objGrid -> setHeader();
    
}
 
?>
 
 
 
Great! this code show me, the the 6th (algebra) field, but when i insert another field (geometria) only show me the 7th (algebra)... why?.

Re: Build a dinamyc code with mysql_field_name query.

Posted: Mon Feb 04, 2008 8:06 pm
by josh27
i solve the problem ;) THANKS. HAHAHA... i hope can help to somebody that use PhpMyDatagrid

Code: Select all

 
 
<?php
 
        
$con = mysql_connect("localhost", "db_test", "db");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
$db_selected = mysql_select_db("db_test",$con);
 
$sql = "SELECT * from authors";
 
$result = mysql_query($sql,$con);
 
//$name = mysql_field_name($result, 2);
 
$campos = mysql_num_fields($result);
 
$nuevos = $campos - 3 ;
 
 
 
    include ("phpmydatagrid.class.php");
    
    $objGrid = new datagrid;
    
    $objGrid -> friendlyHTML();
 
    $objGrid -> pathtoimages("./images/");
 
    $objGrid -> closeTags(true);  
    
    $objGrid -> form('authors', true);
    
    $objGrid -> methodForm("post"); 
    
    $objGrid -> total("salary,workeddays");
    
    $objGrid -> searchby("id,name,lastname,afiliation");
    
    //$objGrid -> linkparam("sess=".$_REQUEST["sess"]."&username=".$_REQUEST["username"]);   
    
    $objGrid -> decimalDigits(2);
    
    $objGrid -> decimalPoint(",");
    
    $objGrid -> conectadb("localhost", "db_test", "test", "db_test");
    
    $objGrid -> tabla ("authors");
 
    /* Allow Add, edit, delete or view records */
    $objGrid -> buttons(true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true);
    
    /* Keyfield must be defined to indentify each row */
    $objGrid -> keyfield("id");
 
    /* A code is related with some transactions. so is very dificult to someone to try to do unauthorized process */
    /* There are a internal code but you can made it strong by setting this function" */
    $objGrid -> salt("Some Code4Stronger(Protection)");
 
    $objGrid -> TituloGrid("PeruTotal - School System 7.1 by JoshWebmaster");
 
    $objGrid -> FooterGrid("<div style='float:left'>&copy; 2008 PeruTotal.Net</div>");
 
    $objGrid -> datarows(30);
    
    $objGrid -> paginationmode('links');
 
    /*$objGrid -> orderby("age", "DESC");*/
 
    $objGrid -> noorderarrows();
    
    $objGrid -> FormatColumn("id", "ID", 10, 10, 0, "10", "center", "integer");
    $objGrid -> FormatColumn("alumno_name", "Nombre", 30, 30, 0, "105", "left");
    $objGrid -> FormatColumn("alumno_apellido", "Apellido", 30, 30, 0, "110", "left");
    $objGrid -> FormatColumn("alumno_ano", "Ano", 3, 3, 0, "30", "center");
    $objGrid -> FormatColumn("alumno_sec", "Secc", 5, 5, 0, "30", "left");
    [b]//inicio de dinamico
    
for ( $counter = 5; $counter < $nuevos; $counter += 1) {
    
    $name = mysql_field_name($result, "$counter"); 
    $objGrid -> FormatColumn("$name", "$name", 8, 8, 0, "50", "center");
}
    //fin de dinamico[/b]   
            $objGrid -> FormatColumn("username", "Usuario", 8, 8, 0, "50", "center");
    $objGrid -> FormatColumn("password", "Clave", 8, 8, 0, "50", "center");
        /*$objGrid -> where ("active = '1'");*/
    /*$objGrid -> FormatColumn("alumno_foto","Foto", "25", "0","0","150","center","image:images/sample/%s");*/
 
    $objGrid -> setHeader();
    
 
 
?>