Verifying condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Igguana
Forum Commoner
Posts: 36
Joined: Tue Mar 11, 2003 1:08 pm

Verifying condition

Post by Igguana »

I have a form to search in a database with 5 dropdown menus inside, the values choosen there, are catched by this code. The only problem is that the field 'Precio' on the database has to be <OR= (smaller or equal) than the one catched from the form, never bigger. :oops:
I do not know how to introduce this condition in this code without crashing it.

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

$fields = array('Modo','Localidad','Tipo','Habitaciones','Precio');
$clauses = array();
for($i=0; $i!=count($fields); $i++)
{
if (isset($_POST['envio'.($i+1)]) && $_POST['envio'.($i+1)] != -1)
$clauses[] = "$fields[$i]='{$_POST['envio'.($i+1)]}'";
}

$query = "SELECT * FROM $tabla";
if (count($clauses) > 0)
$query .= ' WHERE '.join('AND ', $clauses);
$pegar=mysql_query($query, $conexion) or die($query.' :'.mysql_error());
?>
<TABLE BORDER="1" CELLSPACING="1" CELLPADDING="1">
<TR>
<TD>Modo</TD>
<TD>Localidad </TD>
<TD>Tipo</TD>
<TD>Habitaciones</TD>
<TD>Precio</TD>
</TR>
<?php
while($row = mysql_fetch_array($pegar))
{
?>
<TR>
<TD><?php echo $row["Modo"]; ?></TD>
<TD><?php echo $row["Localidad"]; ?></TD>
<TD><?php echo $row["Tipo"]; ?></TD>
<TD><?php echo $row["Habitaciones"]; ?></TD>
<TD><?php echo $row["Precio"]; ?></TD>
</TR>
<?php
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oh, then it might be better to unroll the thing again. For only five elements... ;)
replace
$fields = array('Modo','Localidad','Tipo','Habitaciones','Precio');
$clauses = array();
for($i=0; $i!=count($fields); $i++)
{
if (isset($_POST['envio'.($i+1)]) && $_POST['envio'.($i+1)] != -1)
$clauses[] = "$fields[$i]='{$_POST['envio'.($i+1)]}'";
}
by

Code: Select all

$clauses = array();
if (isset($_POST['envio1'] ) && $_POST['envio1'] != -1)
	$clauses[] = 'Modo='.(int)$_POST['envio1'];
if (isset($_POST['envio2'] ) && $_POST['envio2'] != -1)
	$clauses[] = 'Localidad='.(int)$_POST['envio2'];
if (isset($_POST['envio3'] ) && $_POST['envio3'] != -1)
	$clauses[] = 'Tipo='.(int)$_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'];
Igguana
Forum Commoner
Posts: 36
Joined: Tue Mar 11, 2003 1:08 pm

Thank You!!

Post by Igguana »

Your code allways works fine.
Now I'm trying to show the results grouped in the amount of five, and to go the next five or come back to the previous.
I'm using "LIMIT" in the query for doing this, but I have a mysql_num_rows function there that is responsible to show the total amount of coincidences, and by this way, the variable returns the values asigned to LIMIT, not the total one.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

in mysql4 there is a way to determine the number of records before a limit clause took place (http://www.mysql.com/doc/en/Miscellaneous_functions.html#IDX1361)
But since you probably use a 3.xx version try

Code: Select all

...

$query = "FROM $tabla";  // removed SELECT * here
if (count($clauses) > 0)
	$query .= ' WHERE '.join('AND ', $clauses);

$pegar = mysql_query('SELECT count(*) '.$query, $conexion) or die($query.' :'.mysql_error());
$totalAmount = array_shift(mysql_fetch_row($pegar));

$query = 'SELECT * ' . $query;
$pegar=mysql_query($query, $conexion) or die($query.' :'.mysql_error());
http://www.mysql.com/doc/en/Counting_rows.html
http://www.php.net/manual/en/function.array-shift.php
Igguana
Forum Commoner
Posts: 36
Joined: Tue Mar 11, 2003 1:08 pm

Unable to do it alone?

Post by Igguana »

Sorry one more time, and thank U.
Should I introduce the LIMIT condition in this search or in a different one. It geves me an error when I introduce it here.
I have another problem also, I wont to LIMIT the results to five and to give the user the posibility to go to the next five or to return to the previous. For that, I introduced a link at the end of the results that saids "go to next 5" and in the href statement I introduce again the vars envio1, envio2, envio3 etc. It doesn't work.
Is his the right way?
Post Reply