Discussion:
reuse my data source
ICCSI
2014-05-31 17:16:31 UTC
Permalink
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>
Jim Giner
2014-05-31 18:52:50 UTC
Permalink
Post by ICCSI
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>
Well - you have to reset the query results in order to browse thru them
a second time. Try doing a fetchall to create an array of your results
and then use the foreach to loop thru them for your two needs.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-06-02 16:59:58 UTC
Permalink
Post by ICCSI
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
Shawn McKenzie
2014-06-02 20:48:31 UTC
Permalink
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.
Post by ICCSI
Post by ICCSI
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>
<?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
Loading...