Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create Form with Drop-down Boxes for Existing Function WordPress

  • SOLVED

Hello, I need a form with 3 drop-down boxes, populated from the database. The form then needs to pass the values to the function below. The date is in this format: yyyy-mm-dd
Field #1 populates from wp_location.room_desc
Field #2 populates the Month from wp_transactions.date (human readable form i.e. January)
Field #3 populates the Year from wp_transactions.date


And here is the function:

//21 Select - Active Cages Producing < 10
function select_location_producing_10( $attr, $content ) {
global $wpdb;

// Date.
$date = ( isset( $attr['date'] ) ) ? "AND T.date = '" . $attr['date'] . "'" : '';

// Room_desc.
$room_desc = ( isset( $attr['room_desc'] ) ) ? "AND L.room_desc = '" . $attr['room_desc'] . "'" : '';

//Item_category.
$item_category = ( isset( $attr['item_category'] ) ) ? "AND I.item_category = '" .$attr['item_category'] . "'": '';

$query = $wpdb->prepare( "

SELECT
L.cage_desc,
L.rack_desc,
SUM(T.quantity) as sum
FROM
wp_transactions T,
wp_location L
WHERE
T.location_id=L.location_id
$room_desc
AND MONTH($date)
AND YEAR($date)
AND T.trans_type= 'Inventory'
GROUP BY
T.location_id
HAVING
SUM(T.quantity)<=10
ORDER BY
L.rack_desc
" );
$results = $wpdb->get_results( $query );
$return = '';
foreach( $results as $result ) {
$return .= $result->rack_desc . '.....' . $result->cage_desc . '.....' . $result->sum . "</br>\n";
}
return $return;


Note: If I add a '-1' to the end of 'AND MONTH($date)=MONTH(CURDATE())', the function provides the results for last month correctly, so I believe it should work with the form as well.

Answers (1)

2011-08-08

Hai Bui answers:

Hi,

This is the form code:
<form action="" method="post">
<select name="room_desc">
<?php
$query = $wpdb->prepare( "
SELECT DISTINCT room_desc
FROM
wp_location
ORDER BY
room_desc
" );
$results = $wpdb->get_results( $query );
$out='';
foreach( $results as $result ) {
$out.='<option value="'.$result->room_desc.'">'.$result->room_desc.'</option>
';
}
echo $out;
?>
</select>
<select name="wp_transactions_month">
<?php
$query = $wpdb->prepare( "
SELECT DISTINCT MONTH(date) AS 'Month'
FROM
wp_transactions
ORDER BY
Month
" );
$results = $wpdb->get_results( $query );
$out='';
foreach( $results as $result ) {
$out.='<option value="'.$result->Month.'">'.date("F",mktime(0,0,0,$result->Month,1,2000)).'</option>
';
}
echo $out;
?>
</select>
<select name="wp_transactions_year">
<?php
$query = $wpdb->prepare( "
SELECT DISTINCT YEAR(date) AS 'Year'
FROM
wp_transactions
ORDER BY
Year
" );
$results = $wpdb->get_results( $query );
$out='';
foreach( $results as $result ) {
$out.='<option value="'.$result->Year.'">'.$result->Year.'</option>
';
}
echo $out;
?>
</select>
<input type="submit" value="Submit">
</form>


And form processing code:

<?php
$room_desc=$_POST["room_desc"];
$transaction_month=$_POST["wp_transactions_month"];
$transaction_year=$_POST["wp_transactions_year"];
$transaction_date=date("Y-m-d",mktime(0,0,0,$transaction_month,1,$transaction_year));
$attr=array('room_desc'=>$room_desc,'date'=>$transaction_date);

$p=select_location_producing_10($attr,'');
....
?>


BTW, what is $content parameter is for?

Let me know if you have any problem.


Patricia Moff comments:

Hai Bui,
Thank you for your quick response. Should the code be placed in a plug-in? On a page template?
Please forgive me for not understanding the obvious ;)


Hai Bui comments:

You can place the code in a page template.


Patricia Moff comments:

OK, I made a new page template like so:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read more'));?><div style="clear:both;"></div>
<?php endwhile; ?>

FORM CODE HERE?

PROCESSING CODE HERE?

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>


It thru this error:
Parse error: syntax error, unexpected '.' in /Hosting/reports.laynelabs.com/wp-content/themes/Expo/page_room_desc.php on line 103

ideas?


Hai Bui comments:

Can I access your WP Dashboard?


Patricia Moff comments:

yeah, I'll turn off the protection,
There were four '.' in the form processing code, I removed those and we made so progress,
this is the new error:
Undefined index: wp_transactions_month
Undefined index: wp_transactions_year

I don't have tables by those names.
the date is in wp_transactions.date


Patricia Moff comments:

I remove the site protection. If you register, I will upgrade you to admin


Hai Bui comments:

Ok. I've just registered. Pls upgrade me to admin, I will fix it for you.


Patricia Moff comments:

done:)


Patricia Moff comments:

this is your page
lab-techs/form-test-hai-bui/


Patricia Moff comments:

any luck so far?


Patricia Moff comments:

You did it! Nice Job!


Hai Bui comments:

It basically works. I'm fine-tuning it (fixing some errors).


Patricia Moff comments:

I think it might be throwing July 2011 no matter what now???
caching issue? I reloaded the page


Patricia Moff comments:

perfect, thank you so much!