Re: Loop issues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx> 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
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux