0

I am having trouble trying to figure out how to populate a multi-dimensional array. Let's say I have a table of transactions with various billing dates. First I have an array that retrieves the following 'billed' dates:

Array
(
    [0] => Array
        (
            [BILLED] => 2011-11-18 00:00:00
        )

    [1] => Array
        (
            [BILLED] => 2011-11-22 00:00:00
        )

)

I also have the following query which is currently hard-coded to one of the two 'billed' dates shown above:

$qryOrders = $this->db->query("
       SELECT tblOrders.* 
       FROM tblOrders 
       WHERE tbltc.BILLED = '2011-11-22'"); 
$data['Orders'] = $qryOrders->result_array();

I know that I can very easily determine the count of array items by using count($Orders); but how can I instead pass through each of the 'billed' dates of 2011-11-18 and 2011-11-22 to that I can determine the overall count, for both of the specified dates?

I hope I've explained it clearly enough. I am thinking that I probably need some kind of foreach loop and each time through the loop I could pass in the billed date, and keep track of a running total each time through the loop.

4
  • 1
    Is this a question about arrays or about SQL? Commented Nov 25, 2011 at 5:22
  • I'm not sure actually. Should I have mentioned that I am using CodeIgniter framework? Commented Nov 25, 2011 at 5:25
  • In other words, you want a count of orders received between two dates? MySQL's COUNT and BETWEEN statements will help. Commented Nov 25, 2011 at 5:28
  • Thanks Matt. Actually, that's not what I want. I don't want a count of items BETWEEN two dates. I want a count of items on each of the series of dates. Sometimes 2 or 3 or 4 or 5 etc. I must need to iterate through to get my dates, and then run COUNT foreach of the dates. Commented Nov 25, 2011 at 5:36

1 Answer 1

1
// this is getting all results for a record
$sql = 
<<<sql
SELECT tblOrders.* 
FROM tblOrders 
WHERE tbltc.BILLED between '{$arr[0]}' and '{$arr[1]}'
sql;

// this is to get total count of matched record
// $sql = 'select count(*) from ..';

if you are using CI, you can easily using bind

example:-

$sql = 'SELECT tblOrders.* FROM tblOrders WHERE tbltc.BILLED between ? AND ?';
$this->db->query($sql, array($arr[0], $arr[1]));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer. I'm not sure what you mean about using bind?
CI manual has the example, please refer to the updated answer and codeigniter.com/user_guide/database/queries.html (look for query binding)
I should also add that sometimes I have 2 elements in the date array, sometimes 3 or 4 or 5. This varies, thus my thinking a loop would be needed.
this is something I can't help, you have to understand how the BETWEEN works (start, end). you can do iterate to find the minimum and maximum then bind
I've read-up on how the BETWEEN works, and it's not what I am looking for. I need to find an example that will iterate through my records and use the date value each time. thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.