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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to cycle through items in table w/PHP

Status
Not open for further replies.

natwod

Programmer
Nov 21, 2002
41
US
I posted this in the PHP forum, and nobody seemed to know, so I am trying here.

I have a question about displaying database info using PHP. I am trying to display data from a database one entry at a time, but I am not able to get it to move on to the next Row. I’m not sure, but there may be a problem with my While statement as well.

This is a sample of what I have currently. I have already set up a database connection with the variable $connectionRS. The output for this code is only one row; I want it to display all of the rows (entries) in my database file.

<table>
<?php

Do {
$itemLastName = odbc_result($connectionRS, &quot;LastName&quot;);
$itemFirstName = odbc_result($connectionRS, &quot;FirstName&quot;);

echo &quot;<tr> <td>{$itemLastName}</td>&quot;;
echo &quot;<td>{$itemFirstName}</td></tr>&quot;;

} While (odbc_next_result($connectionRS));

?>
</table>
Note: I also tried copying the odbc_next_result…. statement up inside the Do braces to see if that would “move the playhead” down to the next row; didn’t work.

Thanks a ton for any comments!

Natwad
 
Natwad,
Try this and see how it does.

================================================
#this sets up your query and defines result
$result = odbc_do ([ODBC CONNECTION], [QUERY]) or die (&quot;Invalid Query&quot;);

#This creates a function to fetch your results
function __odbc_fetch_object($result)
{
if( function_exists(&quot;odbc_fetch_object&quot;) )
return odbc_fetch_object($result);

$rs = array();
$rs_obj = false;
if( odbc_fetch_into($result, &$rs) )
{
foreach( $rs as $key=>$value )
{
$fkey = odbc_field_name($result, $key+1);
$rs_obj->$fkey = trim($value);
}
}
return $rs_obj;
}

<table>
<tr>

#This would be your while statment to return your results
while ($row = __odbc_fetch_object($result))
{

#Close your php session to use HTML
?>
<td>
#Open php session to return results
<?
#Use [LastName as the row, as it appears in your query]
echo &quot;$row->LastName&quot;;
#close your php session and back to html
?></td>
<td>
# open your php session again
<?
#Use [FirstName as the row, as it appears in your query]
echo &quot;$row->FirstName&quot;;
#close your php session and back to html
?></td></tr>
#This opens php session and closes while statement
<?
}
?>
</tr>
</table>
============================================================
This is how I used it in my own odbc query into a database. Let me know if you have any luck.

Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top