|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Major problems on looping through an array
Hi all currently i am looping through sql and setting the findings into an array and then later using a foreach loop to output what it has.
It nearly works but a major problem is that it seems to double up sometimes with the wrong info? This is how i set the array: PHP Code:
Then i loop through it later on using this: PHP Code:
I need to do it like this as i get other information and echo out before the loop is done like so: (this sets the title in the header of the document from the array gathered ealier) PHP Code:
I know i am probably making some stupid mistake? You can see how annoying the error is here: http://www.prxa.info/forum/viewtopic.php?tid=97 (as you can see there it shows the first one fine but then makes me have a reply for each other persons reply lol). |
|
#2
|
|||
|
|||
|
Most database fetch calls (e.g. mysql_fetch_array) allow you to define how you want the data returned.
An optional result_type parameter allows you to identify how you want to specify the keys in your returned result. In the case of MySQL, MYSQL_ASSOC will return each column from the record, in an array indexed by the column name; MYSQL_NUM will return each column from the record, in an array indexed by a numeric value (the order of the column in the result set; MYSQL_BOTH will return two entries for each column in the record, one entry in the array indexed by the column name, the second entry by the numeric value. The default is MYSQL_BOTH
__________________
9 out of 10 PHP problems can be resolved by setting PHP Code:
Remember, the command line is your friend Development Projects: PHPExcel PHPPowerPoint |
|
#3
|
||||
|
||||
|
I have figured out that i don't think that is the problem.
I have actually found out the problem does lie in the array set the the sql, it seems to add the extra posts into it there so i don't think the problem actually is the foreach loop. Look at this post for example: http://www.prxa.info/area51/viewtopic.php?tid=1 Number 3 in the array shouldn't be there. And so it is echod out in the foreach loop, which is right since it is in the array, but the array is wrong. So is it a problem with the way i grab it with the sql? |
|
#4
|
||||
|
||||
|
It depends..
1. what does your sql look like? 2. are you sure $db->fetch_array is not buggy?
__________________
[read: PHP Security Guide | CSRF | PCRE Modifiers | Encryption | Form Processing | File Validation] [tools: PHPEd | PHP Docs | jQuery | CodeIgniter | Drupal | SwiftMailer | CKEditor | reCAPTCHA] |
|
#5
|
||||
|
||||
|
If you're doing a JOIN incorrectly in your SQL query this could easily happen. My guess is that's where the problem lies.
|
|
#6
|
||||
|
||||
|
E-Oreo was correctly it was a wrong join query, thanks all.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > PHP Development > Major problems on looping through an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|