On 05/31/2014 10:16 AM, ICCSI wrote:
I have following code to get data to show data and fill my select list. The following code works either to fill list or show data in the table, but not both. It looks like the data show on the table then it reaches the end of data, so the select list does not have anything to fill. I might be wrong. If I am right, how can I move the data to BOF, or if I am wrong then is it possible to have it reuse the data source second time on the form. Your help and information is great appreciated, Regards, Iccsi, <?php $hostname = 'localhost'; $username = 'root'; $password = 'password'; try { $dbh = new PDO("mysql:host=$hostname;dbname="MyDB", $username, $password); echo 'Connected to database'; } catch(PDOException $e) { echo $e->getMessage(); } $stmt = $dbh->query('SELECT MyField from Mytable'); while($data = $stmt->fetch(PDO::FETCH_ASSOC)){ echo "<tr><td>".$data{'MyField'}."</td></tr>"; } ?> <select name='mylist' id='mylist' size='1'> <?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "<option>".$row['MyField']."</option>"; } ?> </select>
Try this: <?php $hostname = 'localhost'; $username = 'root'; $password = 'password'; $dataSet = array(); try { $dbh = new PDO("mysql:host=$hostname;dbname="MyDB", $username, $password); echo 'Connected to database'; } catch(PDOException $e) { echo $e->getMessage(); exit; } $stmt = $dbh->query('SELECT MyField from Mytable'); while ( $data = $stmt->fetch(PDO::FETCH_ASSOC) ) { $dataSet[] = $data; } if ( $dataSet ) { reset($dataSet); echo "<table>"; foreach ( $dataSet AS $row ) { echo "<tr><td>[$row['MyField']}</td></tr>"; } echo "</table>"; } if ( $dataSet ) { reset($dataSet); echo "<select><option>Make your choice</option>"; foreach ( $dataSet AS $row ) { echo "<option>{$row['MyField']}</option>"; } echo "</select>"; } -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php