| | |
2 dimensional array issue
![]() |
•
•
Join Date: Sep 2008
Posts: 33
Reputation:
Solved Threads: 1
So here's the code:
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?
php Syntax (Toggle Plain Text)
function procRestSearch($searchQuery){ //Don't forget to have $searchquery defined before calling this function $intSearchCounter = 0; //Used to set the arrays $searchResult = mysql_query($searchQuery); while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){ //Change/Add/Delete the variables below to match what data needs to be returned $strRest = array($intSearchCounter => array('restID' => $searchRow['rest_id'], 'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'], 'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'], 'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'], 'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'], 'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'], 'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'], 'restWebsite' => $searchRow['rest_website'])); $intSearchCounter++; } return $strRest; }
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?
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.
Meaning, show all errors except notices.
Check this link to configure error reporting in your script.
Cheers,
Naveen
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
Check this link to configure error reporting in your script.
Cheers,
Naveen
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Sep 2008
Posts: 33
Reputation:
Solved Threads: 1
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.
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.
0
#4 Nov 20th, 2009
What I meant was ,
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 ?
PHP Syntax (Toggle Plain Text)
function procRestSearch($searchQuery){ //Don't forget to have $searchquery defined before calling this function $intSearchCounter = 0; //Used to set the arrays $searchResult = mysql_query($searchQuery); $strRest = array(); while($searchRow = mysql_fetch_array($searchResult, MYSQL_ASSOC)){ //Change/Add/Delete the variables below to match what data needs to be returned $strRest[$intSearchCounter] = array('restID' => $searchRow['rest_id'], 'restName' => $searchRow['rest_name'], 'restAddress' => $searchRow['rest_address'], 'restAddress2' => $searchRow['rest_address2'], 'restCity' => $searchRow['rest_city'], 'restState' => $searchRow['rest_state'], 'restZip' => $searchRow['rest_zip'], 'restCC' => $searchRow['rest_country_code'], 'restAC' => $searchRow['rest_area_code'], 'restPhone' => $searchRow['rest_phone_no'], 'restFAC' => $searchRow['rest_fax_no'], 'restManager' => $searchRow['rest_manager'], 'restAsManager' => $searchRow['rest_assistant_manager'], 'restWebsite' => $searchRow['rest_website']); $intSearchCounter++; } return $strRest; }
Does it work for you ?
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Similar Threads
- Need Help With two-dimensional array (VB.NET)
- two dimensional array (C)
- Validation with js of two dimensional array (JavaScript / DHTML / AJAX)
- 2 dimensional array class (C)
- multi dimensional array search xml parser (PHP)
- Need help passing a multi-dimensional array (C++)
- Trying to create a method to convert string letters into a two dimensional array (Java)
Other Threads in the PHP Forum
- Previous Thread: Converting PHP Feed to XML Feed?
- Next Thread: Web/C++/PHP File Updates
Views: 947 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for array
2d access add administration api append array arrays assembly box c# c++ char checkbox class colour control conversion convert coordinates corrupted counttheoccurenceofanintegerinthe10inputs data database delete dropdown dynamic exception fdlib file forloop frequency fstream function functions game graph image import index input inputs int integer intersection java javascript jsp limit list listbox lookup loop macro malloc matching memory menu multi multidimensional mysql number object objects occurence offset opengl output overwrite parameter pascal perl php pointer pointers problem programs raid read recursion return search select shuffle sort string strtok table text transfer type upload user values variable vb vb.net vbnet virtualization visualstudio2008







Cheers! 