/    Sign up×
Community /Pin to ProfileBookmark

Droplists for numeric data

I’ve got a form with PHP code that builds a droplist from unique elements in the columns of a table. These droplists are then used to filter the data. This is working out pretty well, (after a lot of head scratching and a bunch of help here) but one of my columns is numeric data, and the droplists are being built in such a way that the elements are quoted and thus treated as strings. This of course means I have a nice looking droplist but can’t use it to filter the data.

This is obviously not a new problem, but I’m pretty new to all of this and I can’t figure out how to make the droplists for numeric data form with intetegers rather than strings.

Can anyone help me?

[code=php]<FORM ENCTYPE=”application/x-www-form-urlencoded” method=”get” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”>
<?
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Build and populate the droplists by using an array of column names.
– Distinct values from each column in the EventCombMT table will be used to create a droplist.
– To add or remove a droplist, edit the $columns variable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//~~~~~ loop through the array creating a droplist of ~~~~~//
//~~~~~ distinct values for each column in the array ~~~~~//
foreach( $columns as $temp) {
//set up the query
$query = “SELECT DISTINCT $temp FROM EventCombMT ORDER BY $temp;”;
$result = odbc_exec($connectionstring, $query);
//now build the droplists for each element in the array in the lines below
?>
<select NAME=”<?=$temp?>”>
<option VALUE=””>All <?=$temp?>s</option>
<?php
// create a droplist element for each item in the array
while(odbc_fetch_into($result, $row)) {
// if this droplist element ($row[0]) was passed as a parameter($_GET[$temp]), set the option as selected and give it a distinctive appearance.
if (strtoupper($_GET[$temp]) == strtoupper($row[0])) {
print ” <option value='”.$row[0].”‘ selected=’selected’ style=’background-color: white;font-weight:bold;color:red;’>”.ucwords($row[0]).”</option>n”;
} else {
print ” <option value='”.$row[0].”‘>”.ucwords($row[0]).”</option>n”;
}
}
?>
</select>
<?
}
?>
<INPUT type=”submit” value=”Apply Filter”>
</form>[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@LazyJonesDec 28.2005 — The fact that values are quotet, has nothing to do with data inside (could be string, could be int, boolean, ...). They are quotet because in HTML, all the attributes are quotet. You can still "filter the data" the way you want.

In php, variables are loosely binded. The variable being string or int, depends on the way you use it.
Copy linkTweet thisAlerts:
@scaiferwauthorDec 28.2005 — The fact that values are quotet, has nothing to do with data inside (could be string, could be int, boolean, ...). They are quotet because in HTML, all the attributes are quotet. You can still "filter the data" the way you want.

In php, variables are loosely binded. The variable being string or int, depends on the way you use it.[/QUOTE]


When I try to filter on that one field, I get no results. Any idea why? All other fields are text.
Copy linkTweet thisAlerts:
@LazyJonesDec 28.2005 — That really depend how you are filtering, can you post some code?
Copy linkTweet thisAlerts:
@scaiferwauthorDec 28.2005 — That really depend how you are filtering, can you post some code?[/QUOTE]


Here is is...

[code=php]<? //setup functions
$pagetitle = "EventComb Filter";

// create array of columns to use
$columns = array("Server","LogFile","EventID","EventSource","User");

//initialize sql string to be built on by parsing query string
$Query = "SELECT * FROM EventCombMT WHERE ";

// loop through the columns and create a variable for each column that has been passed through the query string
foreach($columns as $temp)
{
$$temp = (isset($_GET[$temp])) ? $_GET[$temp] : "";
//add parameter to SQL string if a value has been delivered
if($_GET[$temp] != ""){
//$Query = $Query."$temp"."='".$_GET[$temp]."' and ";
$Query = $Query."$temp"."='".$_GET[$temp]."' and ";
}
}

//strip the terminating ' and ' from the sql string
$Query = substr($Query, 0, strlen($Query)-5);


/**************************************************************************************************************/?>

<head>
<title><?= $pagetitle ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" media="screen, projection" href="files/qanet.css" />
<link rel="stylesheet" type="text/css" media="screen, projection" href="files/sidebars.css" />
<style>
.tableheader {
color: navy;
font-size: 14px;
font-weight:bold;
vertical-align: top;
border: 1px solid navy;
}
.tablecell {
color: #6699cc;
font-size: 10pt;
vertical-align: top;
border: 1px solid navy;
}
</style>
</head>

<body margins="2" background="images/bg-greystripe.gif">


<?php
//connect to database
$connectionstring = odbc_connect("EventCombMT", "", "");

//execute query
$queryexe = odbc_do($connectionstring, $Query);

?>

<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PAGE MASTHEAD ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<table bordercolor="navy" border="1" width="100%"><tr><td>
<table bgcolor="white" border=0 cellspacing=0 cellpadding=10 align=left style="border-collapse: collapse; color: navy;" >
<tr>

<td valign="bottom" align="left" width="10%">
<? include("includes/masthead-logo.inc.php"); ?>
</td>
<td valign="bottom" align=left>
<span style="font-size:36px;">EventComb Filter</span>
</td>

</tr>
</table>
</td>
</tr>
</table>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /page masthead ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>


<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FILTER DROPLISTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

<table bordercolor="navy" cellpadding="5" border="1" width="100%"><tr valign=top><td>
<FORM ENCTYPE="application/x-www-form-urlencoded" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Build and populate the droplists by using an array of column names.
- Distinct values from each column in the EventCombMT table will be used to create a droplist.
- To add or remove a droplist, edit the $columns variable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


//~~~~~ loop through the array creating a droplist of ~~~~~//
//~~~~~ distinct values for each column in the array ~~~~~//
foreach( $columns as $temp) {
//set up the query
$query = "SELECT DISTINCT $temp FROM EventCombMT ORDER BY $temp;";
$result = odbc_exec($connectionstring, $query);
//now build the droplists for each element in the array in the lines below
?>
<select NAME="<?=$temp?>">
<option VALUE="">All <?=$temp?>s</option>
<?php
// create a droplist element for each item in the array
while(odbc_fetch_into($result, $row)) {
// if this droplist element ($row[0]) was passed as a parameter($_GET[$temp]), set the option as selected and give it a distinctive appearance.
if (strtoupper($_GET[$temp]) == strtoupper($row[0])) {
//background="images/bg-greystripe.gif"
print " <option value='".$row[0]."' selected='selected' style='background-color: white;font-weight:bold;color:red;'>".ucwords($row[0])."</option>n";
} else {
print " <option value='".$row[0]."'>".ucwords($row[0])."</option>n";
}
}
?>
</select>
<?
}
?>
<INPUT type="submit" value="Apply Filter">
</form>
</td>
<td>
<form>
<INPUT type="submit" action="<?=$_SERVER["PHP_SELF"];?>" value="Clear All">
</form>
</td>
</tr>
</table>
<p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /filter droplists ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->


<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOAD TABLE OF RESULTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->


<?php

//if parameters have been supplied via query string, load table, otherwise prompt for selection parameters
if (count($_GET)) {

while (odbc_fetch_row($queryexe)) {

//*******************************************************************************


// ================================================================================
// OUTPUT DATA IN TABLES
// --------------------------------------------------------------------------------

?>
<table border=1 cellpadding=5 width=100% style='border-style:thin;border-color: #000080;border-collapse:collapse;'>
<tr>
<td class=tableheader>Server</td>
<td class=tableheader>Logfile</td>
<td class=tableheader>EventID</td>
<td class=tableheader>RecordType</td>
<td class=tableheader>EventSource</td>
<td class=tableheader>EventDate</td>
<td class=tableheader>User</td>
<td class=tableheader>EventText</td>
</tr>
<?

$num_rows=0; // initialize record counter

while(odbc_fetch_row($queryexe))
{

//collect results
$server = odbc_result($queryexe, 1);
$logfile = odbc_result($queryexe, 2);
$eventid = odbc_result($queryexe, 3);
$recordtype = odbc_result($queryexe, 4);
$eventsource = odbc_result($queryexe, 5);
$eventdate = odbc_result($queryexe, 6);
$user = odbc_result($queryexe, 7);
$eventtext = odbc_result($queryexe, 8);

//format and display results
print ("<tr>");
print ("<td class=tablecell>$server</td>");
print ("<td class=tablecell>$logfile</td>");
print ("<td class=tablecell>$eventid</td>");
print ("<td class=tablecell>$recordtype</td>");
print ("<td class=tablecell>$eventsource</td>");
print ("<td class=tablecell>$eventdate</td>");
print ("<td class=tablecell>$user</td>");
print ("<td class=tablecell>$eventtext</td>");
print ("</tr>");
$num_rows+=1; //increment by one
}
print ("</table>");

//*******************************************************************************
}

// report number of rows returned
if (!$num_rows) {$num_rows = 0;};
echo "<p>($num_rows records returned.)<p>";


} else {

// No parameters have been supplied via the query string. Prompt for input.
?>
<center>
<div style="padding: 20px 50px 20px 10px; margin: 50px 0 0 0; border: solid 6px #6699cc; font-size:90%;width : 30%;">
<table><tr>
<td><img style="vertical-align:text-bottom; margin: 0px 15px;" src="images/icon_info_md.gif" /></td>
<td>
Select and submit criteria
</td>
</tr></table>
</div>
</center>
<?

}
?>


<?
//diconnect from database
odbc_close($connectionstring);
?>

<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /load table of results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@LazyJonesDec 28.2005 — Oh, now i see what you mean, the quotes in your query string WHERE clause is the problem. I have no straight answer to problem :/ maybe you could handle numeric values separetly. You can do something like this

if(is_numeric($_GET[$temp])) ...
Copy linkTweet thisAlerts:
@NogDogDec 28.2005 — Add this function:
[code=php]
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
[/code]

Then where you "add parameter to SQL string if a value has been delivered":
[code=php]
$Query .= "$temp=" . quote_smart($_GET[$temp]) . " and ";
[/code]
×

Success!

Help @scaiferw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...