Ir al contenido


Foto

MOSTRAR REGISTROS DE UNA BASE DE DATOS EN MYSQL


  • Por favor identifícate para responder
6 respuestas en este tema

#1 joselu1476

joselu1476

    Member

  • Miembros
  • PipPip
  • 12 mensajes

Escrito 06 septiembre 2011 - 03:15

AL MOMENTO DE CREAR UNA BASE DE DATOS Y DE TRAERLOS A UN ARCHIVO Y MOSTRARLOS EN EL NAVEGADOR ME RESULTA QUE ME CREA UN ESPACIO O UNA FILA ES CUANDO ESTA EL $i=0 pero cuando lo coloco $i=1 no me da ese error en ealidad no se que esta pasando
me podrian decir como puedo solucionarlo
gracias

<body>

<form action="modificar_productos.php?ka=<?php echo $_GET["ka"];?>" method="post" style="text-align:center">
<table align="center">
<tr>
<h2>Listar Productos</h2>
<a href="agregar.php" style="text-decoration:none"><strong>Agregar</strong>

<td>Nro</td>
<td>C&oacute;digo</td>
<td>Nombre del Producto</td>
<td>Descripci&oacute;n</td>
<td>Fecha del Producto</td>

<td>Eliminar</td>
<td>Modificar</td>
</tr>


<?php
$reg=$tra->listar_productos();
for($i=0;$i<sizeof($reg);$i++)
{
?>
<tr>
<td align="left"><?php echo $reg[$i]["numero"];?></td>
<td align="left"><?php echo $reg[$i]["codigo"];?></td>
<td align="left"><?php echo $reg[$i]["producto"];?></td>
<td align="left"><?php echo $reg[$i]["descripcion"];?></td>
<td align="left"><?php echo $reg[$i]["fecha"];?></td>
<td align="left"><a href="eliminar_productos.php?eli=<?php echo $reg[$i]["codigo"];?>" title="<?php echo $reg[$i]["producto"];?>">Eliminar</td>
<td align="left"><a href="pre_modificar_productos.php?ka=<?php echo $reg[$i]["codigo"];?>" style="text-decoration:none">Modificar</td>
</tr>
<?php
}
?>
</table>

</form>
</body>

  • 0

#2 squall2501

squall2501

    Advanced Member

  • Miembros
  • PipPipPip
  • 82 mensajes
  • Locationen oa

Escrito 06 septiembre 2011 - 05:56

no te entiendo ¿que tipo de error te muestra?

Te recuerdo que estas recorriendo un arreglo que empieza en cero 0 por ende $i debe ser igual 0 
  • 0

#3 cesar_cz

cesar_cz

    Advanced Member

  • Moderadores
  • PipPipPip
  • 919 mensajes
  • LocationChile

Escrito 06 septiembre 2011 - 06:13

Hola Joselus me gustaría ver tu método:

listar_productospara ver por qué se genera ese registro de más, seguramente el problema está en cuando llenas el array...

saludos desde Santiago de Chile..
[/color][/color]
  • 0

#4 joselu1476

joselu1476

    Member

  • Miembros
  • PipPip
  • 12 mensajes

Escrito 06 septiembre 2011 - 08:15

Este archivo es para la conexion con la base de datos


<?php
class conectar
{
public static function con()
{
$con=mysql_connect("localhost","root","root");
mysql_query("SET NAMES 'utf8'");
mysql_select_db("farmacias");
return $con;
}
}



class productos
{
private $pro;
public function __construct()
{
$this->pro[]=array();

}
public function mostrar_productos()
{
$sql="select * from productos";
$res=mysql_query($sql,conectar::con());
while($reg=mysql_fetch_assoc($res))
{
$this->pro[]=$reg;
}
return $this->pro;
}


Y este archivo es mostrar.php para ver los registros dedes la base de datos
<?php
require_once("class/conexion.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
</head>

<body>
<form action="" method="post">
<table align="center">
<h2 align="center">LISTAR PRODUCTOS</h2><a href="insertar_productos.php" style="text-align:right">Agregar</a>

<br />
<tr>
<td>Nro</td>
<td>C&oacute;digo</td>
<td>Nombre del Producto</td>
<td>Descripci&oacute;n</td>
<td>Fecha</td>
<td>Eliminar</td>
<td>Modificar</td>
</tr>

<?php
$tra=new productos();
$reg=$tra->mostrar_productos();
for($i=1;$i<sizeof($reg);$i++)
{
?>
<tr>
<td><?php echo $reg[$i]["numero"];?></td>
<td><?php echo $reg[$i]["codigo"];?></td>
<td><?php echo $reg[$i]["producto"];?></td>
<td><?php echo $reg[$i]["descripcion"];?></td>
<td><?php echo $reg[$i]["fecha"];?></td>
<td><a href="eliminar.php?eli=<?php echo $reg[$i]["codigo"];?>">Eliminar</a></td>
<td><a href="modificar_productos.php?mo=<?php echo $reg[$i]["codigo"];?>">Eliminar</a></td>
</tr>

<?php
}
?>
</table>
<p>&nbsp;</p>
</form>



</body>
</html>
El problema radica en el navegador cuando a la variable lo pongo $i=0 crea un espacio en blanco en las filas

  • 0

#5 cesar_cz

cesar_cz

    Advanced Member

  • Moderadores
  • PipPipPip
  • 919 mensajes
  • LocationChile

Escrito 06 septiembre 2011 - 08:19

Quítale los corchetes a la declaración del array en el cosntructor, por eso se te generan registros vacíos:

public function __construct()
  {
      $this->pro=array();
     
  }

saludos

  • 0

#6 joselu1476

joselu1476

    Member

  • Miembros
  • PipPip
  • 12 mensajes

Escrito 06 septiembre 2011 - 08:24

Muchas gracias lo he probado y se me resulta en realidad no me habia dado cuenta de ese pequeño error  :smiley:
  • 0

#7 joselu1476

joselu1476

    Member

  • Miembros
  • PipPip
  • 12 mensajes

Escrito 06 septiembre 2011 - 08:31

SU PAGINA WEB ES UNA MARAVILLA USTED ES UN GRAN MAESTRO UNA PARTE DE MIS CONOCIMIENTOS SE LO DEBO A USTED LE QUISIERA PREGUNTAR ESTABA TRATANDO DE REALIZAR UN CARRITO DE COMPRA PERO CON ALGUNAS FALLAS QUISIERA APRENDER A REALIZAR UN FORO COMO USTED LO TIENE ES FENOMENAL NO SI PODRIA REALIZAR ESTOS TEMAS COMO USTED LO LLEVA EN SU MODO DE VIDEOSTUTORIALES
GRACIAS MAESTRO :smiley:
  • 0




IP.Board spam blocked by CleanTalk.