473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Array help - best practice

348 100+
I'd be interested in someone looking over this foreach loop to see if there is a better way to do this. These loops gives me exactly what I need but I can't help but think it isn't the best practice or even the right way of doing it.

The $reports variable is an array that holds data from my db. An example of what may be in the array is:

Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jane    Williams    1967-07-11
  3. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jack    Williams    1969-04-19
  4. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Bob    Savana    1959-09-15
  5.  
Hopefully, you can see that I have 4 rows where the first 8 columns are all exactly the same with the exception of the person's name and date of birth. What I need is to use this array to build an html report. In my code sample below, the outer loop gets only the first row to build the first page then the inner loop iterates over the people's names and date of birth

Can someone please tell me if there is a better way of doing this?

Expand|Select|Wrap|Line Numbers
  1.   $oldarr = $reports;
  2.   $newarr = array_splice($reports,0,1);
  3.   foreach($newarr as $val)
  4.   {
  5.     // my first 8 columns will go here
  6.     if($val['rpt_type'] == 'Report12')
  7.     {
  8.       foreach($oldarr as $rpt)
  9.       {
  10.         echo $rpt['fname'],$rpt['lname'],$rpt['dob'] . '<br>';
  11.       }
  12.     }
  13.   }
  14. }
  15.  
May 4 '09 #1
3 1805
Ciary
247 Expert 100+
I'm not sure what you want, but if i understand you, you want something like this:
Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2.  
  3. Wilma    Ventura    1970-06-04
  4. Jane    Williams    1967-07-11
  5. Jack    Williams    1969-04-19
  6. Bob    Savana    1959-09-15
if thats the case, i dont really see why you use array_splice. other then that, i think you're code is the best way to do it. i don't really see another.
May 4 '09 #2
fjm
348 100+
@Ciary
Hi Ciary,

You're right, array_splice is not right. It is doing exactly what it should do but it's not right for what I need.

Let me try and explain again. Please look at the sample data below. In the example data below, there is only one actual report with 4 people associated. The 4 people associated with the report are what are causing all 4 rows to echo.

Each report has its own report number as the primary key in the database. If I could just loop off of that, I would be ok. Can someone please advise?


Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jane    Williams    1967-07-11
  3. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jack    Williams    1969-04-19
  4. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Bob    Savana    1959-09-15
May 4 '09 #3
Ciary
247 Expert 100+
ok, but where is that different to what i suggested? i know another way though, with only one foreach.

Expand|Select|Wrap|Line Numbers
  1. $newest = "";
  2. foreach($newarr as $val){
  3.    if($val[0] != $newest){
  4.       $newest = $val[0];
  5.       echo $val[0]." ".$val[1]." ".$val[2]." ".$val[3]." ".$val[4]." ".$val[5]." ".$val[6]." ".$val[7].'<br><br>';
  6.    }
  7.    echo $val['fname'],$val['lname'],$val['dob'] . '<br>';
  8. }
  9.  
this will first show the 8 first Columns. then it will show a list with your users.
May 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: Cory Toms | last post by:
Hey All, I have question about the best way to go about doint this: SqlDataReader _dr=components.getItems(); fooclass _myarray = new fooclass; //create new array of my class int i=0; ...
3
by: Rich | last post by:
The procedure below checks if a character entered into a cell of a datagridview is contained in a string array of valid characters for this particular cell. It seems kludgy. I am asking what the...
17
by: AMP | last post by:
Hello I get 2 errors in the following code that I cant Debug without some help. They are both in the TryParse function. Seems simple enough.The Errors are Below. private void...
1
by: trevor.farchild | last post by:
Hi, long time reader, first time poster I have an application that will be doing this 5 times a second: Get a bunch of data from a NetworkStream and convert it into a Bitmap Therefore, the...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
6
by: bill | last post by:
I am about to start on a module that will accept a location from a user, use Google geolocation services to get the lat/lon and then compute the distance from the site visitor to about 100 kennels...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.