Page 1 of 1

OH NO! Missunderstanding variables

Posted: Mon Mar 17, 2003 8:38 am
by Igguana
To my beloved Volka o whoever:
I have a problem with the variables that came to this query.
Modo, localidad and Tipo, they are no (int) but (string) maybe because of that I have an error that I just realice.

SELECT * FROM DatosInmueble WHERE Tipo=Apartamento :Unknown column 'Apartamento' in 'where clause'

No matter if I convert them before with the (string) info or if I left them like they are, it doesn't work.

<?php
$base="bla";
$tabla="DatosInmueble";
$conexion = mysql_connect('bla') or die(mysql_error());
mysql_select_db($base,$conexion) or die(mysql_error());

$clauses = array();
if (isset($_POST['envio1'] ) && $_POST['envio1'] != -1)
$clauses[] = 'Modo='.$_POST['envio1'];
if (isset($_POST['envio2'] ) && $_POST['envio2'] != -1)
$clauses[] = 'Localidad='.$_POST['envio2'];
if (isset($_POST['envio3'] ) && $_POST['envio3'] != -1)
$clauses[] = 'Tipo='.$_POST['envio3'];
if (isset($_POST['envio4'] ) && $_POST['envio4'] != -1)
$clauses[] = 'Habitaciones='.(int)$_POST['envio4'];
if (isset($_POST['envio5'] ) && $_POST['envio5'] != -1)
$clauses[] = 'Precio<='.(int)$_POST['envio5'];

$query = "SELECT * FROM $tabla";
if (count($clauses) > 0)
$query .= ' WHERE '.join('AND ', $clauses);
$pegar=mysql_query($query, $conexion) or die($query.' :'.mysql_error());
$coincidencias=mysql_num_rows($pegar);

$comienzo=(0+$aumento);// :oops: This second query I need it to group results!
$query2= "SELECT * FROM $tabla LIMIT $comienzo, 4";
if (count($clauses) > 0)
$query2 .= ' WHERE '.join('AND ', $clauses);
$pegar2=mysql_query($query2, $conexion) or die($query2.' :'.mysql_error());
?>

Posted: Mon Mar 17, 2003 9:31 am
by twigletmac
You need to put single quotes around the strings in the search query so that you end up with something like:

Code: Select all

SELECT * FROM DatosInmueble WHERE Tipo='Apartamento'
Try:

Code: Select all

if (isset($_POST['envio1'] ) && $_POST['envio1'] != -1) {
    $clauses[] = "Modo='".$_POST['envio1']."'"; 
}
if (isset($_POST['envio2'] ) && $_POST['envio2'] != -1) {
    $clauses[] = "Localidad='".$_POST['envio2']."'";
}
if (isset($_POST['envio3'] ) && $_POST['envio3'] != -1) {
    $clauses[] = "Tipo='".$_POST['envio3']."'";
}
if (isset($_POST['envio4'] ) && $_POST['envio4'] != -1) {
    $clauses[] = "Habitaciones='".(int)$_POST['envio4']."'";
}
if (isset($_POST['envio5'] ) && $_POST['envio5'] != -1) {
    $clauses[] = "Precio<='".(int)$_POST['envio5']."'";
}
Mac

Unfortunately, this is not the QUID

Posted: Mon Mar 17, 2003 5:29 pm
by Igguana
I've been trying your solution, but it doesn't work neither.

Parse error: parse error, unexpected T_STRING in /chs/p3/bpoem.com/home/html/resultados2.html on line 17
and things like that.
Why it works with the variables filled with numbers?

To twigletmac

Posted: Mon Mar 17, 2003 7:12 pm
by Igguana
I'm trying again with the code exactly as you wrote over those lines and even the (int) variables don't work.
I'm totally lost.

Posted: Tue Mar 18, 2003 2:04 am
by twigletmac
I tried running the code (substituting in what I suggested) to check for parse errors and got none - could we see your current code?

Mac

Posted: Tue Mar 18, 2003 2:27 am
by volka
the code Twigletmac gave you should work, there are no parse errors in.
Only some changes to fit your previous questions and some additional explainations

Code: Select all

<?php
$base="bla";
$tabla="DatosInmueble";
$conexion = mysql_connect('bla') or die(mysql_error());
mysql_select_db($base,$conexion) or die(mysql_error());

$clauses = array();
/** changing $_POST to $_REQUEST.
$_POST contains the parameters that have been transmitted via method="POST" (e.g. in forms)
But links like href="action.php?key1=value1&key2=value2" use the GET-method. You'll need it for the ""prev" "next" links
(unless you create a form with hidden-fields)
$_REQUEST contains both , $_POST and $_GET
*/
// use mysql_escape_string on any userdata that is redirected to mysql as string
if (isset($_REQUEST['envio1'] ) && $_REQUEST['envio1'] != -1)
	$clauses[] = "Modo='".mysql_escape_string($_REQUEST['envio1'])."'";

if (isset($_REQUEST['envio2'] ) && $_REQUEST['envio2'] != -1)
	$clauses[] = "Localidad='".mysql_escape_string($_REQUEST['envio2'])."'";

if (isset($_REQUEST['envio3'] ) && $_REQUEST['envio3'] != -1)
	$clauses[] = "Tipo='".mysql_escape_string($_REQUEST['envio3'])."'";
/** no need to quote numbers in query
SELECT ... WHERE field=1   vs.  SELECT ... WHERE field='string'
and the explicit cast to (int) in php removes the need of mysql_escape_string
*/
if (isset($_REQUEST['envio4'] ) && $_REQUEST['envio4'] != -1)
	$clauses[] = 'Habitaciones='.(int)$_REQUEST['envio4'];

if (isset($_REQUEST['envio5'] ) && $_REQUEST['envio5'] != -1)
	$clauses[] = 'Precio<='.(int)$_REQUEST['envio5'];

// create the base-query, table and conditions
// something like   from DatosInmueble WHERE Precio<=1
$query = 'FROM '.$tabla;
if (count($clauses) > 0)
	$query .= ' WHERE '.join('AND ', $clauses);
// query how many records matching the conditions there are
// something like   SELECT count(*) from DatosInmueble WHERE Precio<=1
$pegar=mysql_query('SELECT count(*) '.$query, $conexion) or die($query.' :'.mysql_error());
// the result is a single column in a single row; fetch it and "shift" the value out of the array
$coincidencias = array_shift(mysql_fetch_row($pegar));

// however this parameter is transfered (TODO)
$aumento = (int)$_REQUEST['aumento'];
// should be always a positive number (or zero)
if ($aumento < 0)
	$aumento = 0;
// build the fetch-query with LIMIT
// something like     SELECT * from DatosInmueble WHERE Precio<=1 LIMIT 5,5
$query = 'SELECT * ' . $query . ' LIMIT ' . $aumento . ', 5';
$pegar=mysql_query($query, $conexion) or die($query.' :'.mysql_error());

echo '<table border="1">';
while($row = mysql_fetch_array($pegar))
{
	echo '<tr>';
	foreach($row as $column)
		echo '<td>', $column, '</td>';
	echo '</tr>';
}

/** here you have to place the links "prev" "next"
either build a form with the necessary parameters as hidden fields or a link
take a look at the urlencode()-manual page
*/
?>
(script tested not even by compiler ;) )

Everything seems fine now

Posted: Tue Mar 18, 2003 9:15 am
by Igguana
Thank U. The code seems to work pretty fine.
Now I'm fighting with the menu to do it dinamical
(I guess this recall is too big for an aprentice)
I did a menu that takes the fields from the database by doing querys.
Then you have a bunch of list menus and a jump menu.
One of the list menus depends on the jump one because its contents are related to a previous selection, so when somebody choose and option for the jump menu, the page reloads and then this list menu is filled with options (the rest where filled from the begining)
What happens is that if the user has already filled all the fields, when the page reloads, he will realice that everything is empty again.
I have manage a system to prevent this in the jump menu, but this is not worth for the lists.
Some light over this?
<?
$base="bla";
$tabla="DatosInmueble";
$conexion=mysql_connect("bla");
mysql_select_db($base,$conexion);
$pegar1 = "SELECT Modo FROM $tabla GROUP BY Modo";
$pegar2 = "SELECT Localidad FROM $tabla GROUP BY Localidad";
$pegar3 = "SELECT Tipo FROM $tabla GROUP BY Tipo";
$pegar4 = "SELECT Habitaciones FROM $tabla GROUP BY Habitaciones";
$pegar5 = "SELECT Precio FROM $tabla GROUP BY Precio";
?>
<script language="JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>

<table width=100% border=0>
<tr>
<td bgcolor='#CCCC00'>
<form name='form1' method='post' action='resultados2.html'>
<table width='50%' border='0'>



<tr>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'>Modalidad</font></td>
<td><font face="Arial, Helvetica, sans-serif" size="1" color="#FFFF00">Localidad</font></td>
<td><font face="Arial, Helvetica, sans-serif" size="1" color="#FFFF00">Zona</font></td>
<td>&nbsp;</td>
</tr>
<tr>
<td> <font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name='envio1' size='1'>
<option value="-1"></option>
<?
$resultado1 = mysql_db_query($base,$pegar1)or die (mysql_error());
while ($registro1['Modo'] = mysql_fetch_row($resultado1)){
foreach($registro1['Modo'] as $clave1){
}
?>
<option><?

echo $clave1;
}

?></option>
</select>
</font></td>
<td> <font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name="saltador" onChange="MM_jumpMenu('parent',this,0)">
//with this functions I can make the jump menu keep the preselected option even though the page reloads.
<option value="<?
function devuelvedato ()
{
$nulo=-1;
global $var1;
if (isset($var1))
echo $var1;
else
echo $nulo;
}
devuelvedato();
?>" selected>
<?
function devuelvedato2 ()
{
$nulo2="";
global $var1;
if (isset($var1))
echo $var1;
else
echo $nulo2;
}
devuelvedato2();
?></option>
<?
$resultado2 = mysql_db_query($base,$pegar2)or die (mysql_error());
while ($registro2['Modo'] = mysql_fetch_row($resultado2)){
foreach($registro2['Modo'] as $clave2){
}
?>
<option value="buscadorpruebas2.php?var1=<? echo $clave2; ?>"><font face="Arial, Helvetica, sans-serif" size="1" color="#FFFF00"><?
echo $clave2;
Here I sent the info to the reloaded page to know which option was selected in the jump menu
}
$pegar6 = "SELECT Zona FROM $tabla WHERE (Localidad='$var1') GROUP BY Zona";
?>
</font></option>
</select>
</font></td>
<td> <font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name='envio6' size='1'>
<option value="-1"></option>
<?
$resultado6 = mysql_db_query($base,$pegar6)or die (mysql_error());
while ($registro6['Habitaciones'] = mysql_fetch_row($resultado6)){
foreach($registro6['Habitaciones'] as $clave6){
}
?>
<option><?

echo $clave6;
}

?></option>
</select>
</font></td>
<td>&nbsp; </td>
</tr>
<tr>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'>Habitaciones</font></td>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'>Precio m&aacute;ximo:</font></td>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'>Tipo</font></td>
<td>&nbsp;</td>
</tr>
<tr>
<td> <font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name='envio4' size='1'>
<option value="-1"></option>
<?
$resultado4 = mysql_db_query($base,$pegar4)or die (mysql_error());
while ($registro4['Habitaciones'] = mysql_fetch_row($resultado4)){
foreach($registro4['Habitaciones'] as $clave4){
}
?>
<option><?

echo $clave4;
}

?></option>
</select>
</font></td>
<td> <font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name='envio5' size='1'>
<option value="-1"></option>
<option value="300">300€</option>
<option value="600">600€</option>
<option value="900">900€</option>
<option value="1200">1200€</option>
<option value="3000">3000€</option>
<option value="6000">6000€</option>
<option value="12000">12000€</option>
<option value="18000">18000€</option>
<option value="24000">24000€</option>
<option value="30000">30000€</option>
<option value="60000">60000€</option>
<option value="90000">90000€</option>
<option value="120000">120000€</option>
<option value="180000">180000€</option>
<option value="210000">210000€</option>
<option value="240000">240000€</option>
<option value="300000">300000€</option>

</select>
</font></td>
<td><font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<select name='envio3' size='1'>
<option value="-1"></option>
<?
$resultado3 = mysql_db_query($base,$pegar3)or die (mysql_error());
while ($registro3['Tipo'] = mysql_fetch_row($resultado3)){
foreach($registro3['Tipo'] as $clave3){
}
?>
<option><font face="Arial, Helvetica, sans-serif" size="1" color="#FFFF00">
<?

echo $clave3;
}
?>
</font></option>
</select>
</font> </td>
<input type="hidden" value=<? echo $var1; ?> name="envio2">
<td><font face='Arial, Helvetica, sans-serif' size='1' color='#FFFF00'>
<input type='submit' name='Consultar' value='Consultar'>
</font></td>
</tr>
<tr>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'></font></td>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'></font></td>
<td><font face='Arial, Helvetica, sans-serif' size='1'

color='#FFFF00'></font></td>

<td>&nbsp;</td>
</tr>



</table>
</form>
</td>
</tr>

</table>
<?


mysql_close();

?>

Posted: Tue Mar 18, 2003 1:55 pm
by volka
Haven't gone through the script but maybe this example will help you

Code: Select all

<html>
	<body>
<?php
if (isset($_POST['vSelect']))
{
		echo 'last selection: ', $_POST['vSelect'];
		$sel = $_POST['vSelect'];
}
else
	$sel = -1; 
?>

	<form method="POST" action="<?php echo $_SERVER['POST']; ?>">
		<select name="vSelect">
<?php
for ($i=0; $i!=10; $i++)
	echo '<option value="', $i, ($i==$sel) ? '" selected="selected" >':'">', chr(65+$i), '</option>', "\n";
?>			
		</select>
		<input type="submit" />	
	</form>
	</body>
</html>
the option element having the property selected="selected" will be displayed in a dropdown or the first option if no selected="selected" is present.
There can be multiple selected options in a <select multiple="true" ..>-element but not in a dropdown (unpredictable result, mostly the last <option selected="selected">)

Posted: Thu Mar 20, 2003 5:30 am
by Igguana
This part of the code is completely dark for me.

<form method="POST" action="<?php echo $_SERVER['POST']; ?>">
<select name="vSelect">
<?php
for ($i=0; $i!=10; $i++)
echo '<option value="', $i, ($i==$sel) ? '" selected="selected" >':'">', chr(65+$i), '</option>', "\n";
?>
</select>
<input type="submit" />
</form>
</body>
</html>

Posted: Thu Mar 20, 2003 7:15 am
by volka
it's a short(er) version of

Code: Select all

<form method="POST" action="<?php echo $_SERVER['POST']; ?>">
	<select name="vSelect">
	<!-- everything outside a php-block is sent directly to the client -->
<?php
	for ($i=0; $i!=10; $i++)
	{
		echo '		<option value="', $i, '"';
		if ($i==$sel) // if the current index is the "selected" one in the POSTed form
			echo 'selected="selected"'; // select it again in the "new" form
		echo '>';
		echo chr(65+$i); // chr(65)->'A', chr(66)->'B', chr(67)->'C', ...
		echo '</option>';
	}
?>
	</select>
	<input type="submit" />
</form>
</body>
</html>
probably the ternary operator troubles you

Code: Select all

<pre>
<?php
$b = TRUE;
echo ($b == TRUE) ? 'true':'false', "\n";
// same as
if ($b == TRUE)
	echo 'true', "\n";
else
	echo 'false', "\n";	

$b = FALSE;
echo ($b == TRUE) ? 'true':'false', "\n";
// same as
if ($b == TRUE)
	echo 'true', "\n";
else
	echo 'false', "\n";	
		
$b = TRUE;
$s = 1 + (($b == TRUE) ? 1 : -1);
echo $s, "\n";
// same as
if ($b == TRUE)
	$s = 1 + (1);
else	
	$s = 1 + (-1);
echo $s, "\n";

$b = FALSE;
$s = 1 + (($b == TRUE) ? 1 : -1);
echo $s, "\n";
// same as
if ($b == TRUE)
	$s = 1 + (1);
else	
	$s = 1 + (-1);
echo $s, "\n";

$b = 1;
echo ($b%2 == 0) ? 'even':'odd', "\n";
$b = 2;
echo ($b%2 == 1) ? 'even':'odd', "\n";
?>
</pre>