Foros del Web » Programando para Internet » PHP »

como condiciono una variable con un campo de la bd

Estas en el tema de como condiciono una variable con un campo de la bd en el foro de PHP en Foros del Web. Hola tengo lo siguiente: <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = ...
  #1 (permalink)  
Antiguo 05/12/2011, 16:34
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
como condiciono una variable con un campo de la bd

Hola tengo lo siguiente:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_laboratorio, $laboratorio);
$query_productos = "SELECT * FROM ingreso_productos";
$productos = mysql_query($query_productos, $laboratorio) or die(mysql_error());
$row_productos = mysql_fetch_assoc($productos);
$totalRows_productos = mysql_num_rows($productos);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table border="1">
<tr>
<td>nombre</td>
<td>Tipo</td>
<td>costo_producto</td>
<td>precio_venta</td>
<td>fecha</td>
<td>Existencia</td>
<td></td>
</tr>

<?php
$id=$row_productos['id_producto'];
$agregarlo="<a href='listaprueba.php?id_producto=$id&accion=agreg ar'>Agregar
</a>"?>
<?php do { ?>
<tr>
<td><?php echo $row_productos['nombre']; ?></td>

<td><?php echo $row_productos['Tipo']; ?></td>
<td><?php echo $row_productos['costo_producto']; ?></td>
<td><?php echo $row_productos['precio_venta']; ?></td>
<td><?php echo $row_productos['fecha']; ?></td>
<td><?php echo $row_productos['Existencia']; ?></td>
<td><?php echo $agregarlo ?></td>
</tr>
<?php } while ($row_productos = mysql_fetch_assoc($productos)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($productos);
?>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?php
if(isset($_GET['id_producto']))
$id=$_GET['id_producto'];

if(isset($_GET['accion']))
$realizar=$_GET['accion'];
else
$realizar='vacio';

switch($realizar){

case "agregar";
if(isset($_SESSION['lab_manager'][$id]))
$_SESSION['lab_manager'][$id]++;
else
$_SESSION['lab_manager'][$id]=1;

break;

case "eliminar";
if(isset($_SESSION['lab_manager'][$id])){
$_SESSION['lab_manager'][$id]--;

if($_SESSION['lab_manager'][$id]==0);
unset($_SESSION['lab_manager']['$id']);
}
break;

case "vacio";
unset($_SESSION['lab_manager']);
break;



}
//planteamiento
if(isset($_SESSION['lab_manager'])){

echo "<table border='1'>";
echo "<tr>";
echo "<td>Nombre </td>";
echo "<td>Cantidad </td>";
echo "<td>Valor</td>";
echo "</tr>";


$total=0;

foreach($_SESSION['lab_manager']as $id=> $existencia){


$consulta=mysql_query("select * from ingreso_productos where id_producto=$id" ,$db);
$mostrar=mysql_fetch_array($consulta);
$nombre=$mostrar['nombre'];
$precio_venta=$mostrar['precio_venta'];
$costo=$existencia*$precio_venta;
$total=$total+$costo;


echo "<tr>";
echo "<td>$nombre</td>" ;
echo "<td>$existencia <a href='principal.php?id_producto=$id&accion=elimina r'>rebajar</a></td>" ;
echo "<td>$costo</td>" ;
echo "</tr>";


}//termina foreach

echo "<tr>";
echo "<td>Total: </td>";
echo "<td>$total </td>";

echo "</tr>";
echo "</table>";
echo "<a href='principal.php?id_producto=$id&accion=vacio'> Cancelar</a> || <a href='prueba1.php'>Seguir comprando</a>";
}//terminar si no la envian

else
echo "No hay cotizacion <a href='prueba1.php'>Agrega un producto</a>";
?>
Quiero comprobar si el tipo de mi producto es a se hace un descuento o si es b se hace otro pero como compara mi variable que en este caso es la de $tipo con el campo de mi base de datos donde se almacena el tipo. Ojala alguien pueda ayudarme
  #2 (permalink)  
Antiguo 05/12/2011, 16:40
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

Pues ya ahí haces una consulta para obtener los datos del producto, solo agrega un switch para comparar dentro de tu foreach() y ya lo tienes.
  #3 (permalink)  
Antiguo 05/12/2011, 16:42
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

pero como compararia la variable me podrias orientar un poco mas?
  #4 (permalink)  
Antiguo 05/12/2011, 16:46
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

Es tan simple:
Código PHP:
Ver original
  1. switch($laVariableQueTieneElTipoDeProducto) {
  2.       case 'a':
  3.             $descuento = 10;
  4.             break;
  5.        case 'b':
  6.             $descuento = 20;
  7.             break;
  8.         default:
  9.             $descuento = 0;
  10.             break;
  11. }
  #5 (permalink)  
Antiguo 05/12/2011, 16:51
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

ok, lo manejare asi. Gracias
  #6 (permalink)  
Antiguo 05/12/2011, 16:56
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

oye pero en si quiero que al total se le haga el descuento dependiendo del tipo osea un ejemplo es articulo a compre 4 me toca un descuento de 30% y si compre 10 de ese articulo a toca uno de 35% igual seria dentro de ese mismo switch vdd?
  #7 (permalink)  
Antiguo 05/12/2011, 21:32
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

Lo tienes que hacer dentro de cada vuelta del foreach para controlar eso.

Saludos.
  #8 (permalink)  
Antiguo 06/12/2011, 09:51
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

puse esto dentro del foreach

switch($tipo){

case "extractos";
if($existencia>=3 && $existencia<=9 )
$descuento=$precio_venta*.30;
else if($existencia>=10 && $existencia<=19)
$descuento=$precio_venta*.35;
else if($existencia>=20 && $existencia<=39)
$descuento=$precio_venta*.40;
break;



case "gel";
if($existencia>=3 && $existencia<=9 )
$descuento=$precio_venta*.30;
else if($existencia>=10 && $existencia<=19)
$descuento=$precio_venta*.35;
else if($existencia>=20 && $existencia<=39)
$descuento=$precio_venta*.40;
break;

case "balines";
if($existencia=500)
$precio_venta-1;
else if($existencia=5000)
$precio_venta=-2;
break;


}

pero no me toma nada
  #9 (permalink)  
Antiguo 06/12/2011, 10:05
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

Faltaría ver de donde extraes las variables $tipo y $existencia
  #10 (permalink)  
Antiguo 06/12/2011, 11:45
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

esas las extraigo de mi base de datos

$consulta=mysql_query("select * from ingreso_productos",$db);
while($mostrar=mysql_fetch_array($consulta)){
$id=$mostrar["id_producto"];
$nombre=$mostrar["nombre"];
$tipo=$mostrar["Tipo"];
$precio_venta=$mostrar["precio_venta"];
$fecha=$mostrar["fecha"];
$existencia=$mostrar["Existencia"];
  #11 (permalink)  
Antiguo 06/12/2011, 12:34
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

¿Podrías mostrar todo el código como lo tienes?
  #12 (permalink)  
Antiguo 06/12/2011, 12:48
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: como condiciono una variable con un campo de la bd

ESta es una pagina que se llama prueba1.php

<?php
$db=mysql_connect("localhost","laboratorio","labor atorio");
$declarar=mysql_select_db("lab_manager",$db);
if($declarar==true){

}else {}
?>
<html>
<body>
<table border="1">
<tr>
<td>nombre</td>
<td>Tipo</td>
<td>precio_venta</td>
<td>fecha</td>
<td>Existencia</td>
<td>Agregar</td>
</tr>
<?php

$consulta=mysql_query("select * from ingreso_productos",$db);
while($mostrar=mysql_fetch_array($consulta)){
$id=$mostrar["id_producto"];
$nombre=$mostrar["nombre"];
$tipo=$mostrar["Tipo"];
$precio_venta=$mostrar["precio_venta"];
$fecha=$mostrar["fecha"];
$existencia=$mostrar["Existencia"];
$agregar="<a href='principal.php?id_producto=$id&accion=agregar '>Agregar</a>";
?>

<tr>
<td><?php echo $nombre?></td>
<td><?php echo $tipo?></td>
<td><?php echo $precio_venta?></td>
<td><?php echo $fecha?></td>
<td><?php echo $existencia?></td>
<td><?php echo $agregar?></td>
</tr>
<?php
}
?>
</table>
<a href="principal.php">Ver cotizacion</a>


</body>
</html>
----------------------------------------------------------------------------------------
Hice un link en la palagra agregar para que me envie el producto que estoy agregando y lo envie a la otra pagina d principal.php
<html>
<body>
<?php
if(isset($_GET['id_producto']))
$id=$_GET['id_producto'];

if(isset($_GET['accion']))
$realizar=$_GET['accion'];
else
$realizar='vacio';

switch($realizar){

case "agregar";
if(isset($_SESSION['lab_manager'][$id]))
$_SESSION['lab_manager'][$id]++;
else
$_SESSION['lab_manager'][$id]=1;

break;

case "eliminar";
if(isset($_SESSION['lab_manager'][$id])){
$_SESSION['lab_manager'][$id]--;

if($_SESSION['lab_manager'][$id]==0);
unset($_SESSION['lab_manager']['$id']);
}
break;

case "vacio";
unset($_SESSION['lab_manager']);
break;



}
//planteamiento
if(isset($_SESSION['lab_manager'])){

echo "<table border='1'>";
echo "<tr>";
echo "<td>Nombre </td>";
echo "<td>Cantidad </td>";
echo "<td>Valor</td>";
echo "</tr>";


$total=0;
$total1=0;
foreach($_SESSION['lab_manager']as $id=> $existencia){


$consulta=mysql_query("select * from ingreso_productos where id_producto=$id" ,$db);
$mostrar=mysql_fetch_array($consulta);
$nombre=$mostrar['nombre'];
$precio_venta=$mostrar['precio_venta'];
$costo=$existencia*$precio_venta;
$total=$total+$costo;

switch($tipo){

case "extractos";
if($existencia>=3 && $existencia<=9 )
$descuento=$precio_venta*.30;
else if($existencia>=10 && $existencia<=19)
$descuento=$precio_venta*.35;
else if($existencia>=20 && $existencia<=39)
$descuento=$precio_venta*.40;
break;



case "gel";
if($existencia>=3 && $existencia<=9 )
$descuento=$precio_venta*.30;
else if($existencia>=10 && $existencia<=19)
$descuento=$precio_venta*.35;
else if($existencia>=20 && $existencia<=39)
$descuento=$precio_venta*.40;
break;

case "balines";
if($existencia=500)
$precio_venta-1;
else if($existencia=5000)
$precio_venta=-2;
break;


}

echo "<tr>";
echo "<td>$nombre</td>" ;
echo "<td>$existencia <a href='principal.php?id_producto=$id&accion=elimina r'>rebajar</a></td>" ;
echo "<td>$costo</td>" ;
echo "<td></td>";
echo "<td></td>";
echo "</tr>";


}//termina foreach
if ($total>=2000)
$total1=$total*.50;
echo "<tr>";
echo "<td>Total: </td>";
echo "<td>$total </td>";
echo "<td>$total1 </td>";
echo "<td>$descuento</td>";
echo "<td>$tipo</td>";
echo "</tr>";
echo "</table>";
echo "<a href='principal.php?id_producto=$id&accion=vacio'> Cancelar</a> || <a href='prueba1.php'>Seguir comprando</a>";
}//terminar si no la envian

else
echo "No haz seleccionado ningun producto
<a href='prueba1.php'> Agrega un producto</a>";
?>

</body>
</html>
  #13 (permalink)  
Antiguo 06/12/2011, 13:45
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: como condiciono una variable con un campo de la bd

El problema es que también necesitas extraer $tipo de tu foreach(), tal como lo haces en tu primer while.

Saludos.

Etiquetas: bd, html, mysql, sql, variables, campos
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 21:55.