Jump to content

Defining & Accessing An ARRAY (TABLE)


mickeymouse

Recommended Posts

I need to set up the following table (or ARRAY) but don't know how to define it.

 

Acct A     month 1    month 2     month 3   …. month 12

Acct B

and so on (unknown quantity )

I tried various solutions but no success.  My latest code is:

//Deffinition:
$AcctNum=array
(
$MyMonths=array()
);

Filling:
$MyMonths[$row[5]][$effmo] = $MyMonths[$row[5]][$effmo] - $row[3];

Extracting:
print("$AcctNum[1] $MyMonths[1] $MyMonths[2] ....... $MyMonths[12]<br>
       $AcctNum[2] $MyMonths[1] $MyMonths[2] ....... $MyMonths[12]<br>
etc.
");

 

Edited by mickeymouse
Link to comment
Share on other sites

Niche,

I've looked at /studied all the W3S documentation and it doesn't solve my problem.

The same with your example (maybe it's a case that I don't understand).

I don't have fixed data to put in the table - like Peter and age etc.  My equivalent of Peter, Ben and Joe

would be account names and I don't know what they are -they are the user's entries and different for different users.

So, what I need is to define the table, then have code to fill the table with the user's input, and then code to extract from the table to print.

If you look at my posting again you will have a better idea of what I mean - I have done an edit giving more info.

(My original posting was incomplete and got posted by accident).

I hope you can help me - I've been working on this all day - wasted time unfortunately.

Thanks

Link to comment
Share on other sites

then your code would look something like this:

$acct_a = 'company abc';
$acct_b = 'company stu';
$acct_c = 'company xyz';
$val_1 = 35;
$val_2 = 1007;
$val_3 = 20004567543;
$accounts = array($acct_a=>$val_1, $acct_b=>$val_2, $acct_c=>$val_3);
var_dump($accounts);

Now everything is variable

EDIT:

Be sure to use => instead of =  AND use array_push() to add to your array

https://www.w3schools.com/php/func_array_push.asp 

 

 

 

Edited by niche
Link to comment
Share on other sites

I don't see how I can use this.  I can't define

$acct_a = 'company abc';

$acct_b = 'company stu';

$acct_c = 'company xyz' etc for 100 or 200 unknown accounts;

nor can I define

$val_1 = 35;

$val_2 = 1007;

$val_3 = 20004567543; maybe 1,200 unknown amounts.

 

What I need is to define a table where I can dump & accumulate data into it

from user data which comes from my database and then retrieve the results from

the table and print/display it in a report form.

 

Link to comment
Share on other sites

I don't know if an SQL query would help.

For the moment, can you look at my code and see if you can identify errors.

I've used this code approach in another report and it works but here, I can't make it work.

Tks


$acnm=array();
$acmo=0;

$numacts = $AffectedRows; 

// LOAD ACCOUNT NAMES FROM ACCOUNTS DB TABLE 
// INTO ACCOUNT NAME TABLE
//   WORKS OK - PROVEN BY A TEST PRINT.
//   $row[2] is the account name index.
//   $row[1] is the account name. 

while ($row = mysql_fetch_row($Results))
    {$acnm[$row[2]]=$row[1];
    }
//-------------------------------------------

// CREATE & INITIALIZE AMOUNTS TABLES
// COVERING 12 MONTHS FOR EACH ACCOUNT NAME

$cntr=0;
while($cntr<$numacts)
    {$cntr=$cntr+1;
    $amts[$cntr]=Array(0,0,0,0,0,0,0,0,0,0,0,0,0);
    }
//-------------------------------------------

// LOAD AMOUNTS FROM TRANSACTIONS INTO TABLES     
// $row[4] and $row[5] are account indexes
// $row[3] is the ammount
$cntr=0;  
while ($row = mysql_fetch_row($Results))
    {$cntr=$cntr+1;
        $effmo = substr($row[1],5,2);  //extract month from date field.
        $amts[$row[4]][$effmo]=$amts[$row[4]][$effmo] - $row[3];   //*
        $amts[$row[5]][$effmo]=$amts[$row[5]][$effmo] + $row[3];   //*
    }
// * Should be OK as I've used this in other reports and it worked.
//-------------------------------------------

// PRINT ACCOUNT FROM ACCOUNTS TABLE & 
// MONTHLY AMOUNTS FROM AMOUNTS TABLES
// THIS IS THE PROBLEM - IT DOESN'T WORK!
    $cntr=0;
    while($cntr<$numacts)
	    {$cntr=$cntr+1;
	        {print("$acnm[$cntr]
	           &#160;$amts[$cntr][1]
	           &#160;$amts[$cntr][2]
	           &#160;$amts[$cntr][3]
	           &#160;$amts[$cntr][4]
	           &#160;$amts[$cntr][5]
	           &#160;$amts[$cntr][6]
	           &#160;$amts[$cntr][7]
	           &#160;$amts[$cntr][8]
	           &#160;$amts[$cntr][9]
	           &#160;$amts[$cntr][10]
	           &#160;$amts[$cntr][11]
	           &#160;$amts[$cntr][12]
	           <br>");
		    }
	    }
// PRINTING RESULTS:
accountA  Array[1]  Array[2]  Array[3]  ...
accountB  Array[1]  Array[2]  Array[3] ...
etc for each account name entry.

 

Link to comment
Share on other sites

Your script has many errors and warnings. 

Can you focus on a specific snippet that's not working for you?

 

 

Edited by niche
Link to comment
Share on other sites

Please tell me where all the errors and warnings are.

All of it seems to work except for the last part which is the printing.

 

// PRINT ACCOUNT FROM ACCOUNTS TABLE & 
// MONTHLY AMOUNTS FROM AMOUNTS TABLES
// THIS IS THE PROBLEM - IT DOESN'T WORK! <-------------
    $cntr=0;
    while($cntr<$numacts)
	    {$cntr=$cntr+1;
	        {print("$acnm[$cntr]
	            $amts[$cntr][1]
	            $amts[$cntr][2]
	            $amts[$cntr][3]
	            $amts[$cntr][4]
	            $amts[$cntr][5]
	            $amts[$cntr][6]
	            $amts[$cntr][7]
	            $amts[$cntr][8]
	            $amts[$cntr][9]
	            $amts[$cntr][10]
	            $amts[$cntr][11]
	            $amts[$cntr][12]
	           <br>");
		    }
	    }
// PRINTING RESULTS:
accountA  Array[1]  Array[2]  Array[3]  ...
accountB  Array[1]  Array[2]  Array[3] ...
etc for each account name entry.

 

Link to comment
Share on other sites

$acnm[$cntr]

makes sense assuming its a numeric array.

{print($acnm[$cntr])};

produces a parse error - print() enclosed with curly brackets

$amts[$cntr][11]

what are you trying to accomplish with [11]?

EDIT:

Seems to me you might be trying to describe a multidimensional array

https://www.w3schools.com/php/php_arrays_multidimensional.asp

If so, what does the dump of $amts look like?

 

Edited by niche
Link to comment
Share on other sites

Re #1:  Actually, $acnm is not a numeric array.  It's an array that contains the account names. $cntr is numeric.  So I don't see a problem with $acnm[$cntr] and it does work.

Re #2:  I agree the print does not need the curly brackets, but I would think that they don't cause a problem.  Anyway, thanks for spotting it and I'm removing them.

Re #3: I expect that I have one of these "$amts[$cntr]=Array(0,0,0,0,0,0,0,0,0,0,0,0,0);" for each account as created by my earlier code:

$cntr=0;

while($cntr<$numacts)

{$cntr=$cntr+1;

$amts[$cntr]=Array(0,0,0,0,0,0,0,0,0,0,0,0,0);

}

Then with $amts[$cntr][1] to $amts[$cntr][12] I am trying to extract the 12 months in each account's array.

 

 

 

Link to comment
Share on other sites

the dump, mor precisely, the print is

AcctNameA Array[1] Array[2] Array[3] ... Array[12]

AcctNameB Array[1] Array[2] Array[3] ... Array[12]

etc for each account name entry.

Obviously my $amts[$cntr][1 to 12] doesn't work. But what should it be?

Link to comment
Share on other sites

You target a multidimensional array in php, through loops, current(), next() just off the top of my head.

Cracking these kind of arrays can be tough.  As I remember, they were for me.

Link to comment
Share on other sites

You need to look up multi dimensional arrays in php, usually you would not go by account name as you could have duplicates so unique I'd ref would be better,

Setting and reading multi dimensional array

$accountData['account1']=[1,3,5,7,9];
$accountData['account3']=[1,2,3,4,5,6,7,8,9,10,11,12];
$accountData['accoun2']=[4,5];
$accountDataKeys = array_keys($accountData);
$accountidcount=count($accountData);
for($i=0; $i<$accountidcount;$i++){
echo $accountDataKeys[$i].': <br>';
  foreach($accountData[$accountDataKeys[$i]] as $key => $value){
echo $key." : ".$value."<br>";
}
}

 

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...