Select use from a MysQl database

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
jrts
Forum Newbie
Posts: 5
Joined: Sat Mar 01, 2003 11:27 am

Select use from a MysQl database

Post by jrts »

:(
What shall I do so when I choose the word "all" from a "select" of a html form, the results that appear are all the values of a column of a data base Mysql. Now the search only works when finds a equivalent with the data base from the "select" html.
Please, find below the program:
Could you have a look at it.


<?php require_once('Connections/conexs.php'); ?>
<?php
$maxRows_consulta = 10;
$pageNum_consulta = 0;
if (isset($HTTP_GET_VARS['pageNum_consulta'])) {
$pageNum_consulta = $HTTP_GET_VARS['pageNum_consulta'];
}
$startRow_consulta = $pageNum_consulta * $maxRows_consulta;

$colname_consulta = "1";
if (isset($HTTP_GET_VARS['poblacio'])) {
$colname_consulta = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['poblacio'] : addslashes($HTTP_GET_VARS['poblacio']);
}
$varpob_consulta = "1";
if (isset($HTTP_GET_VARS['tipus'])) {
$varpob_consulta = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['tipus'] : addslashes($HTTP_GET_VARS['tipus']);
}
mysql_select_db($database_conexs, $conexs);
$query_consulta = sprintf("SELECT tipus, poblacio, zona, foto1, foto2, descrip, preu FROM bens WHERE poblacio= '%s' and tipus = '%s'", $colname_consulta,$varpob_consulta);
$query_limit_consulta = sprintf("%s LIMIT %d, %d", $query_consulta, $startRow_consulta, $maxRows_consulta);
$consulta = mysql_query($query_limit_consulta, $conexs) or die(mysql_error());
$row_consulta = mysql_fetch_assoc($consulta);

if (isset($HTTP_GET_VARS['totalRows_consulta'])) {
$totalRows_consulta = $HTTP_GET_VARS['totalRows_consulta'];
} else {
$all_consulta = mysql_query($query_consulta);
$totalRows_consulta = mysql_num_rows($all_consulta);
}
$totalPages_consulta = ceil($totalRows_consulta/$maxRows_consulta)-1;
?>
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

I would use an "if" statement to check the form's selection, and construct a query if "all" is chosen. For example:

Code: Select all

if($var == "all") &#123;
     $query = "SELECT column FROM table";
&#125;
else &#123;
     $query = "SELECT column FROM table WHERE column='$var'";
&#125;
Hope that helps!
jrts
Forum Newbie
Posts: 5
Joined: Sat Mar 01, 2003 11:27 am

sorry but I'm a learner

Post by jrts »

8O
Where I can put this exactly?
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Re: Select use from a MysQl database

Post by Crashin »

First of all, which variable in your code is coming from the select element of the HTML form?
jrts
Forum Newbie
Posts: 5
Joined: Sat Mar 01, 2003 11:27 am

Post by jrts »

The variable is 'poblacio', this variable give the result especific from the html select, but I want to catch all the values when the select is "All"

In the before post there is the script¡

thanks for all
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Hi jrts,

In your HTML SELECT substitute the Word All for the % character like so:

Code: Select all

<OPTION VALUE="%">All</OPTION>
That way when a user selects the ALL option but when your PHP receives the FORM result it'll receive the % character which will retreive everything in your query.

% is a pattern-matching wildcard which means that if you use WHERE id='%' this will retrieve all the rows where id is not NULL.
Post Reply