Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

an array/loop problem 2

Status
Not open for further replies.

jasc2k

Programmer
Nov 2, 2005
113
GB
Hi there,
It has been a while since I have looked at loops/arrays but this has me stumped. I am using the following code in a couple of other locations and all is fine BUT this time I know there will only be one result from the SQL query this seems to be the issue. When I return multiple results to the while loop it works fine.

Code:
$result = $database->query("SELECT * FROM ".TBL_MAIL_SENT." WHERE UserFrom = '$session->username' AND mail_id = '$msgid'");
			$mail = mysql_fetch_array($result);
			$user = $mail['UserFrom'];
		
			$mail_ary = array();
			while($data = mysql_fetch_object($result)){
				$mail_ary[] = array('UserFrom' => $data->UserFrom, 'UserTo' => $data->UserTo , 'Subject' => $data->Subject, 'Message' => $data->Message, 'mail_id' => $data->mail_id, 'SentDate' => $data->SentDate);

Even though there is only one result I still need the array populated in the same format/layout.

Any help?
Thanks
 
the mysql_fetch_* commands move the recordset pointer forward each time. so if there is only one row the first call (to mysql_fetch_array()) will advance the pointer to the end of the recordset.

I recommend this instead:
Code:
$result = $database->query("SELECT * FROM ".TBL_MAIL_SENT." WHERE UserFrom = '$session->username' AND mail_id = '$msgid'");
$mail_ary = mysql_fetch_assoc($result));
$user =& $mail_ary['UserFrom'];
 
hi there,
thanks for your reply, though your suggestion works I need the array with headers:
Code:
$mail_ary[] = array('UserFrom' => $data->UserFrom, 'UserTo' => $data->UserTo , 'Subject' => $data->Subject, 'Message' => $data->Message, 'mail_id' => $data->mail_id, 'SentDate' => $data->SentDate);
becuase of the way my table displays the data i.e
Code:
if (count($mail_ary)){
				echo "<table align='left' border='2' cellspacing='0' cellpadding='0'>";
				foreach ($mail_ary as $key => $mail_itm){
}

any ideas?
thanks again
 
In that case, use mysql_data_seek and return the result pointer to the beginning, so your mysql_fetch_object call can get the row again.

Code:
mysql_data_seek($result,0) or die("Cannot reset pointer" . mysql_error());

By the way, if there's only one result you don't need the while loop.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
using my method you get the array with the headers. no need to reset the pointer. that's the whole ...point... of mysql_fetch_assoc

Code:
$result = $database->query("SELECT * FROM ".TBL_MAIL_SENT." WHERE UserFrom = '$session->username' AND mail_id = '$msgid'");
$mail_ary = mysql_fetch_assoc($result));
$user =& $mail_ary['UserFrom'];
echo '<table>';
echo '<tr><th>'.implode('</th><th>', array_keys($mail_ary) ). '</th></tr>';
foreach ($mail_ary as $row):
 $row = array_map($row, 'htmlspecialchars');
 echo '<tr><td>'.implode(</td><td>', $row) . '</td></tr>';
endforeach;
echo '</table>';

the above prints the table going across.

if you want a table going down then let us know.

 
Superb thanks for your posts guys.
good point about the while loop hadnt spotted that but in the end went with jpadie's fix.
Just a couple tweaks and the code works great.
In the right order too ;o)

Thanks again guys
Was pullin my hair out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top