Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts

    Cool Arrays not working properly??

    Hi!

    I have this:
    PHP Code:
    $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

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

  • #2
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi sea4me,

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


    PHP Code:
    $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?


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

    Last edited by fullmontez; 05-05-2009 at 05:41 AM. Reason: putting PHP brackets around code

  • #3
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    @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.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Well first of all you can't use $month for both a string and an array at the same time --
    PHP Code:
    $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
    PHP Code:
    if($month $i
    , and the last time that happens, $i equals 11, so no surprise here. You really want to use
    PHP Code:
    if($month == $i
    instead.

  • #5
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    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 ....

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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
    PHP 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
    PHP Code:
    $ordersPerMonth[$month]++; 
    does the exact same thing as the snippet above, only faster.
    Last edited by venegal; 05-07-2009 at 02:15 PM.

  • Users who have thanked venegal for this post:

    sea4me (05-09-2009)

  • #7
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    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

    THANKS A LOT!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •