samtwilliams Posted June 23, 2010 Share Posted June 23, 2010 Afternoon All, I am trying to store the results of a sql query to a multidimensional array with a key. I have this so far but i am getting an offset warning when returning the results via a loop; Make the array: while ($ptdbRow = mssql_fetch_assoc($ptdbResult)){ $ptdbResultsArray = array(array($ptdbRow['patientid'], $ptdbRow['forename'], $ptdbRow['surname'], $ptdbRow['dob'])); } Results: for ( $row = 0; $row < 4; $row++ ) { for ( $column = 0; $column < 4; $column++ ) { echo '|'.$ptdbResultsArray[$row][$column]; } echo '|<br />'; } Any help appreciated. Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/ Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 Your are referencing the wrong numbers... and the explanation I gave was wrong sorry... give me a few to fix it. EDIT: OKay here's the problem you are trying to call the following: $ptdbResultsArray[0][0] .... $ptdbResultsArray[0][1]... etc.... You've no arrays with the keys of the second inner array being a number.... Your array looks likes it something like this... $ptdbResultsArray[0]["Tom"] for example. Your column keys are strings not numbers most likely. Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076073 Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 Not exactly what you want I believe but maybe a start this should print the following paitentid|forename|surname|dob| for row number 0 through row number 3 for ( $row = 0; $row < 4; $row++ ) { foreach($ptdbResultsArray[$row] as $key => $value) { echo '|'.$key; } echo '|<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076080 Share on other sites More sharing options...
trq Posted June 23, 2010 Share Posted June 23, 2010 Your over complicating things. $ptdbResultsArray = array(); while ($ptdbRow = mssql_fetch_assoc($ptdbResult)) { $ptdbResultsArray[] = $ptdbRow; } foreach ($ptdbResultsArray as $row) { echo implode('|', $row) . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076081 Share on other sites More sharing options...
samtwilliams Posted June 23, 2010 Author Share Posted June 23, 2010 Thanks thorpe/gwolgamott, but i need to add a key to the array for a link and also display the results in a table. I can't seem to work out how to add a key to yoru solution thorpe and also table the results, although i do like the method as it is far cleaner than what i have been using. Any ideas? Sam Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076109 Share on other sites More sharing options...
wildteen88 Posted June 23, 2010 Share Posted June 23, 2010 Thorpes code does what you're wanting to do $ptdbResultsArray will be multidimensional array. To get the key from the foreach loop use the following format: foreach ($ptdbResultsArray as $key => $row) { Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076130 Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 I think for some reason he wants to have the keys be a string value? Is that correct? Although using thorpe method you really don't need it... just look for the appropriate array element at the appropriate level is all I believe. Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076134 Share on other sites More sharing options...
samtwilliams Posted June 23, 2010 Author Share Posted June 23, 2010 yes thast right i do, also i cant work out how to add the results to a table, i need to add a random code (rand()) to each row before its entered to the array as well. I have so far for the output; <table> <?PHP foreach ($ptdbResultsArray as $row) { echo '<tr>'; echo implode('<td>', $row) . "</td>"; echo '</tr>'; } ?> </table> But i still need to add a random code to each row before its in the array. Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076137 Share on other sites More sharing options...
wildteen88 Posted June 23, 2010 Share Posted June 23, 2010 What do you mean by random code. Are you wanting to display the results in a random order? Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076142 Share on other sites More sharing options...
samtwilliams Posted June 23, 2010 Author Share Posted June 23, 2010 Sorry, may have confused everyone, I have my sql query which gets entered into an array, but i have some php that is generating an md5 hash which i need to add to each row with the sql query before its entered into an array. So an example of my output would be; key code patientid patientforename patientsurname dob 0 23ur2384388fg84f834 NHS938383 Sam Williams 16/09/2010 I nearly have the table finished as well except i cant work out why the patient ID isn't getting included in the table, instead its just dumped on top; <table> <tr> <td width="16%"><strong>Array Key</strong></td> <td width="16%"><strong>Patient ID</strong></td> <td width="23%"><strong>Forename</strong></td> <td width="23%"><strong>Surname</strong></td> <td width="17%"><strong>DOB</strong></td> <td width="21%"><div align="center"><strong>Allocation</strong></div></td> </tr> <?PHP foreach ($ptdbResultsArray as $key => $row) { echo '<tr>'; echo '<td>'.$key.'</td>'; echo implode('<td>', $row) . "</td>"; echo '</tr>'; echo $key; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076146 Share on other sites More sharing options...
wildteen88 Posted June 23, 2010 Share Posted June 23, 2010 Where is this md5 hash (code) generated. You can insert the hash for each row in this loop $ptdbResultsArray = array(); while ($ptdbRow = mssql_fetch_assoc($ptdbResult)) { $ptdbResultsArray[] = array_push($ptdbRow, $YOUR_MD5_HASH_HERE); } Or assign the hash as the key for each row $ptdbResultsArray[$YOUR_MD5_HASH_HERE] = $ptdbRow; To grab the hash from the key use foreach ($ptdbResultsArray as $hash => $row) { echo $hash . ' - ' . implode('|', $row) . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076152 Share on other sites More sharing options...
samtwilliams Posted June 23, 2010 Author Share Posted June 23, 2010 now im recieving this error: Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\index.php on line 123 I cant get my results into a table. Sam Quote Link to comment https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/#findComment-1076166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.