Re: [PHP] Loop issues



Dan Shirah wrote:
Hello all,

I am having trouble trying to figure out how I should compose this loop to
give me ALL the results I want.

Below are my queries. I am querying two different databases to pull in
records that match the requested $id. I am then putting the result into a
$variable and also counting the number of rows returned. These queries work
just fine and pull in all of the data that I want...and by viewing the
print_r() I have verified that all the information was returned.

<?php
$get_cs = "SELECT DISTINCT
request_type, card_id, first_name, last_name
FROM
support_payment_request
WHERE
card_id = '$id'";
$cs_type = mssql_query($get_cs) or die(mssql_get_last_message());
$cs_num = mssql_num_rows($cs_type);

if($cs_num > 0) {
while ($cs_row = mssql_fetch_array($cs_type)) {
$cs_type2 = $cs_row['request_type'];
$cs_first = $cs_row['first_name'];
$cs_last = $cs_row['last_name'];
$cs_name = $cs_first." ".$cs_last;
print_r ($cs_row);
}
}
$get_tr = "SELECT DISTINCT
request_type, card_id, first_name, last_name
FROM
payment_request
WHERE
card_id = '$id'";
$tr_type = mssql_query($get_tr) or die(mssql_get_last_message());
$tr_num = mssql_num_rows($tr_type);

if($tr_num > 0) {
while ($tr_row = mssql_fetch_array($tr_type)) {
$tr_type2 = $tr_row['request_type'];
$tr_first = $tr_row['first_name'];
$tr_last = $tr_row['last_name'];
$tr_name = $tr_first." ".$tr_last;
print_r ($tr_row);
}
}
$num_total = $cs_num + $tr_num;
$multiple = "MULTIPLE";
?>

Here is where I am running into problems. First I am writing an if ()
statement to see if there were any rows returned from the queries. If a row
was returned I am echoing out the data that was assigned to the different
variables above. This works...kind of...

<td width='89' height='13' align='center' class='tblcell'><div
align='center'><?php echo "<a href='javascript:editRecord($id)'>$id</a>";
?></div></td>
<td width='172' height='13' align='center' class='tblcell'><div
align='center'><?php if ($cs_num > 0) { echo "$cs_name<br />\n"; }
if ($tr_num > 0) { echo "$tr_name<br />\n";
} ?></div></td>
<td width='201' height='13' align='center' class='tblcell'><div
align='center'><?php echo "$dateTime"; ?></div></td>
<td width='158' height='13' align='center' class='tblcell'><div
align='center'><?php if ($num_total > 1) { echo $multiple; }
if ($num_total == 1 && $cs_num == 1) { echo $cs_type2;
}
if ($num_total == 1 && $tr_num == 1) { echo $tr_type2;
} ?></div></td>
<td width='160' height='13' align='center' class='tblcell'><div
align='center'><?php echo "$last_processed_by"; ?></div></td>

If a single row was returned by the query, all of the information echos out
just fine. BUT, If one of the queries returned more than one row, the
information that is echo'd out is only the LAST row's information. For
example, the result of my $cs_type query returns 3 names: John Smith, Jane
Smith, James Smith. The only information being populated to my table is
James Smith. Because of this I think I need to put a loop where the echo
"$cs_name<br />\n"; is so it will loop through all of the returned names and
show them all. I have tried a for, foreach and while loop but I just can't
seem to wrap my fingers around the right way to use it.

Any help is appreciated.

Thanks,
Dan

What about something like this?

<?php

$records = array();

$SQL = "SELECT DISTINCT request_type,
card_id,
first_name,
last_name
FROM support_payment_request
WHERE card_id = '$id'";

$cs_type = mssql_query($SQL) or die(mssql_get_last_message());

if ( $cs_type ) {
while ($records[] = $record = mssql_fetch_array($cs_type)) {
print_r ($record);
}
}

$SQL = "SELECT DISTINCT request_type,
card_id,
first_name,
last_name
FROM payment_request
WHERE card_id = '$id'";
$tr_type = mssql_query($get_tr) or die(mssql_get_last_message());

if ( $tr_type ) {
while ( $records[] = $record = mssql_fetch_array($tr_type)) {
print_r ($record);
}
}

if ( count($records) ) {
echo '<table>';

foreach ( $records AS $id => $row ) {

echo <<<ROW
<tr>
<td width='89' height='13' align='center' class='tblcell'><a href='javascript:editRecord({$row['card_id']})'>{$row['card_id']}</a></td>
<td width='172' height='13' align='center' class='tblcell'>{$row['first_name']} {$row['last_name']}</td>
</tr>
ROW;

}
echo '</table>';
}

?>

Obviously, this is completely untested. You should be able to run it.

I took out the table cells that had to do with the date/time and the total,

Not sure where you were getting that information from.

But this should give you a little better idea on how to do it.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
.



Relevant Pages

  • Re: [PHP] Loop issues
    ... The reason is because the first time the while loop executes, it sets $name from unset to 'Bob' and on the second time through, it sets $name from 'Bob' to 'Alice'. ... The issue isn't just echoing in a loop, but defining an array of results to begin with. ... Below are my queries. ... Smith, James Smith. ...
    (php.general)
  • Loop issues
    ... I am having trouble trying to figure out how I should compose this loop to ... Below are my queries. ... If a single row was returned by the query, all of the information echos out ... Smith, James Smith. ...
    (php.general)
  • Re: [PHP] Loop issues
    ... When I print out the array it shows that I pulled all of the names ... the while loop executes, it sets $name from unset to 'Bob' and on the ... Below are my queries. ... Smith, James Smith. ...
    (php.general)
  • Re: Reading a variable line by line with while loop
    ... done < `echo "$Variable"` ... echo "History - $History" ... The last echo command returns nothing, but if I put an echo command in ... the loop either before, or after the replace spaces command, it echoes ...
    (Ubuntu)
  • Re: 2.6.16-rc4: known regressions
    ... either with a command line argument, or with just the early bootup initrd ... Is there a way to tell the kernel about which is the root device other ... a loop with one second delay. ... echo -n "Waiting for root device to appear" ...
    (Linux-Kernel)