Array help please?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Array help please?

Post by ramblin54321 »

I've seen many examples and still don't get it.

Code: Select all

while ($i < $num) 
{
$Last=mysql_result($result,$i,"Last");
$First=mysql_result($result,$i,"First");
print('<tr><td width="30"><input name="Last[$i]" size="30" value=');
print("$Last");
print('></td>
<td width="30"><input name="First[$i]" size="30" value=');
print("$First");
print('></td>
<td><input name="RegHours[$i]" value="" size="5"></td>
<td><input name="OTHours[$i]" value="" size="5"></td>
<td><input name="DTHours[$i]" value="" size="5"></td>
<td><input name="HolHours[$i]" value="" size="5"></td>
<td><input name="VacHours[$i]" value="" size="5"></td>
<td><input name="SLHours[$i]" value="" size="5"></td></tr>');
$i++;}
print("<tr><td><INPUT TYPE=submit NAME=submit VALUE='Calculate'></form></td></tr></table>");
?>
I think that gets what I want into the array, but not sure.
The bigger problem is putting the post variables into regular variables, go ahead and laugh as long as you can point me in the right direction:

Code: Select all

<?
$PayThroughDate=$_POST['PayThroughDate'];
print("$PayThroughDate");
for($i = 0; $i < 10; $i++){ 
$Last[i]=$_POST['Last[$i]'];
$First[i]=$_POST['First[$i]'];
$RegHours[i]=$_POST['RegHours[$i]'];
$OTHours[i]=$_POST['OTHours[$i]'];
$DTHours[i]=$_POST['DTHours[$i]'];
$HolHours[i]=$_POST['HolHours[$i]'];
$VacHours[i]=$_POST['VacHours[$i]'];
$SLHours[i]=$_POST['SLHours[$i]'];
print("$Last, $First, $RegHours, $OTHours, $DTHours, $HolHours, $VacHours, $SLHours ");
}; 
?>
I've tried with and without the dollar sign.
What I'm trying to do is list all of the employee names from the database, put in all their hours at the same time before submitting them instead of having to submit them record by record.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Array help please?

Post by fugix »

what errors do you receive?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Array help please?

Post by social_experiment »

Just using [ ] for each item doesn't mean they are put in the same array.

Code: Select all

<?php
 $array = array();
 // this places an array with items in the second element of the array called
 // $array.
 $array[1] = array('item', 'item1', 'item2'); 
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array help please?

Post by superdezign »

Run this after your form is submitted:

Code: Select all

print_r($_POST);
Form arrays are treated as real arrays after PHP gets the POST data. That means that all of the RegHours fields, for example, are in $_POST['RegHours']. $_POST['RegHours'] is an array of size $num. This works the same way with GET data, too.
Also, just a sidenote... Putting $i in single quotes like you did gives you literal "$i", not the value in $i.
As another sidenote, you don't have to explicitly give each form array field an index. You could do this instead (which will also fix a few of the occurrences of what I just mentioned above):

Code: Select all

while ($i < $num) {
    $Last=mysql_result($result,$i,"Last");
    $First=mysql_result($result,$i,"First");
    print('<tr><td width="30"><input name="Last[]" size="30" value=');
    print($Last);
    print('></td>
    <td width="30"><input name="First[]" size="30" value=');
    print($First);
    print('></td>
    <td><input name="RegHours[]" value="" size="5"></td>
    <td><input name="OTHours[]" value="" size="5"></td>
    <td><input name="DTHours[]" value="" size="5"></td>
    <td><input name="HolHours[]" value="" size="5"></td>
    <td><input name="VacHours[]" value="" size="5"></td>
    <td><input name="SLHours[]" value="" size="5"></td></tr>');
    $i++;
}

print("<tr><td><INPUT TYPE=submit NAME=submit VALUE='Calculate'></form></td></tr></table>");
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

Great, fantastic! So I take out all of the $i and it works. print_r($_POST); shows me that the information is in the arrays. I am half way there. I tried the second half on my own but sorry to say, still need help. I tried this:

Code: Select all

include ('settings.php');
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT Last,First FROM allowances ORDER by Last Asc";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) 
{
$Last=$_POST[Last]; 
$First=$_POST[First];
$RegHours=$_POST[RegHours];
$OTHours=$_POST[OTHours];
$DTHours=$_POST[DTHours];
$HolHours=$_POST[HolHours];
$VacHours=$_POST[VacHours];
$SLHours=$_POST[SLHours];
print("$Last, $First, $RegHours, $OTHours, $DTHours, $HolHours, $VacHours, $SLHours"); 
$i++;}
and got this: Array, Array, Array, Array, Array, Array, Array, Array Array, Array, Array, Array, Array, Array, Array, ArrayArray, Array, Array, Array, Array, Array, Array, Array.
I tried this:

Code: Select all

foreach($_post[x] as $key) 
echo $key; 
substituting my variables for x, and got the variables for each field together instead of by record. I don't want all of the RegHours together. I want each variable representing each field of each record and then continuing through the next records. Once I get them all out of the arrays and into regular variables, I will make an insert statement for the database. I tried all of the foreach lines together and then the print $key and it still didn't work. Thanks for any help.
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

O.K., I still don't know how to get the variables out of the array into regular variables but I did manage to insert everything except the date. Why doesn't the date work? And how do I get the array variables into dynamic regular variables?

Code: Select all

<?
$PayThroughDate=$_POST['PayThroughDate'];
print("$PayThroughDate");
include ('settings.php');
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT Last,First FROM allowances ORDER by Last Asc";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$sql = "INSERT INTO hours (PayThroughDate, Last, First, RegHours, OTHours, DTHours, HolHours, VacHours, SLHours) VALUES 
('$PayThroughDate', '$Last[$i]', '$First[$i]', '$RegHours[$i]', '$OTHours[$i]', '$DTHours[$i]', '$HolHours[$i]', '$VacHours[$i]', '$SLHours[$i]')";
$i++;
mysql_query ($sql);
 } 
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array help please?

Post by superdezign »

In MySQL, you have to send date values in the correct format. In MySQL, the default format for DATE is YYYY-MM-DD and the default format for DATETIME is YYYY-MM-DD HH:MM:SS. So, you should probably replace the value in $PayThroughDate with strtotime('Y-m-d H:i:s', $PayThroughDate). Also, I've noticed that you are running a query simply to find the amount of rows that it returns. For this, you should use MySQL's COUNT() function. And while we're on the subject, the function is mysql_num_rows(), not mysql_numrows().

Also, as a sidenote, put quotes around array indexes. When you don't use quotes, PHP assumes that you are using constants before assuming that you are using strings. Also, you don't need to put quotes around variables to print them. I modified your code as well as replaced the while loops with for loops.

Code: Select all

include ('settings.php');
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT COUNT(*) as `count` FROM `allowances` ORDER BY `Last` ASC";
$result = mysql_query($query);

for ($i = 0, $num = mysql_result($result, 0); $i < $num; $i++) {
    $Last = $_POST['Last']; 
    $First = $_POST['First'];
    $RegHours = $_POST['RegHours'];
    $OTHours = $_POST['OTHours'];
    $DTHours = $_POST['DTHours'];
    $HolHours = $_POST['HolHours'];
    $VacHours = $_POST['VacHours'];
    $SLHours = $_POST['SLHours'];
    print("$Last, $First, $RegHours, $OTHours, $DTHours, $HolHours, $VacHours, $SLHours"); 
}

Code: Select all

$PayThroughDate= strtotime('Y-m-d H:i:s', $_POST['PayThroughDate']);
print($PayThroughDate);
include ('settings.php');
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT COUNT(*) AS `count` FROM `allowances` ORDER BY `Last` ASC";
$result = mysql_query($query);

for ($i = 0, $num = mysql_result($result, 0); $i < $num; $i++) {
    $sql = "INSERT INTO `hours` (`PayThroughDate`, `Last`, `First`, `RegHours`, `OTHours`, `DTHours`, `HolHours`, `VacHours`, `SLHours`) VALUES 
    ('$PayThroughDate', '$Last[$i]', '$First[$i]', '$RegHours[$i]', '$OTHours[$i]', '$DTHours[$i]', '$HolHours[$i]', '$VacHours[$i]', '$SLHours[$i]')";
    mysql_query ($sql);
 }
Untested, so there might be an error or two.
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

Hi Superdezign,
thanks for the reply. I tried your code exactly as you have it and got this: Array, Array, Array, Array, Array, Array, Array, ArrayArray, Array, Array, Array, Array, Array, Array, Array
So I still am unable to get the variables out of the array. And therefore unable to do calculations. As for the date, I know about the correct format and it will insert it when put in manually as 11-04-19 but for some unknown reason will not insert it as '$PayThroughDate'. I tried it without the quotes and still the database shows: 0000-00-00. In php admin, I left the Length/Values blank, if that matters?
The insert that worked:

Code: Select all

while ($i < $num) {
$sql = "INSERT INTO hours (PayThroughDate, Last, First, RegHours, OTHours, DTHours, HolHours, VacHours, SLHours) VALUES 
($PayThroughDate, '$Last[$i]', '$First[$i]', '$RegHours[$i]', '$OTHours[$i]', '$DTHours[$i]', '$HolHours[$i]', '$VacHours[$i]', '$SLHours[$i]')";
$i++;
mysql_query ($sql);
 } 
except for the date, if I were to try $Last=$Last[$i], $First=$First[$i], $RegHours=$RegHours[$i] it would only store the last value of the array in the variable $Last (changes on each iteration). Do you see what I'm saying? How do I get different variables $Last for each value of it in the array?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array help please?

Post by superdezign »

Sorry, I incorrectly used strtotime, which is why your date is wrong. Replace "strtotime('Y-m-d H:i:s', $_POST['PayThroughDate'])" with "date('Y-m-d H:i:s', strtotime($_POST['PayThroughDate']))". And, if you are using a DATE value instead of DATETIME value, get rid of the "H:i:s" in the format string.

As for it displaying "Array Array...", that's expected. You are attempting to echo arrays, which PHP converts to the string "Array." If you want to see the data in the arrays, use print_r().
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

I am able to get the values into the database but now I want them in variables so that I can do multiplication and so forth on them. The following code when printed shows correct values but for the variable names just puts zeros:

Code: Select all

while ($i < $num) {

print("$PayThroughDate");
${$Last.$i} = $Last[$i]; 
${$First.$i} = $First[$i];
${$RegHours.$i} = $RegHours[$i];
${$OTHours.$i} = $OTHours[$i];
${$DTHours.$i} = $DTHours[$i];
${$HolHours.$i} = $HolHours[$i];
${$VacHours.$i} = $VacHours[$i];
${$SLHours.$i} = $SLHours[$i];
print("${$Last.$i} = $Last[$i]; 
${$First.$i} = $First[$i];
${$RegHours.$i} = $RegHours[$i];
${$OTHours.$i} = $OTHours[$i];
${$DTHours.$i} = $DTHours[$i];
${$HolHours.$i} = $HolHours[$i];
${$VacHours.$i} = $VacHours[$i];
${$SLHours.$i} = $SLHours[$i];");
$i++;
}
in other words, the part after the equal sign is correct, but the part before the equal signs is coming up as zeros.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array help please?

Post by superdezign »

I.. have no idea what you are trying to do. You are creating new variables named "Array0" and overwriting it a few times, then "Array1" and overwriting it a few times, etc.

I think the best way for you to approach this is to output each array using print_r(), THEN decide what to do with the data. To make it easier to read, surround it in "<pre>" tags.
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

What I am trying to do is put each field of each record into a variable. I do not want a stinking array! I want $RegHours[0] to be an a variable called $RegHours0 and $RegHours[1] to be in a variable called $RegHours1 so that I can multiply each $RegHours by the appropriate pay rate.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Array help please?

Post by social_experiment »

Code: Select all

// is $num defined?
while ($i < $num) {

print("$PayThroughDate");
${$Last.$i} = $Last[$i]; 
${$First.$i} = $First[$i];
${$RegHours.$i} = $RegHours[$i];
${$OTHours.$i} = $OTHours[$i];
${$DTHours.$i} = $DTHours[$i];
${$HolHours.$i} = $HolHours[$i];
${$VacHours.$i} = $VacHours[$i];
${$SLHours.$i} = $SLHours[$i];
print("${$Last.$i} = $Last[$i]; 
${$First.$i} = $First[$i];
${$RegHours.$i} = $RegHours[$i];
${$OTHours.$i} = $OTHours[$i];
${$DTHours.$i} = $DTHours[$i];
${$HolHours.$i} = $HolHours[$i];
${$VacHours.$i} = $VacHours[$i];
${$SLHours.$i} = $SLHours[$i];");
$i++;
}
You probably defined $num elsewhere, if not you should. (You should probably define $i as well).

${$Last.$i} doesn't make sense. You assign values to a variable as such : $a = 5;, in your case it will be $Last.$i = $Last[$i];.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array help please?

Post by superdezign »

I think you meant to use ${'Last' . $i} or ${"Last$i"}. But I don't see why you'd rather type out every variable instead of using a loop to handle it all in smaller pieces of code. I recommend you actually learn to use the constructs provided to you when programming. Otherwise, you're wasting your time learning the language.
ramblin54321
Forum Commoner
Posts: 32
Joined: Wed Nov 18, 2009 5:31 am

Re: Array help please?

Post by ramblin54321 »

I finally got it :D. What was so hard to find out was that I needed to change the name of the variables so that they are different from the array variable names. The following finally worked:

Code: Select all

<?
$PayThroughDate=$_POST['PayThroughDate'];
$Last=$_POST['Last'];
$First=$_POST['First'];
$RegHours=$_POST['RegHours'];
$OTHours=$_POST['OTHours'];
$DTHours=$_POST['DTHours'];
$HolHours=$_POST['HolHours'];
$VacHours=$_POST['VacHours'];
$SLHours=$_POST['SLHours'];

include ('settings.php');
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT Last,First FROM allowances ORDER by Last Asc";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
print("$PayThroughDate");
while ($i < $num) {
    
$LastName = "Last".$i;
${$LastName} = $Last[$i];
$FirstName = "First".$i;
${$FirstName} = $First[$i];
$RegHoursName = "RegHours".$i;
${$RegHoursName} = $RegHours[$i];
$OTHoursName = "OTHours".$i;
${$OTHoursName} = $OTHours[$i];
$DTHoursName = "DTHours".$i;
${$DTHoursName} = $DTHours[$i];
$HolHoursName = "HolHours".$i;
${$HolHoursName} = $HolHours[$i];
$VacHoursName = "VacHours".$i;
${$VacHoursName} = $VacHours[$i];
$SLHoursName = "SLHours".$i;
${$SLHoursName} = $SLHours[$i];
  
print("$LastName = ${$LastName}, $FirstName = ${$FirstName}, $RegHoursName = ${$RegHoursName}, $OTHoursName = ${$OTHoursName},
$DTHoursName = ${$DTHoursName}, $HolHoursName = ${$HolHoursName}, $VacHoursName = ${$VacHoursName},
$SLHoursName = ${$SLHoursName}<br>");
$i++;
}


Post Reply