SQL / PHP Haga una matriz con nombre de columna / valor para una ID única específica

StackOverflow https://stackoverflow.com//questions/9653665

  •  11-12-2019
  •  | 
  •  

Pregunta

Tengo una tabla de SQL con nombre de clientes donde cada cliente se almacena en una fila, con una ID única (incremento automático) y los nombres de las columnas son: CLIENTENAME, CLIENTLASTNAME, CLIENTMOBILE y así sucesivamente.

Quiero hacer una matriz que recuperará el contenido de un cliente específico (fila) y será como: clientname => lalala, clientlasthname=>lalalala... y así sucesivamente.

Entonces quiero mostrar los resultados en un formulario HTML (no presentado, simplemente mostrar) Cada celda en el formulario tiene una ID única y el nombre igual a los nombres de las columnas del SQL DB.

yo solo nuevo en eso y parece que no puedo conseguirlo. Esto es lo que he hecho hasta ahora:

<?php
   header("Content-Type:text/html; charset=utf-8");
   if ($_POST) {
       $link = mysql_connect("localhost","userid","pw");
       if (!$link) { die("Database Connection Failed". mysql_error()); }
       mysql_set_charset('utf8',$link);
       $db_select = mysql_select_db("form2",$link);
       if(!$db_select){ die("Database Selection Failed " . mysql_error()); }
    }  else { echo "Search:"; }
?>

<html>
 <head> </head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 <link rel="stylesheet" href="css/dsearch.css" />
 <link rel="stylesheet" href="css/base.css" />
 <link type="text/css" href="css/selected/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
 <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
 <script language="javascript" type="text/javascript" src="js/jquery-ui.min.js"></script>
 <script language="javascript" type="text/javascript" src="js/dropdowndata.sql/dropdowndata.clients.js"></script>
 <script language="javascript" type="text/javascript" src="js/buttons.js"></script>

 <body>
   <form class="form" id="dsearch" name="dsearch" action="dsearch.php" method="POST" enctype="application/x-www-form-urlencoded">
      <div class="dsearch"> 
        <li class="ui-widget"><label for="clients">client id: </label><input class="client" name="searchcid" style="width:100px" /></li>
        <li class="cell3"><input type="submit" value="Αναζήτηση" style="color:green; width:210px;  " /></li>
      </div>
   </form>
   <div class="results">


<?php
   $result1 = mysql_query("SELECT * FROM clients WHERE id='$_POST[searchcid]'", $link);
   if(!$db_select) { die("No Data found in db " . mysql_error()); };

   $items=array();
   while($row = mysql_fetch_array($result1)){
     $items[$row['id']] = $row['dlastname'];    

/* here is where I need the help I guess, 
   I need to fill the $items array with the data formatted as mentioned above.
   And after that I want to populate a form that I haven't already written with the
   values ,any help or maybe any suggestions on some tutorials I could go study
   would be appreciated.
   It goes on like this: */

   }
   print_r ($items);

?>

   </div>

  </body>
</html>
<?php
   if ($_POST) {
        mysql_close($link);
   }

?>

Editar: Significó cómo obtener la matriz

$clinfo=array();
$clinfo=mysql_fetch_assoc($result1);            
print_r ($clinfo);

Ahora necesito una forma de ir a poner los datos donde el ID de campo es igual al ID de la matriz DocInfo

¿Fue útil?

Solución

descubrió cómo hacerlo con un poco de PHP y jQuery, Aquí está:

$(document).ready(function(){

     <?php
            if($_GET){
                       foreach($docinfo as $key=>$val) { 
                       echo "$('#" . $key . "').val('" . htmlspecialchars($val) ."');";
                       }
               }
    ?>
});

Esto crea una línea de jQuery para cada ID de celular y lo llena con el valor apropiado

Cualquier comentario o mejoras son extremadamente apreciadas

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top