开发者

php loops and grouping mysql

开发者 https://www.devze.com 2023-01-07 05:57 出处:网络
I am currently adding a purchase history section to payment system, the system is built in PHP (codeigniter framework) and mysql. The idea of the payment history section is that all the customer purch

I am currently adding a purchase history section to payment system, the system is built in PHP (codeigniter framework) and mysql. The idea of the payment history section is that all the customer purchases are grouped together by date, so in the end the HTML would look similar to this,

My question is I can get the data out of the database no problem, but how can i loop through it so that I show the date of the purchases and then purchases relevent to that date and so on and so forth.

EDIT - A PRINT OUT OF THE ARRAY THAT IS RETURNED FROM THE DB SORRY FOR THE SIZE

    Array
(
    [0] => Array
        (
            [id] => 
            [employer_id] => 1
            [cv_id] => 
            [transaction_id] => -1402310
            [date_created] => 20-07-10
            [date_updated] => 
            [code] => 849711492
            [status] => SHOW
            [is_complete] => TRUE
            [job_status] => 1
            [dob] => 
            [will_relocate] => Y
            [education_level] => HIGH_SCHOOL
            [job_looking_for] => 
            [keywords_education] => Halifax University
            [keywords_employment] => senior applications developer
            [self_description] => 
            [looking_for] => Lorem ipsum
            [sector_id_csv] => 10,23,14,6,3
            [key_skills_csv] => 
            [expected_salary_level] => LEVEL_6
            [expected_salary_low] => 0
            [expected_salary_high] => 0
            [best_salary_low] => 0
            [best_salary_high] => 0
            [last_job_salary_low] => 
            [last_job_salary_high] => 
            [last_job_title_1] => Designer
            [last_job_title_2] => 
            [last_job_employer_1] => BT
            [last_job_employer_2] => 
            [qual_highest_level] => 0
            [is_grad] => Y
            [is_contract] => N
            [is_temporary] => Y
            [is_permanent] => Y
            [is_full_time] => Y
            [is_part_time] => Y
            [logitude] => 
            [latitude] => 
            [city] => 
            [area] => 
            [sign_up_stages_completed] => 
            [sign_up_last_stage_completed] => 
            [sign_up_complete] => 
            [email_authenticated] => 
            [email] => 
            [email_contact] => 
            [phone_contact_number] => 
            [password] => 
            [firstname] => 
            [surname] => 
            [address_1] => 
            [address_2] => 
            [address_3] => 
            [postcode] => 
            [country] => 
        )

    [1] => Array
        (
            [id] => 
            [employer_id] => 1
            [cv_id] => 
            [transaction_id] => -1402310
            [date_created] => 20-07-10
            [date_updated] => 
            [code] => 849719383
            [status] => SHOW
            [is_complete] => TRUE
            [job_status] => 1
            [dob] => 
            [will_relocate] => Y
            [education_level] => HIGH_SCHOOL
            [job_looking_for] => 
            [keywords_education] => Crewe Art School
            [keywords_employment] => senior applications developer
            [self_description] => 
            [looking_for] => Lorem ipsum
            [sector_id_csv] => 13,24,7,17,5
            [key_skills_csv] => 
            [expected_salary_level] => LEVEL_6
            [expected_salary_low] => 0
            [expected_salary_high] => 0
            [best_salary_low] => 0
            [best_salary_high] => 0
            [last_job_salary_low] => 
            [last_job_salary_high] => 
            [last_job_title_1] => Developer
            [last_job_title_2] => 
            [last_job_employer_1] => Orange
            [last_job_employer_2] => 
            [qual_highest_level] => 0
            [is_grad] => Y
            [is_contract] => N
            [is_temporary] => Y
            [is_permanent] => Y
            [is_full_time] => Y
            [is_part_time] => N
            [logitude] => 
            [latitude] => 
            [city] => 
            [area] => 
            [sign_up_stages_completed] => 
            [sign_up_last_stage_completed] => 
            [sign_up_complete] => 
            [email_authenticated] => 
            [email] => 
            [email_contact] => 
            [phone_contact_number] => 
            [password] => 
            [firstname] => 
            [surname] => 
            [address_1] => 
            [address_2] => 
            [address_3] => 
            [postcode] => 
            [country] => 
        )

    [2] => Array
        (
            [id] => 
            [employer_id] => 1
            [cv_id] => 
            [transaction_id] => 23-9-1402338
            [date_created] => 17-07-10
            [date_updated] => 
            [code] => 849711492
            [status] => SHOW
            [is_complete] => TRUE
            [job_status] => 1
            [dob] => 
            [will_relocate] => Y
            [education_level] => HIGH_SCHOOL
            [job_looking_for] => 
            [keywords_education] => Halifax University
            [keywords_employment] => senior applications developer
            [self_description] => 
            [looking_for] => Lorem ipsum
            [sector_id_csv] => 10,23,14,6,3
            [key_skills_csv] => 
            [expected_salary_level] => LEVEL_6
            [expected_salary_low开发者_开发技巧] => 0
            [expected_salary_high] => 0
            [best_salary_low] => 0
            [best_salary_high] => 0
            [last_job_salary_low] => 
            [last_job_salary_high] => 
            [last_job_title_1] => Designer
            [last_job_title_2] => 
            [last_job_employer_1] => BT
            [last_job_employer_2] => 
            [qual_highest_level] => 0
            [is_grad] => Y
            [is_contract] => N
            [is_temporary] => Y
            [is_permanent] => Y
            [is_full_time] => Y
            [is_part_time] => Y
            [logitude] => 
            [latitude] => 
            [city] => 
            [area] => 
            [sign_up_stages_completed] => 
            [sign_up_last_stage_completed] => 
            [sign_up_complete] => 
            [email_authenticated] => 
            [email] => 
            [email_contact] => 
            [phone_contact_number] => 
            [password] => 
            [firstname] => 
            [surname] => 
            [address_1] => 
            [address_2] => 
            [address_3] => 
            [postcode] => 
            [country] => 
        )

    [3] => Array
        (
            [id] => 
            [employer_id] => 1
            [cv_id] => 
            [transaction_id] => 23-9-1402338
            [date_created] => 17-07-10
            [date_updated] => 
            [code] => 849719383
            [status] => SHOW
            [is_complete] => TRUE
            [job_status] => 1
            [dob] => 
            [will_relocate] => Y
            [education_level] => HIGH_SCHOOL
            [job_looking_for] => 
            [keywords_education] => Crewe Art School
            [keywords_employment] => senior applications developer
            [self_description] => 
            [looking_for] => Lorem ipsum
            [sector_id_csv] => 13,24,7,17,5
            [key_skills_csv] => 
            [expected_salary_level] => LEVEL_6
            [expected_salary_low] => 0
            [expected_salary_high] => 0
            [best_salary_low] => 0
            [best_salary_high] => 0
            [last_job_salary_low] => 
            [last_job_salary_high] => 
            [last_job_title_1] => Developer
            [last_job_title_2] => 
            [last_job_employer_1] => Orange
            [last_job_employer_2] => 
            [qual_highest_level] => 0
            [is_grad] => Y
            [is_contract] => N
            [is_temporary] => Y
            [is_permanent] => Y
            [is_full_time] => Y
            [is_part_time] => N
            [logitude] => 
            [latitude] => 
            [city] => 
            [area] => 
            [sign_up_stages_completed] => 
            [sign_up_last_stage_completed] => 
            [sign_up_complete] => 
            [email_authenticated] => 
            [email] => 
            [email_contact] => 
            [phone_contact_number] => 
            [password] => 
            [firstname] => 
            [surname] => 
            [address_1] => 
            [address_2] => 
            [address_3] => 
            [postcode] => 
            [country] => 
        )

)

Basically Every Time there is a new data I need to list the contents of the array under it, and if two arrays have the same data list them just under one data something like,

30-07-10

Result 1

2

3

28-7-10

4

5


With CodeIgniter, one way to loop through query results is like so:

//if your doing your queries in a controller
$data['query']=$this->db->query('SELECT * FROM purchases WHERE clientID=5');

//or if using models
$data['query']=$this->pmnt_model->getPmnts();

//view
foreach($query result() as $row){
    echo $row->date;
}

the $query variable is the result array returned from the mysql query. The objects can be accessed via the -> operator.

See http://codeigniter.com/user_guide/database/results.html for more info.

You also might want to check out the active record class.

HTH


There are two possibilities that i can see see to accomplish what you need. Here is one; send two arrays to your view, one containing just the dates and the other containing the purchase information:

In the controller

$page_data['purchase_dates'] = $this->yourmodel->get_purchase_dates($record_id);
$page_data['purchases'] = $this->yourmodel->get_purchases($record_id);

Note Make sure you use GROUP BY in your database query for the purchase dates.

In the View

<!-- Start Outer Tree Loop -->
<?php if($purchase_dates != FALSE){?>
<?php foreach($purchase_dates as $id=>$outer_value) { ?>
<!-- Output the date here -->
  <!-- Start Purchase table -->
  <table>
  <tr>
  <th>Param One</th>
  <th>Param Two</th>
  <th>Etcetera</th>
  <th></th>
  </tr>

  <!-- Start inner loop -->
  <?php foreach($purchases as $id=>$inner_value) { ?>
   <?php if($inner_value['purchase_date'] == $outer_value['purchase_date']) { ?>
    <tr>
    <td><?php echo $inner_value['Param One'] ?> </td>
    <td><?php echo $inner_value['Param Two'] ?></td>
    <td><?php echo $inner_value['Etcetera'] ?> </td>
   </tr>
   <?php  } ?>
  <?php  } ?>
  <!-- End inner loop -->

  </table>
  <!-- End Purchase table -->

<?php } ?>
<?php }?>
<!-- End Outer Tree Loop -->

I have added comments to illustrate the code (this may be the best option for you as the array you posted is two dimensional).

The second: is to send a three dimensional array to the view and loop through it in a very similar fashion as above.

<?php if($purchases != FALSE){?>
<?php foreach($purchases as $id=>$purchase_day) { ?>

<?php foreach($purchase_day as $purchase) { ?>

<!-- Output here -->

<?php } ?>

<?php } ?>
<?php }?>

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号