Foros del Web » Programando para Internet » PHP »

Array PHP

Estas en el tema de Array PHP en el foro de PHP en Foros del Web. Buenas, me gustaria hacer lo siguiente, tengo dos tablas en la DB, una llamada users, y otra inventory, me gustaria hacer: 1º Coger el user_id ...
  #1 (permalink)  
Antiguo 25/08/2009, 07:14
Avatar de zornak  
Fecha de Ingreso: septiembre-2008
Ubicación: Toledo
Mensajes: 96
Antigüedad: 15 años, 7 meses
Puntos: 3
Array PHP

Buenas, me gustaria hacer lo siguiente, tengo dos tablas en la DB, una llamada users, y otra inventory, me gustaria hacer:

1º Coger el user_id de la tabla users.

2º En inventory, mirar los objetos que sean de ese user_id

3º Hacer en una tabla, una lista, con las columnas de la tabla inventory (las columnas son: (item_name y count)


Me han dicho que con un array, pero no doy con la forma de hacerlo :S

Me podeis poner un ejemplo?
  #2 (permalink)  
Antiguo 25/08/2009, 07:44
 
Fecha de Ingreso: julio-2009
Ubicación: .mysql_error ( XD )
Mensajes: 554
Antigüedad: 14 años, 9 meses
Puntos: 13
Respuesta: Array PHP

y cuáles son los campos de la tabla inventory que están relacionados a user_id?


1.- bueno, acá solo debes hacer una consulta simple
Código sql:
Ver original
  1. $consulta = SELECT user_id FROM users WHERE user_id = 777 //el WHERE es por si quieres un id en especifico


2.- no sé como es que tienes relacionada una tabla con otra, pero debe ser algo asi
Código sql:
Ver original
  1. SELECT objetos FROM Inventory WHERE user_id = 777


3.- las 2 consultas anteriores las puedes unir con un JOIN y luego haces un while para que te guarde los datos en un array
Código php:
Ver original
  1. $resultado = mysql_query ($consulta);
  2. while ($array = mysql_fetch_array($resultado))
  3. {
  4.      <?php echo $array["tucampo"] ?>
  5.      ...........
  6. }

cualquier cosa nos lo haces saber
saludos
  #3 (permalink)  
Antiguo 25/08/2009, 09:02
Avatar de zornak  
Fecha de Ingreso: septiembre-2008
Ubicación: Toledo
Mensajes: 96
Antigüedad: 15 años, 7 meses
Puntos: 3
Respuesta: Array PHP

Tengo un lio impresionante....

Tengo este codigo:

Código php:
Ver original
  1. <? if($_GET['funcion'] == "see_invetory")
  2. {
  3.     $char = $_GET['char_name'];
  4.     $owner_id = mysql_query("SELECT * FROM characters WHERE char_name=$char");
  5.     $row = mysql_fetch_assoc($owner_id);
  6.     $owner = $row['charid'];
  7.     $see_invetory = "SELECT * FROM items WHERE owner_id=$owner";
  8.     $resultado = mysql_query ($see_invetory);
  9.  
  10.     while ($array = mysql_fetch_array($resultado))
  11.    
  12.     {?>
  13.     <table width="600" border="0" cellspacing="5">
  14.     <tr>
  15.         <td width="405">Objeto</td>
  16.         <td width="70">Cantidad</td>
  17.         <td width="99">Enchant</td>
  18.     </tr>
  19.     <tr>
  20.         <td width="405"><?php echo $array["item_id"]?></td>
  21.         <td width="70"><?php echo $array["count"]?></td>
  22.         <td width="99"><?php echo $array["enchant_level"]?></td>
  23.     </tr>
  24. </table>
  25. <?}}?>


¿Que tengo mal?

Última edición por zornak; 26/08/2009 a las 02:36
  #4 (permalink)  
Antiguo 26/08/2009, 03:22
 
Fecha de Ingreso: septiembre-2003
Ubicación: Madrid
Mensajes: 53
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Array PHP

Cita:
Iniciado por zornak Ver Mensaje
Tengo un lio impresionante....

Tengo este codigo:

Código php:
Ver original
  1. <? if($_GET['funcion'] == "see_invetory")
  2. {
  3.     $char = $_GET['char_name'];
  4.     $owner_id = mysql_query("SELECT * FROM characters WHERE char_name=$char");
  5.     $row = mysql_fetch_assoc($owner_id);
  6.     $owner = $row['charid'];
  7.     $see_invetory = "SELECT * FROM items WHERE owner_id=$owner";
  8.     $resultado = mysql_query ($see_invetory);
  9.  
  10.     while ($array = mysql_fetch_array($resultado))
  11.    
  12.     {?>
  13.     <table width="600" border="0" cellspacing="5">
  14.     <tr>
  15.         <td width="405">Objeto</td>
  16.         <td width="70">Cantidad</td>
  17.         <td width="99">Enchant</td>
  18.     </tr>
  19.     <tr>
  20.         <td width="405"><?php echo $array["item_id"]?></td>
  21.         <td width="70"><?php echo $array["count"]?></td>
  22.         <td width="99"><?php echo $array["enchant_level"]?></td>
  23.     </tr>
  24. </table>
  25. <?}}?>


¿Que tengo mal?

Buenas, según veo, cambia mysql_fetch_array($resultado)) por mysql_fetch_assoc($resultado)).

Saludos.
  #5 (permalink)  
Antiguo 26/08/2009, 04:07
 
Fecha de Ingreso: septiembre-2004
Mensajes: 324
Antigüedad: 19 años, 7 meses
Puntos: 13
Respuesta: Array PHP

Parece que el codigo esta bien.

cambia los mysql_query pon esto:
mysql_query($algo) or die(mysql_error());
  #6 (permalink)  
Antiguo 26/08/2009, 04:26
Avatar de zornak  
Fecha de Ingreso: septiembre-2008
Ubicación: Toledo
Mensajes: 96
Antigüedad: 15 años, 7 meses
Puntos: 3
Respuesta: Array PHP

No te entiendo :S
  #7 (permalink)  
Antiguo 26/08/2009, 04:28
 
Fecha de Ingreso: septiembre-2003
Ubicación: Madrid
Mensajes: 53
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Array PHP

Linea 10 de tu código.
// antes
//while ($array = mysql_fetch_array($resultado))
//ahora
while($array = mysql_fetch_assoc($resultado))

mysql_fetch_array => para acceder a los campos $array[0], $array[1]
mysql_fetch_assoc => se puede usar el nombre del campo como key del array.

Existe otra manera.
while ($array = mysql_fetch_array($resultado, MYSQL_ASSOC))

Saludos.
  #8 (permalink)  
Antiguo 26/08/2009, 04:53
Avatar de zornak  
Fecha de Ingreso: septiembre-2008
Ubicación: Toledo
Mensajes: 96
Antigüedad: 15 años, 7 meses
Puntos: 3
Respuesta: Array PHP

lo tengo asi:

Código php:
Ver original
  1. <? if($_GET['funcion'] == "see_invetory")
  2. {
  3.     $char = $_GET['char_name'];
  4.     $owner_id = mysql_query("SELECT * FROM characters WHERE char_name=$char");
  5.     $row = mysql_fetch_assoc($owner_id);
  6.     $owner = $row['charid'];
  7.     $see_invetory = "SELECT * FROM items WHERE owner_id=$owner";
  8.     $resultado = mysql_query ($see_invetory);
  9.  
  10.     while ($array = mysql_fetch_assoc($resultado))
  11.    
  12.     {?>
  13.     <table width="600" border="0" cellspacing="5">
  14.     <tr>
  15.         <td width="405">Objeto</td>
  16.         <td width="70">Cantidad</td>
  17.         <td width="99">Enchant</td>
  18.     </tr>
  19.     <tr>
  20.         <td width="405"><?php echo $array["item_id"]?></td>
  21.         <td width="70"><?php echo $array["count"]?></td>
  22.         <td width="99"><?php echo $array["enchant_level"]?></td>
  23.     </tr>
  24. </table>
  25. <?}}?>

Y aun no va xD
  #9 (permalink)  
Antiguo 26/08/2009, 05:02
 
Fecha de Ingreso: septiembre-2003
Ubicación: Madrid
Mensajes: 53
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Array PHP

Buenas, te pinta la cabecera de la tabla y no los valores?, o no te pinta nada?.

Saludos.
  #10 (permalink)  
Antiguo 26/08/2009, 05:03
Avatar de zornak  
Fecha de Ingreso: septiembre-2008
Ubicación: Toledo
Mensajes: 96
Antigüedad: 15 años, 7 meses
Puntos: 3
Se queda todo en negro :S

EDITO: Se queda en negro, porque mi fondo es negro xD seria mejor: Se queda vacio

Hola, por fin!!! ya me coje los datos....

(hice la parte esa en un archivo a parte en vez de en una funcion en el mismo archivo)

Pero los datos... me salen asi:

Objeto Cantidad Enchant
5588 1 0
9716 10 0 10650 5 0 10 1 0 1146 1 0 1147 1 0 2369 1 0

Solo me pone en la tabla, el primer dato que saca :S

El codigo me ha quedado:

Código php:
Ver original
  1. <table width="600" border="3" cellspacing="5">
  2. <tr>
  3.     <td width="405">Objeto</td>
  4.     <td width="70">Cantidad</td>
  5.     <td width="99">Enchant</td>
  6. </tr>                  
  7. <?
  8.     $charID = $_GET['char_id'];
  9.     $see_invetory = "SELECT * FROM items WHERE owner_id=$charID";
  10.     $resultado = mysql_query ($see_invetory);
  11.  
  12.     while ($array = mysql_fetch_assoc($resultado))
  13.    
  14.     {
  15.         echo '<tr>
  16.               <td width="405">'.$array["item_id"].'</td>
  17.               <td width="70">'.$array["count"].'</td>
  18.               <td width="99">'.$array["enchant_level"].'</td>
  19.               </tr>
  20.             </table>';
  21.     }
  22. ?>

Arreglado xDD

Era por el </table> que lo tenia dentro del "echo"

Gracias a todos!!! Ya funciona!!!! :D

Última edición por GatorV; 26/08/2009 a las 09:22
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 06:07.