Bluehost.com Web Hosting $6.95

Loop issues

This is a discussion on Loop issues within the PHP General forums, part of the PHP Programming Forums category; Hello all, I am having trouble trying to figure out how I should compose this loop to give me ALL ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-16-2007
Dan Shirah
 
Posts: n/a
Default Loop issues

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

Reply With Quote
  #2 (permalink)  
Old 11-16-2007
Jeremy Mcentire
 
Posts: n/a
Default 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

Reply With Quote
  #3 (permalink)  
Old 11-16-2007
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Loop issues

Jeremy,

What you replied with is what I was also trying to get across.

When I print out the array it shows that I pulled all of the names
correctly. And the issue is like you stated that the $name variable is being
overwritten.

What I have not been able to figure out is how to get each name populated to
my table.

Do you think it is my while () statement that needs to be changed?


On 11/16/07, Jeremy Mcentire <jmcentire@zootoo.com> wrote:
>
> 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

>
>


Reply With Quote
  #4 (permalink)  
Old 11-16-2007
Daniel Brown
 
Posts: n/a
Default Re: [PHP] Loop issues

On Nov 16, 2007 1:57 PM, Dan Shirah <mrsquash2@gmail.com> wrote:
[snip!]
> 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);
> }
> }


This is defining $cs_type2, et al, as $cs_row['request_type'], et
al.... then overwriting them. Is this your desired effect? If so,
why?


> $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);
> }
> }


Same basic idea here, Danny Boy. What are you doing the
transliteration for when you're not accessing those variables (at
least as far as I can see)?

> $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
>




--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
Reply With Quote
  #5 (permalink)  
Old 11-16-2007
Jeremy Mcentire
 
Posts: n/a
Default Re: [PHP] Loop issues

So, take a look at what's below and see how it works for you.

<?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);

while ($cs_row = mssql_fetch_array($cs_type)) {
$cs[cs_row['card_id']] = $cs_row;
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);

while ($tr_row = mssql_fetch_array($tr_type)) {
$tr[$tr_row['card_id']] = $tr_row;
print_r($tr_row);
}

$num_total = $cs_num + $tr_num;
$multiple = "MULTIPLE";

?>

<?php

foreach ($id_set as $id) {
echo "ID: {$id}";
echo "cs: " . print_r($cs[$id]);
echo "tr: " . print_r($tr[$id]);
}

?>
Reply With Quote
  #6 (permalink)  
Old 11-16-2007
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Loop issues

Yeah...see...I'm confused apparently!

My while loop is getting all of the names correctly, apparently my problem
is...once i get them, how do I echo each one out seperately?

Based off of the print_r($cs_row) I can see all of the names. What should I
be doing differently to output each name into my table?


On 11/16/07, Daniel Brown <parasane@gmail.com> wrote:
>
> On Nov 16, 2007 1:57 PM, Dan Shirah <mrsquash2@gmail.com> wrote:
> [snip!]
> > 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);
> > }
> > }

>
> This is defining $cs_type2, et al, as $cs_row['request_type'], et
> al.... then overwriting them. Is this your desired effect? If so,
> why?
>
>
> > $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);
> > }
> > }

>
> Same basic idea here, Danny Boy. What are you doing the
> transliteration for when you're not accessing those variables (at
> least as far as I can see)?
>
> > $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
> >

>
>
>
> --
> Daniel P. Brown
> [office] (570-) 587-7080 Ext. 272
> [mobile] (570-) 766-8107
>
> If at first you don't succeed, stick to what you know best so that you
> can make enough money to pay someone else to do it for you.
>


Reply With Quote
  #7 (permalink)  
Old 11-16-2007
Jeremy Mcentire
 
Posts: n/a
Default Re: [PHP] Loop issues

> Yeah...see...I'm confused apparently!
>
> My while loop is getting all of the names correctly, apparently my
> problem
> is...once i get them, how do I echo each one out seperately?
>
> Based off of the print_r($cs_row) I can see all of the names. What
> should I
> be doing differently to output each name into my table?


What you're doing wrong:

Assume mssql_fetch_array($cs_type) has two rows:
request_type = 'type1', first_name = 'Bob', last_name = 'Consumer'
request_type = 'type2', first_name = 'Alice', last_name = 'Cooper'

So we can replace it with a variable $array that has the same data.
$array = array(
array(
'request_type' => 'type1',
'first_name' => 'Bob',
'last_name' => 'Consumer'
),
array(
'request_type' => 'type2',
'first_name' => 'Alice',
'last_name' => 'Cooper'
),
);

Then this:

while ($cs_row = iterator($array)) {
$cs_type2 = $cs_row['request_type'];
$cs_name = $cs_row['first_name']." ".$cs_row['last_name'];
print_r ($cs_row);
}

Is functionally the same as:

$cs_type2 = $array[0]['request_type'];
$cs_name = $array[0]['first_name']." ".$array[0]['last_name'];
print_r ($array[0]);

$cs_type2 = $array[1]['request_type'];
$cs_name = $array[1]['first_name']." ".$array[1]['last_name'];
print_r ($array[1]);

Now. Why would the print_r return the right information?

Because, you're printing it for EACH row.

Why would the variable $cs_type2 have only the information of the
last row?

Because, you're OVERwriting it on EACH row.
Reply With Quote
  #8 (permalink)  
Old 11-16-2007
Jim Lucas
 
Posts: n/a
Default 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
Reply With Quote
  #9 (permalink)  
Old 11-16-2007
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Loop issues

Just noticed on that last one I forgot to change one variable name.

the line that was this
$tr_type = mssql_query($get_tr) or die(mssql_get_last_message());

change it to this
$tr_type = mssql_query($SQL) or die(mssql_get_last_message());


--
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
Reply With Quote
  #10 (permalink)  
Old 11-19-2007
Dan Shirah
 
Posts: n/a
Default Re: [PHP] Loop issues

Jim,

I used your suggestion and modified it a little bit. All of the names are
pulled from the database, but for some reason once it has pulled all the
names form each query it is adding an empty result to the end.

So when I do a echo $row['first_name']." ".$row['last_name']."<br />\n"; it
is returning something like this:

John Smith
Bob Smith

Jane Smith
Robert Smith

The gap between the names contains a single space " ". I tried to trim the
results but cannot get rid of it.

Any ideas?


On 11/16/07, Jim Lucas <lists@cmsws.com> wrote:
>
> 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:57 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0