Hi all:

New with PHP and may in a bit over my head.

I am posting 5 variables to a page. Some of these variables may be NULL. I want to set up an array including only those variables that are not NULL and use that array to send out emails. So if 3 of the variables are not NULL 3 different emeials will go out.

I am having trouble understanding how to dynamically fill the array just with non-NULL variables. I am trying the following but I am sure this is incorrect. Any help appreciated.

	array(
	if ($idate1 != NULL){
		$idate1.",";
	}

if ($idate2 != NULL){
	$idate2.",";	
}		

if ($idate3 != NULL){
	$idate3.",";	
}				

if ($idate4 != NULL){
	$idate4.",";	
}

if ($idate5 != NULL){
	$idate5;	
}
)

    I would simply have the original form post elements named "idate[]" to the processing script; that way, $_POST['idate'] would be an array of values POST'ed.

    You could then use something like [man]array_filter/man to remove the elements that are empty.

      Thank you bradgrafelman. I got this to work and am now pulling in an array made up of the POSTed data. Here is an example of what I now have to manipulate:

      Array ( [1] => 3/18/2011 [2] => 3/19/2011 [5] => 3/20/2011 ) 

      There are 2 things I want to do with this info (I will save the second until I get this sorted out):

      1. Enter it as a record in a DB formatted like "3/18/2011, 3/19/2011, 3/20/2011"
        I have tried inserting $idatearray (this is the array variable that is for the code above). I tried using parameters like the following:
      $params3 = array($firstname, $lastname, $location, $username,$datelast, $promoter, $showid, $idatearray);// This does not work
      
      $params3 = array($firstname, $lastname, $location, $username,$datelast, $promoter, $showid, $idatearray[]);//This does not work
      
      $params3 = array($firstname, $lastname, $location, $username,$datelast, $promoter, $showid, $idatearray[1]);//This DOES work but only inputs the first member of the array
      

      How do I get this to work?

        You should not be storing multiple values in a single column - that violates basic normalization techniques (and/or basic DB relational design techniques).

          WHOA! I never knew that.

          OK...now what? Instead of 1 column in the DB that holds "3/18/2011, 3/19/2011, 3/20/2011" do I set up 5 individual columns (only 3 of the 5 options were originally checked which is why only 3 options are showing up in this example)? So in this case, 3 would be filled with a single date and 2 would be empty?

            Can you tell us a little more about the actual application here, e.g. what these dates actually mean and how you use them?

            EDIT: Also, can you show us the CREATE TABLE definition of the table you're currently working with?

              These dates are being used more as text strings than as dates. I will not be using this info to compare one date relative to another. It is more a case of sending out an email saying "Your admission date is 3/18/2011" and then a second email saying "Your admission date is 3/19/2011" etc.

              I did not use CREATE to start the table but I entered it manually in SQL manager (MSSQL) . It is in NVARCAR format, not datetime.

                If you are never planning on actually using these dates in any way as anything other than one large string, then sure, you could consider the string "3/18/2011, 3/19/2011, 3/20/2011" to contain atomic data (e.g. it can't be divided into smaller components) and just store it like that.

                My natural tendency, however, is to never make such assumptions. For example, even if I don't use these dates individually right now, perhaps I might in the future. As such, I would normalize the DB schema out and have a table that relates a DATE value with the row in the current table (a foreign key relationship). You'd then have a one-to-many relationship between your current table and this new date relation table (e.g. there'd be 1 row in the latter table for each date the user submitted).

                  I think I understand your answer. Would it go back to my post a couple of posts up?

                  Instead of 1 column in the DB that holds "3/18/2011, 3/19/2011, 3/20/2011" do I set up 5 individual columns (only 3 of the 5 options were originally checked which is why only 3 options are showing up in this example)? So in this case, 3 would be filled with a single date and 2 would be empty?

                    I would simply use another table. Creating multiple columns like "date1", "date2", etc. still seems un-normalized to me as well as just being more awkward to work with.

                      OK, now I think I am with you. Just a single column called EventDate (for example) and each entry its own row. So "3/18/11" would have its own row, "3/19/11" its own row and so on.

                        Well, no, you'd actually need two or more columns - one to contain a DATE value, and one to contain the value(s) needed to form a foreign key relationship between the rows in that table with the ones in the main table.

                          bradgrafelman;10976286 wrote:

                          Well, no, you'd actually need two or more columns - one to contain a DATE value, and one to contain the value(s) needed to form a foreign key relationship between the rows in that table with the ones in the main table.

                          Yes, all set on that.

                          Thanks again for all your help with this. I learned a LOT.🙂

                            Write a Reply...