Hi,
I would like to give a user more flexibility that he could build query the way he likes. But I have seems like a small issue on which I spent 3 days and still can not resolve.
I will post whole code. If anybody could try and point to where the problem is ?
<?php
function select_is($select_name)
{
$arr_is = array("=" => "is", "!=" => "is not", ">" => "greater than", "<" => "less than", ">=" => "greater than or equal to", "<=" => "less than or equal to");
?>
<select name="<?php echo $select_name; ?>">
<?php foreach ($arr_is as $k=>$v) {?>
<option value="<?php echo $k; ?>"><?php echo $v; ?></option>
<?php }?></select>
<?php
}
function select_dayofmonth($select_name)
{
?>
<select name="<?php echo $select_name; ?>">
<option value="dayofmonth(curdate())">todays</option>
<?php For ($i=1; $i<=31; $i++) {?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
<?php
}
function select_month($select_name)
{
$arr_month = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$r_count = 1;
?>
<select name="<?php echo $select_name;?>">
<option value="month(curdate())">todays</option>
<?php foreach ($arr_month as $v) {?>
<option value="<?php echo $r_count++;?>"><?php echo $v;?></option>
<?php }?></select>
<?php
}
function select_year($select_name)
{
$arr_year = array("1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006");
?>
<select name="<?php echo $select_name;?>">
<option value="year(curdate())">todays</option>
<?php foreach ($arr_year as $v) {?>
<option value="<?php echo $v;?>"><?php echo $v;?></option>
<?php }?></select>
<?php
}
if(isset($_POST['submit'])){
$q = "SELECT * FROM v_table";
$q .= " WHERE ";
$q .= "dayofmonth(col_name)" . $_POST['is'] . $_POST['dayofmonth'];
$q .= " AND ";
$q .= "month(col_name)" . $_POST['is2'] . $_POST['month'];
$q .= " AND ";
$q .= "year(col_name)" . $_POST['is3'] . $_POST['year'];
echo $q;
/*
$result = safe_mysql_query($query);
if (mysql_num_rows($result) > 0) {
$r_count=1;
echo "<table cellpadding=5 border=1>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$r_count++."</td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[6]."</td>";
echo "</tr>";
}
echo "</table>";
}*/
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>day of month</td><td><?php select_is('is'); ?></td><td><?php select_dayofmonth('dayofmonth'); ?></td></tr>
<tr><td>month</td><td><?php select_is('is2'); ?></td><td><?php select_month('month'); ?></td></tr>
<tr><td>year</td><td><?php select_is('is3'); ?></td><td><?php select_year('year'); ?></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="submit" value="Submit Query!"></td></tr>
</table>
</form>
<?php
}
?>
Functions above are to populate select lists. Is this a wrong way to do it?
Query gets built right when anything else selected but less than and todays values. Part of the query just disappears with "<". Why? For ex:
I should get: SELECT FROM v_table WHERE dayofmonth(col_date)<dayofmonth(curdate()) ;but instead whole part is missing:
SELECT FROM v_table WHERE dayofmonth(col_date)
Is this appropriate way to use php the way I do?
Thanks in advance
Vlad