Jump to content


Photo

DW code in a function


  • Please log in to reply
No replies to this topic

#1 griffinme

griffinme
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 14 April 2005 - 04:05 PM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users