Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Passing Mulit-Select values to PHP query
Old 04-09-2009, 03:45 PM Passing Mulit-Select values to PHP query
Average Talker

Posts: 17
Name: Gary
Trades: 0
Hello. In PHP file #1 is a multiple select box (which Nullpointer helped me on). I need to take the selected items from file #1 into PHP file #2. In file #2, I need to run a query for every selected item, count rows in the results each time the query is run, then provide a total count. Thanks in advance.

The code for file #1 is:
Code:
<?php

//File #1

$dbc = mysql_connect('xxxxx', 'yyyyy','zzzzz')
     or die('Error connecting to MySQL server.');

mysql_select_db('mydb');

$result = mysql_query("select distinct assn from compsin order by assn");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Multi List</title>


</head>

<body>

<form method="post" action="1004mcout.php">

<p>Enter the effective date in the yyyy-mm-dd format</p>

<input type="date" size ="10" id="effdate" name="effdate">

<select multiple name="compsin[ ]" size ="5">

<?php

while ($nt= mysql_fetch_assoc($result)) 
{
     echo '<option value="' . $nt['id'] . '">' . $nt['assn'] . '</option>';
} 
?>
</select>

<p></p>
<p>Click "SUBMIT" </p>

<input type="submit" name="Submit" value="Submit" />

<br />
</form>

</script>
</body>
</html>

The code for File #2 is:

Code:
<?php

// File #2

$dbc = mysql_connect('xxxx', 'yyyyy',xxxxx')
or die('Error connecting to MySQL server.');

mysql_select_db('MyDB');

// Number of closed sales for prior to effective date

$resultc365 = mysql_query("select * from compsin where cldate < '$effdate' ");
$num_rowsc365 = mysql_num_rows($resultc365);

?>

Last edited by gdaniels; 04-09-2009 at 03:49 PM.. Reason: Strange code got copied
gdaniels is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-10-2009, 04:02 AM Re: Passing Mulit-Select values to PHP query
Mad182's Avatar
Skilled Talker

Posts: 54
Name: Madars
Location: Latvia
Trades: 0
Quote:
foreach($_POST['compsin'] as $ntid) {
//do your queries
}
I guess...
__________________
There's no place like 127.0.0.1

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
Old 04-11-2009, 11:13 AM Re: Passing Mulit-Select values to PHP query
Average Talker

Posts: 17
Name: Gary
Trades: 0
Thanks for the reply. I'm just beginning in PHP. I still have a problem and hope someone can help. In the code below, if the SELECT query does not contain && assn ='$value', then everything works. I am trying to run the query for every $value in the array and each time determine the number of $rows for each query result. $rows is added to $total each time to keep a running count of number of rows.

When I run the code below, the ECHO runs for as many times as the FOREACH runs, and $total always is zero. I suspect I have an error in the SELECT statement.

I've checked to make sure the array is valid by echoing $key and $value, and the database has records that meet the query criteria so the query should produce results.

Thanks in advance.


Code:
<?php

$dbc = mysql_connect('xxxx', 'yyyy','xxxx')
or die('Error connecting to MySQL server.');

mysql_select_db('mydb');

// Number of closed condo sales for 7-12 months prior to effective date

$total = 0;

foreach ($_POST['condo'] as $key => $value){

$results = mysql_query("select * from compsin where cldate > '$effdate365' && cldate < '$effdate182' && assn ='$value' ");

$rows = mysql_num_rows($results);

if ($rows < 1)

{

echo "No Matching records.<br>";

}

else

{

$total = ($rows + $total);

}
}

?>

Last edited by gdaniels; 04-11-2009 at 11:15 AM.. Reason: Correct code
gdaniels is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Passing Mulit-Select values to PHP query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12143 seconds with 12 queries