elis Posted October 22, 2008 Share Posted October 22, 2008 This should be fairly easy for me to figure out, but every loop I try becomes infinite or just plain fails. Here is the snippet I'm trying to loop: <?php if($stmtx->rowCount() !==0){ $table_row.='<table><td><a href="interview-feedback.php?cid=' . $id . '&jid=' . $job['job_id'] . '&TB_iframe=true&height=315& amp;width=450" class="thickbox">Feedback for'; if($status==1) { $table_row.=' 1st interview'; } elseif($status==2){ $table_row.=' 2nd interview'; } elseif($status==3){ $table_row.=' final interview'; } $table_row.='</a></td><td>'; if($complete==0) { $table_row.='INCOMPLETE-IMAGE-HOLDER'; } else{ $table_row.='<img src="images/checkbox-checked.jpg">'; } $table_row.='</td>'; } I'm trying to loop the entire snippet, and continue echoing table rows, until there are no more rows pulled from MYSQL that match a user's id. Can anybody help? Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/ Share on other sites More sharing options...
Barand Posted October 22, 2008 Share Posted October 22, 2008 and your query code is where? Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672073 Share on other sites More sharing options...
elis Posted October 22, 2008 Author Share Posted October 22, 2008 Here. <?php $qry = "SELECT * FROM interviewFeedback WHERE candidate_id = :id AND joborder = :jid"; $stmtx = $conn->prepare($qry); $stmtx->bindParam(':jid', $job['job_id']); $stmtx->bindParam(':id', $id); $result = $conn->query($qry); Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672081 Share on other sites More sharing options...
alexweber15 Posted October 22, 2008 Share Posted October 22, 2008 please include the loop that currently fails, along with the query and the echo section, in the same way they appear! a lot is left vague: - do you execute the query every iteration? - what condition do you iterate upon? (presumably ->rowcount() but its not clear) here's a quote from the php manual on PDOStatement->rowCount (i assume you are using PDO by the sql-related syntax) For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action. maybe this could be your problem? * also personally (depends on context and i don't know yours), but I would retrieve ALL values // assume DB handler ($dbh) and formed query ($sql) $result = $dbh->query($sql); // check if query returned anything if($result->fetchColumn()){ $resultset = $result->fetchAll(PDO::FETCH_ASSOC); }else{ die('no results matched query'); } // echo table foreach($resultset as $colname => $colvalue){ ... } i know it doesnt use prepared statements but you get the idea right? Alex Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672114 Share on other sites More sharing options...
elis Posted October 22, 2008 Author Share Posted October 22, 2008 My mistake, I removed the loop from the original because it was making the server crash and forgot to add it in the snippet. To be honest, I'm not sure I'm even using the correct syntax for the loop. <?php $qry = "SELECT * FROM interviewFeedback WHERE candidate_id = :id AND joborder = :jid"; $stmtx = $conn->prepare($qry); $stmtx->bindParam(':jid', $job['job_id']); $stmtx->bindParam(':id', $id); $stmtx->execute(); while($stmtx->rowCount()>0) { $table_row.=' <table><td><a href="interview-feedback.php?cid=' . $id . '&jid=' . $job['job_id'] . '&TB_iframe=true&height=315&width=450" class="thickbox">Feedback for'; } I've cut the $table_row.=' short since I've already included it in the original post. Basically, I just need an echo of some table rows which is what $table_row.=' does. However, when the loop is included, the page doesn't load - I assume because it's reading it as an infinite loop? Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672129 Share on other sites More sharing options...
Barand Posted October 22, 2008 Share Posted October 22, 2008 I don't know which class you are using so I use standard mysql lib functions. Also, I am assuming $status comes from the db records (SELECT *, as well as being inefficient, tells nothing about the query function) The "while loop" is what you need, calling mysql_fetch_assoc. This will stop looping when no more rows left to read. You class prob has an eqivalent to fetch_assoc function. <?php $qry = "SELECT status FROM interviewFeedback WHERE candidate_id = '$id' AND joborder = '{$job['job_id']}'"; $result = mysql_query($qry); $stats = array (1 => '1st interview', '2nd interview', 'final interview'); if (mysql_num_rows($result) > 0) { $table_row = '<table>'; while ($row = mysql_fetch_assoc($result)) { $status = $stats[$row['status']]; $image = $complete==0 ? 'INCOMPLETE-IMAGE-HOLDER' : '<img src="images/checkbox-checked.jpg">'; $table_row.="<tr> <td><a href='interview-feedback.php?cid=$id&jid={$job['job_id']}&TB_iframe=true&height=315&width=450' class='thickbox'>Feedback for $status</a></td> <td>$image</td> </tr>"; } $table_row .= '</table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672148 Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 I assume (without looking at the class, only going by the method description) that $stmtx->rowCount() tells you how many rows were returned in the query. If more than 0 rows were returned, and your while loops condition is to execute while there's more than one row returned, then you end up creating an infinite loop. You probably need to be doing somethingalong the lines of while($stmx->fetchRow()) { or whatever is in that class. edit: whoops. yeah, what barand said. Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672150 Share on other sites More sharing options...
elis Posted October 22, 2008 Author Share Posted October 22, 2008 I assume (without looking at the class, only going by the method description) that $stmtx->rowCount() tells you how many rows were returned in the query. If more than 0 rows were returned, and your while loops condition is to execute while there's more than one row returned, then you end up creating an infinite loop. You probably need to be doing somethingalong the lines of while($stmx->fetchRow()) { or whatever is in that class. That helped. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672158 Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 Awesome. Credit goes to Barand though; he posted before me, saying the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/#findComment-672160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.