Re: Loop problem

From: Date: Fri, 04 May 2007 19:05:26 +0000
Subject: Re: Loop problem
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-254429@lists.php.net to get a copy of this message
No problem. I just prefer to use extract() for areas that are only pulling out a single set of data, or several sets with different variable names within the array. Less typing, no reassignments necessary. Personal preference, that's all. On 5/4/07, Dan Shirah <mrsquash2@gmail.com> wrote:
Thank you Daniel!! I didn't do it exactly as you suggested, but you put me in exactly the right direction! THANK YOU! Below is how I have it working if anyone else is interested. ****The javascript**** function showAlert(id,type) { alert( 'The type is:' + type + 'The ID is:' + id ); } ****The Form**** <?php if(!empty($result_payments)) { while ($row_payments = mssql_fetch_array($result_payments)) { $id = $row_payments['child_support_id']; $case_number = $row_payments['case_number']; $payment_amount = $row_payments['payment_amount']; $total += $row_payments['payment_amount']; ?> <tr>
    <td width="21">ID:</td>
    <td width="88"><a href="javascript:showAlert('<?php echo $id;
?>','C')"><?php echo $id; ?></a></td>
    <td width="98">Case Number:</td>
    <td width="252"><?php echo $case_number; ?></td>
    <td width="116"><div align="right">Payment Amount:</div></td>
    <td width="105"><div align="right">$<?php echo
number_format($payment_amount, 2); ?></div></td> </tr> <?php } } ?> thanks again. On 5/4/07, Daniel Brown <parasane@gmail.com> wrote:
    I know you said it's a form.... but where does the browser realize
that?
    Using a hidden value such as that would require a POST or GET action
via a form. JavaScript wouldn't work for that. However, you could do something like this: <snip> <SCRIPT LANGUAGE="JavaScript"> function showAlert(id,type) {
        alert('The type is:' + type + ' - The ID is:' + id);
} </SCRIPT> <? while($row_payments = mssql_fetch_array($result_payments)) {
        extract($row_payments);
        $total += $payment_amount;
?>
        <tr>
                <td width="21">ID:</td>
                <td width="88"><a
href="javascript:showAlert('<?=$child_support_id;?>','C')"><?=$child_support_id;?></a></td>
                <td width="98">Case Number:</td>
                <td width="252"><?=$case_number;?></td>
                <td width="116"><div align="right">Payment
Amount:</div></td>
                <td width="105"><div
align="right">$<?=number_format($payment_amount,2);?></div></td>
        </tr>
<? </snip>
    If 'C' isn't a constant, but rather a dynamic value from a column in
a database (named the same as the convention in your example suggests), then simply modify it as such: <td width="88"><a href="javascript:showAlert('<?=$child_support_id;?>','<?=$request_type;?>')"><?=$child_support_id;?></a></td>
    That should do it for you.
On 5/4/07, Dan Shirah < mrsquash2@gmail.com> wrote:
Okay, I think this is a 1/2 PHP problem and 1/2 Javascript problem. And I think my Javascript problem is being caused by my PHP. Follow? The code below is a loop of records returned from my query: 1 - If the result is not empty it will loop through the results. 2 - It assigns variables based on columns from the query. 3 - The first column of the row is the ID which is used by the Javascript to pull up the correct record. 4 - I put in a hidden value to assign all the rows in this loop a "request_type" value. 5 - When someone clicks on a record ID it calls the Javascript to "alert" the ID and request_type. 6 - If the loop only returns a single record, the "alert" displays correctly. Example: The type is:C The ID is:80 7 - If the loop returns multiple records, the "alert" does not return the correct values. Example: The type is:undefined The ID is:80
               The type is:undefined The ID is:85
               The type is:undefined The ID is:104
Why do I only get "undefined" if the loop returns more than a single record??? ***The Javascript*** function showAlert(id) { var type; type = document.Submit.request_type.value alert( 'The type is:' + type + 'The ID is:' + id ); } ***The Form*** <table align="center" border="0" cellpadding="0" cellspacing="0" width="680"> <?php if(!empty($result_payments)) { while ($row_payments = mssql_fetch_array($result_payments)) { $id = $row_payments['child_support_id']; $case_number = $row_payments['case_number']; $payment_amount = $row_payments['payment_amount']; $total += $row_payments['payment_amount']; ?> <tr>
    <td width="21">ID:</td>
    <td width="88"><?php echo "<a
href='javascript:showAlert($id)'>$id</a>" ?></td>
    <td width="98">Case Number:</td>
    <td width="252"><?php echo $case_number; ?></td>
    <td width="116"><div align="right">Payment Amount:</div></td>
    <td width="105"><div align="right">$<?php echo
number_format($payment_amount, 2); ?></div></td>
    <input type="hidden" name="request_type" value="C">
</tr> <?php
    }
} ?> </table>
-- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107
-- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107

« previous php.general (#254429) next »