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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 > > |
|
|||
|
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. |
|
|||
|
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]); } ?> |
|
|||
|
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. > |
|
|||
|
> 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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 > > |