Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/129629-solved-need-help-looping/
Share on other sites

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

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

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>';
}
?>

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.