...

View Full Version : Arrays not working properly??



sea4me
05-05-2009, 03:04 AM
Hi!

I have this:

$month = array();

$month[1] = 0;
for($i=0; $i<11; $i++){
$month[] = 0;
}

$query = "SELECT * FROM order_table WHERE `CUSTOMER` = '***the current customer*****'";
$sql = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_row($sql)) {

$id = $row[0];
$date = $row[1];

// Date EX:03-04-2009 @ 23:23

list($month, $day, $year_time) = explode("-", $date);
list($year, $time) = explode(" @ ", $year_time);

if($year == $request_year){

for($i=0; $i<12; $i++){
if($month = $i){
$month[$i] = $month[$i] + 1;
}
}

}


}

print_r($month);

It's suppose to print the array but instead it prints 11 :confused:

I'm new to arrays so any help will be appreciated... :)

fullmontez
05-05-2009, 04:41 AM
Hi sea4me,

I am by no means an expert, but I noticed one little quirk.



$month[1] = 0;
for($i=0; $i<11; $i++){
$month[] = 0;
}

It seems you are running a for loop but you are not using the variable $i in the code to be executed. Maybe this would help?



$month[1] = 0;
for($i=0; $i<11; $i++){
$month[$i] = 0;
}

bdl
05-05-2009, 05:25 AM
@fullmontez> Nice guess, but actually I think the OP's original code gives the correct result; it initiates an array named $month (appropriate) with a starting index of '1', then proceeds to fill 11 more indexes set to 0 for a total of 12. The empty brackets [] style pushes values into the next available space. Your code example creates a '0' index, then proceeds to manually set indexes 2 ~ 10.

@sea4me> It's hard to guess what your code is *supposed* to do without code comments or spelling it out in your post. It's just a hunch, but I think whatever you're trying to do can probably be accomplished in the SQL statement.

venegal
05-05-2009, 09:26 AM
Well first of all you can't use $month for both a string and an array at the same time --
$month[$i] = $month[$i] + 1; should produce an error, because $month is already a string at that time.

For some reason, it just doesn't execute, so the last time $month is assigned a value is here
if($month = $i) , and the last time that happens, $i equals 11, so no surprise here. You really want to use
if($month == $i) instead.

sea4me
05-07-2009, 03:12 AM
how should I handle this?

It there anyway to have $i to be in $month like this: $month_$i so it will be $month_01 $month_02 .... :confused:

venegal
05-07-2009, 01:12 PM
Yes, but its not a very smart thing to do.

I'm afraid you didn't quite understand me there, so I'll try again:
You have two very specific errors in your code that need changing.

1.) You use the same variable ($month) for two different things (an array and a string) at the same time, which doesn't work. Just rename $month (the array, all over the code) to something more descriptive like $ordersPerMonth, and leave $month (the string, two times in the code) as is.

2.) The "condition" in your if statement isn't really a condition but an assignment. What it does is it assigns the value of $i to the variable $month, and then checks that value for truth. The first time around, $i is 0, which evaluates to false, so your if block doesn't execute. Every other time $i is a positive integer, which evaluates to true, so the block does execute, regardless of the original value of $month. You have to change if($month = $i) to if($month == $i) in order to make it work the way it's supposed to work.

That said, the (fixed) code

for($i=0; $i<12; $i++){
if($month == $i){
$ordersPerMonth[$i] = $ordersPerMonth[$i] + 1;
}
}
is quite inefficient.

Instead of checking all the months as to whether they coincide with the month the order was placed, and if so, increasing the appropriate order counter, you should just increase the appropriate order counter, period.

This code

$ordersPerMonth[$month]++;
does the exact same thing as the snippet above, only faster.

sea4me
05-08-2009, 11:11 PM
lol I found out I used it in as var and a array now :)

I didn't realize until you said that I didn't understand :eek:

THANKS A LOT! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum