2 dimensional array issue

Reply

Join Date: Sep 2008
Posts: 33
Reputation: loligator is an unknown quantity at this point 
Solved Threads: 1
loligator loligator is offline Offline
Light Poster

2 dimensional array issue

 
0
  #1
Nov 20th, 2009
So here's the code:

  1. function procRestSearch($searchQuery){
  2. //Don't forget to have $searchquery defined before calling this function
  3. $intSearchCounter = 0; //Used to set the arrays
  4. $searchResult = mysql_query($searchQuery);
  5. while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){
  6. //Change/Add/Delete the variables below to match what data needs to be returned
  7. $strRest = array($intSearchCounter => array('restID' => $searchRow['rest_id'],
  8. 'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'],
  9. 'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'],
  10. 'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'],
  11. 'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'],
  12. 'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'],
  13. 'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'],
  14. 'restWebsite' => $searchRow['rest_website']));
  15.  
  16. $intSearchCounter++;
  17. }
  18. return $strRest;
  19. }

the problem is when I call this function I get a Notice: Undefined offset: 0 in... error. However, if I replace $intSearchCounter with a static value, such as just replacing it with 0, it works but then I only get the last result from the query returned. What am I doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
0
  #2
Nov 20th, 2009
Initialize the array outside the while loop. The array is getting over-written everytime it enters the loop. So you are getting only the last record.
Secondly, notices can be ignored. If you use variables without initializing them, you will get a notice. It is a good programming practice to initialize all your variables before using them. But sometimes, it gets a little annoying. You can disable notices by changing your php.ini file.
Search for error_reporting and uncomment this line.
error_reporting = E_ALL & ~E_NOTICE
Meaning, show all errors except notices.
Check this link to configure error reporting in your script.

Cheers,
Naveen
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: loligator is an unknown quantity at this point 
Solved Threads: 1
loligator loligator is offline Offline
Light Poster
 
0
  #3
Nov 20th, 2009
No good. I can't initialize the array outside of the while loop for none of the values will be set unless there's another way to initialize an array in php...

and i want the errors displayed so i know what's going on with my code. if i try to set the first key in the array with the variable $intSearchCounter, which increments by 1 after each run through of the loop, that's when i get the error and NO RESULTS RETURNED, just the error. Testing where my code went wrong, I replaced $intSearchCounter with 0 just to see what would happen and it only gave me the last result in the query which i fully expected. I just wanted to see where the code went wrong. Basically, it appears as if the array is ignoring $intSearchCounter but I don't know why this would happen.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
0
  #4
Nov 20th, 2009
What I meant was ,
  1. function procRestSearch($searchQuery){
  2. //Don't forget to have $searchquery defined before calling this function
  3. $intSearchCounter = 0; //Used to set the arrays
  4. $searchResult = mysql_query($searchQuery);
  5. $strRest = array();
  6. while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){
  7. //Change/Add/Delete the variables below to match what data needs to be returned
  8. $strRest[$intSearchCounter] = array('restID' => $searchRow['rest_id'],
  9. 'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'],
  10. 'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'],
  11. 'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'],
  12. 'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'],
  13. 'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'],
  14. 'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'],
  15. 'restWebsite' => $searchRow['rest_website']);
  16. $intSearchCounter++;
  17. }
  18. return $strRest;
  19. }
And secondly, using the above error reporting would still display the errors but not the notices. As I said in my previous post, if you like to fix the notices, initialize your variables, use $var['something'] instead of $var[something] and so on. (notices can be ignored).
Does it work for you ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: loligator is an unknown quantity at this point 
Solved Threads: 1
loligator loligator is offline Offline
Light Poster
 
0
  #5
Nov 23rd, 2009
You are the man. I just didn't understand what you mean by initializing the array in the original reply but seeing it makes perfect sense. Thanks so much.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster
 
0
  #6
Nov 23rd, 2009
Ah! All's well. Cheers!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

Tags
array

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 947 | Replies: 5
Thread Tools Search this Thread



Tag cloud for array
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC