Jump to content

DW code in a function


Recommended Posts

This might seem odd, because I managed to get it working. But, I don't understand why it is working when I didn't change how it works.

 

I wanted to take some fairly generic DW generated code and turn it into a function to cut use repeatedly on other pages. It would not work and I kept fiddling with it and got nowhere. From all appearances it should work. It would work fine when just part of the page. Finally I stripped out all the extras that DW puts in until just the code needed was there and it worked. My question is why?

 

 

// non-working function

function updatebal($event3) {

include('Connections/rentals.php');

mysql_select_db($database_rentals, $rentals);
$query_bill3 = "SELECT cost FROM event_bill WHERE event_num = $event3";
$bill13 = mysql_query($query_bill3, $rentals) or die(mysql_error());
$row_bill3 = mysql_fetch_assoc($bill3);
$totalRows_bill3 = mysql_num_rows($bill3);

mysql_select_db($database_rentals, $rentals);
$query_pay3 = "SELECT amount FROM payments WHERE eventId = $event3";
$pay3 = mysql_query($query_pay3, $rentals) or die(mysql_error());
$row_pay3 = mysql_fetch_assoc($pay3);
$totalRows_pay3 = mysql_num_rows($pay3);

mysql_select_db($database_rentals, $rentals);
$query_eventbal3 = "SELECT * FROM events WHERE event_num = $event3";
$eventbal3 = mysql_query($query_eventbal3, $rentals) or die(mysql_error());
$row_eventbal3 = mysql_fetch_assoc($eventbal3);
$totalRows_eventbal3 = mysql_num_rows($eventbal3);

$totalbill3 = 0;
 do { 
$totalbill3 = ($row_bill3['cost'] + $totalbill3);
 } while ($row_bill3 = mysql_fetch_assoc($bill3));
$totalpay3 = 0;  
 do { 
$totalpay3 = ($row_pay3['amount'] + $totalpay3);
 } while ($row_pay3 = mysql_fetch_assoc($pay3));
$totalbal3 = ($totalbill3 + $totalpay3);

$updateSQL3 = sprintf("UPDATE events SET bal=$totalbal WHERE event_num = $event3");

 mysql_select_db($database_rentals, $rentals);
 $Result3 = mysql_query($updateSQL3, $rentals) or die(mysql_error());

mysql_free_result($bill3);

mysql_free_result($pay3);

mysql_free_result($eventbal3);
}


// working function
function updatebal($event3) {
include('Connections/rentals.php');

mysql_select_db($database_rentals, $rentals);

$query_pay3 = "SELECT amount FROM payments WHERE eventId = $event3";
$pay3 = mysql_query($query_pay3, $rentals);

$query_bill3 = "SELECT cost FROM event_bill WHERE event_num = $event3";
$bill3 = mysql_query($query_bill3, $rentals);

$totalpay3 = 0;  
 while ($row_pay3 = mysql_fetch_assoc($pay3)) { 
   $rowspay3 = $row_pay3['amount'];
$totalpay3 += $rowspay3;
 }  
 
$totalbill3 = 0;
 while ($row_bill3 = mysql_fetch_assoc($bill3)) { 
$rowsbill3 = $row_bill3['cost'];
$totalbill3 += $rowsbill3;
 } 

$totalbal3 = ($totalbill3 - $totalpay3);

$updateSQL3 = sprintf("UPDATE events SET bal= $totalbal3 WHERE event_num = $event3");

 mysql_select_db($database_rentals, $rentals);
 $Result3 = mysql_query($updateSQL3, $rentals) or die(mysql_error());

return($totalbal3);
mysql_free_result($bill3);
mysql_free_result($pay3);

exit;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.