Re: reuse my data source

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are ways to reset/rewind the results, however if you loop and fetch
into an array you don't have to reset the array and If you use fetchAll()

    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

You just foreach over $data whenever you want.


On Mon, Jun 2, 2014 at 11:59 AM, Jim Lucas <lists@xxxxxxxxx> wrote:

> 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
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux