Re: PEAR Access Kills While Loop
| From: | brian | Date: | Thu, 21 Sep 2006 16:27:24 +0000 |
| Subject: | Re: PEAR Access Kills While Loop | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-25191@lists.php.net to get a copy of this message | ||
Alex Ezell wrote:
I have a loop that prints out the data from a query into table rows, as shown below. [PHP] $res =& $db->query("SQL Query Here"); $printed = 0; while($res->fetchInto($row)){getOne frees the result in $this->db. I suspect that might be your problem (without seeing more of what's going on. Why don't you just select status in the first query? And if you were to take some of that code out of the class itself you could simplify it somewhat. In your class, make your query and save the result into an array (assuming PEAR::MDB2): $res =& $db->query("SQL Query Here"); // fetch each row as an array and stuff it into an array variable while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {if($printed%2!=0){$rowcolor="#EEEEEE";}else{$rowcolor="#FFFFFF";} $filmType = $films->convertType($row->film_type); $dateAdded = $films->convertDateAdded($row->dateadded); //$filmStatus = $films->printStatus($row->filmID); print<<<eof <tr bgcolor=$rowcolor> <td class="small">$row->filmID</td> <td class="small">$dateAdded</td> <td class="small">$filmType</td> <td><ahref="$PHP_SELF?action=view&filmID=$row->filmID">$row->title</a></td><td>$filmStatus</td> <td class="small"><ahref="/profile_$row->userID.html">$row->firstname $row->lastname</a></td><td><img src="images/edit.gif" alt="edit" width="14" height="15"border="0"></td></tr>eof;$printed++;} [/PHP] Notice the line that is commented which includes the variable $filmStatus. When that line is NOT commented, the loop only prints the first result from the query. When it IS commented, the loop prints all the results as it should. $films->printStatus() is this function in the class: [PHP] function printStatus($filmID){$status = $this->db->getOne("SELECT active FROM films WHERE filmID= '$filmID'");if($status->active == 1){ $printStatus .= "<img DEFANGED_src='\"images/active.gif\"' DEFANGED_width='\"12\"'DEFANGED_height='\"11\"'> ";} else { $printStatus .= "<img DEFANGED_src='\"images/inactive.gif\"' DEFANGED_width='\"12\"'DEFANGED_height='\"11\"'> ";} return $printStatus;} [/PHP] And yes, I know it seems like overkill to just check one little status column but the function will eventually check many more columns and conditions and print out more images and text based on the results. So, when that function is called, the loop in which it is contained stops after the first run. Any ideas? /alex
/* convert here
*/
$row['dateadded'] = $this->convertDateAdded($row['dateadded']);
$row['film_type'] = $this->convertType($row['film_type']);
array_push($this->your_array, $row);
}
@$res->free();
function fetchYourArray()
{
return $this->your_array;} Then, in your presentation script, do something like: $films = $your_class->fetchYourArray(); $count = 1; foreach ($films as $row) {
/* specify Row_0 or Row_1 like this so the style
* can be kept in a stylesheet for easy re-use.
* Using short tags to output PHP directly.
*/
?>
<tr class="Row_<?= ++$count % 2 ?>">
<!-- Note that column fields are in an assoc. array now -->
<td class="small"><?= $row'[filmID'] ?></td>
<td class="small"><?= $row['dateadded'] ?></td>
<td class="small"><?= $row['film_type'] ?></td>
<td><a href="<?= $_SERVER['PHP_SELF'] ?>?action=view&filmID=<?= $row['filmID'] ?>"><?= $row['title'] ?></a></td>
<!-- use the terniary operator to choose the image -->
<td><img src="images/<?= $row['status'] ? 'active' : 'inactive' ?>.gif" width="12" height="11" /> </td>
<td class="small"><a href="/profile_<?= $row['userID'] ?>.html"><?= $row['firstname'] ?> <?= $row['lastname'] ?></a></td>
<td><img src="images/edit.gif" alt="edit" width="14" height="15" border="0"></td>
</tr>
<?php
}
?>
In your stylesheet:
.Row_0 { background-color: #EEE; }
.Row_1 { background-color: #FFF; }
brian