Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/205635-multidminsional-array-issue/
Share on other sites

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.

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 />';
}

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

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.

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>

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 />";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.