[PHP] foreach insert error

2009-10-07 Thread Haig Davis
Hello All,

I have spent the entire day trying to get my calendar app to function
correctly ---  I have no problem with the actual functioning of the
calendar. What is giving me trouble is for each calendar day the user has
the option to check a checkbox requesting the day off and additionally the
user must select a weighting for the request i.e. 1-31 with 1 being 1st
choice and 31 being their least desirable request. The request is then
insered into a mysql database.

I am relativly new to PHP so I may well be on the complete wrong track here.
so far I have tried imploding the array  posting an associative array.

Form Script:

div class=workspace
?php
if(isset($_POST['submit'])){

$userID = mysql_real_escape_string(trim($userID));
$year = mysql_real_escape_string(trim($_GET['year']));
$month = mysql_real_escape_string(trim($_GET['mon']));
$dayoff = $_POST['dayoff'];
$weighting = mysql_real_escape_string(trim($_POST['weight']));
var_dump($dayoff);

foreach ($dayoff as
$day){ //come back
to this when figured out that inserting day works to inser wieghting

$sql = INSERT INTO dayoff (userID, month, year, day) VALUES
('$userID', '$year', '$month', '$day');

$q = mysql_query($sql)
or die ('error making request');
}


 }
?
div class=form
form name=form method=post action=?php $_SERVER['PHP_SELF'] ; ?
h2Request Day Off/h2br /br /
?php include '../includes/dayoffcalendar.php' ; ?

br /br /br /
button type=submit name=submit value=Submit
class=buttonSubmit/button
button type=reset class=buttonReset/button
/form

Calendar Script:


for($i = 1; $i = $last['mday']; $i++){
   if($i == $today['mday']  $first['mon'] == $today['mon'] 
$first['year'] == $today['year']){
  $style = 'today';
   }
   else{
  $style = 'day';
   }
   $filename=$1.['month'].['year'];
   echo '  div class=' . $style . '' . $i . 'br /br /' ; // creates
comment box for points and check box for request day off
   echo '  labelsmallPoint Weighting: /small/labelinput type=text
name=weight$i[] size =3 maxlength=3 /Br /br / ';
   echo '  labelsmallCheck Day Off: /small/labelinput type
=checkbox name=dayoff[] value=\. $i . \ /br / ';
   echo '  /div' . \n;

   }

I appreciate any advice that you may have. Thank you very much in advance,
my head hurts and I've googled for a few hours with no joy.

Thanks

Haig


Re: [PHP] foreach insert error

2009-10-07 Thread Jim Lucas
Haig Davis wrote:
 Hello All,
 
 I have spent the entire day trying to get my calendar app to function
 correctly ---  I have no problem with the actual functioning of the
 calendar. What is giving me trouble is for each calendar day the user has
 the option to check a checkbox requesting the day off and additionally the
 user must select a weighting for the request i.e. 1-31 with 1 being 1st
 choice and 31 being their least desirable request. The request is then
 insered into a mysql database.
 
 I am relativly new to PHP so I may well be on the complete wrong track here.
 so far I have tried imploding the array  posting an associative array.
 
 Form Script:
 
 div class=workspace
 ?php
 if(isset($_POST['submit'])){
 
 $userID = mysql_real_escape_string(trim($userID));
 $year = mysql_real_escape_string(trim($_GET['year']));
 $month = mysql_real_escape_string(trim($_GET['mon']));
 $dayoff = $_POST['dayoff'];
 $weighting = mysql_real_escape_string(trim($_POST['weight']));
 var_dump($dayoff);
 
 foreach ($dayoff as
 $day){ //come back
 to this when figured out that inserting day works to inser wieghting
 
 $sql = INSERT INTO dayoff (userID, month, year, day) VALUES
 ('$userID', '$year', '$month', '$day');
 
 $q = mysql_query($sql)
 or die ('error making request');
 }
 
 
  }
 ?
 div class=form
 form name=form method=post action=?php $_SERVER['PHP_SELF'] ; ?
 h2Request Day Off/h2br /br /
 ?php include '../includes/dayoffcalendar.php' ; ?
 
 br /br /br /
 button type=submit name=submit value=Submit
 class=buttonSubmit/button
 button type=reset class=buttonReset/button
 /form
 
 Calendar Script:
 
 

I see a number of typo's in the following code.


 for($i = 1; $i = $last['mday']; $i++){
if($i == $today['mday']  $first['mon'] == $today['mon'] 
 $first['year'] == $today['year']){
   $style = 'today';
}
else{
   $style = 'day';
}

What are you trying to do here?

Should this...
$filename=$1.['month'].['year'];

Be this...
$filename=$i.['month'].['year'];

Plus, where are you actually using the previous line at?

echo '  div class=' . $style . '' . $i . 'br /br /' ; // creates
 comment box for points and check box for request day off
echo '  labelsmallPoint Weighting: /small/labelinput type=text
 name=weight$i[] size =3 maxlength=3 /Br /br / ';

Should you have weight$i[] in the previous line?  This will break a few things
I think.

echo '  labelsmallCheck Day Off: /small/labelinput type
 =checkbox name=dayoff[] value=\. $i . \ /br / ';
echo '  /div' . \n;
 
}

Over all, simply put, if you do not number the indexes of your array, you will
not be able to have a 1 to 1 association in your submitted array(s)

BTW: I would try and use the heredoc syntax in your example...  Makes things way
cleaner!

echo DAY

div class={$style}{$i}br /br /
labelsmallPoint Weighting: /small/label
input type=text name=days[{$i}][weight] size =3 maxlength=3 /Br 
/br /
labelsmallCheck Day Off: /small/label
input type=checkbox name=days[{$i}][dayoff] value={$i} /br /
/div

DAY;


Now, on your input side.  Try it a few times to see what you get and I think you
will be able to figure out how to eliminate the days that were not selected /
checked.

 
 I appreciate any advice that you may have. Thank you very much in advance,
 my head hurts and I've googled for a few hours with no joy.
 
 Thanks
 
 Haig
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] foreach insert error

2009-10-07 Thread Paul M Foster
On Wed, Oct 07, 2009 at 03:31:14PM -0700, Haig Davis wrote:

 Hello All,
 
 I have spent the entire day trying to get my calendar app to function
 correctly ---  I have no problem with the actual functioning of the
 calendar. What is giving me trouble is for each calendar day the user has
 the option to check a checkbox requesting the day off and additionally the
 user must select a weighting for the request i.e. 1-31 with 1 being 1st
 choice and 31 being their least desirable request. The request is then
 insered into a mysql database.
 
 I am relativly new to PHP so I may well be on the complete wrong track here.
 so far I have tried imploding the array  posting an associative array.

You don't actually saw what your real problem is, and I'm too lazy (and
tired) to try to figure it out from your code. Can you elaborate?

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php