Re: [PHP] Loop issues



I'm not sure what you're trying to do here; a sample of the output you expect would help -- so too would trimming anything extranious to the PHP itself(like the html).

But, I think the issue you're running in to is any variable, like $tr_type2, is being over-writen each time you get a new row from your mysql result iterator.

So, if I have:

$mysql_results = array(
array('name'=>'Bob'),
array('name'=>'Alice'),
);

And used your "while" syntax:

while ($row = next($mysql_results)) {
$name = $row['name'];
}

$name would ALWAYS be 'Alice'. 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.

Then, of course, you can simply iterate over the resulting array when you display.


Jeremy Mcentire
Ant Farmer
ZooToo LLC


On Nov 16, 2007, at 1:57 PM, 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
.



Relevant Pages

  • Re: [PHP] Loop issues
    ... I am having trouble trying to figure out how I should compose this loop to ... Below are my queries. ... Smith, James Smith. ... Because of this I think I need to put a loop where the echo ...
    (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)
  • 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: I have sub-categories but want to display full category path
    ... MySQL connection class from his book "Beginning PHP5 and MySQL" published by ... You don't have to reconnect to the MySQL server in each and every loop. ... Do it once before entering the while loop, then just fire your queries. ... array and just use implodeto print it out with '>>' separators. ...
    (comp.lang.php)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)