Link to home
Start Free TrialLog in
Avatar of Glenn Abelson
Glenn AbelsonFlag for United States of America

asked on

php simple sum question part 2

Here is the actual page: http://www.sm4software.com/sm4analysis/sm4analysisview.php?uniqueid=9
with 4 sample Yes No fields (all assumptions above are fine).    However, everything is stored as variables. The ACTUAL field names are 1, 2, 3, 4...which is probably confusing me, as the total should be the total of the Yes answers.

Here is the actual code saved with a .txt extension so you can read it and open it.  Change the .txt to .php after you save it.

http://www.sm4software.com/sm4analysis/sm4analysisview.txt
Avatar of Joe Wu
Joe Wu
Flag of Australia image

so how are you actually summing the answers up?
i can't actually tell where you are summing up your answer, but i do see these lines:
$sm4analysis->zX->ViewValue = "Yes";

where X is a number from 1-4, i guess this is where you set whether a value is YES or NO am I correct? then can you not just declare say a variable called $counter and set it = 0 initially:

$counter = 0;

then add 1 to the counter each time a value is set to YES?

$sm4analysis->zX->ViewValue = "Yes";
$counter++;

then at the end of your code you can output the counter variable and it would have the number of yes answers.

NOt sure if this is what you are looking for?

Avatar of Glenn Abelson

ASKER

It is kind of working, but your example is incrementing the counter regardless of the value of zX.
Do I need to  set an IF ELSE?

Anyway look at page.

Here is a code piece:

<table class="ewTable">
      <tr class="ewTableRow">
            <td class="ewTableHeader">1</td>
            <td<?php echo $sm4analysis->z1->CellAttributes() ?>>
<div<?php echo $sm4analysis->z1->ViewAttributes() ?>><?php echo $sm4analysis->z1->ViewValue ?></div>
<?php
$yescounter=0;
$sm4analysis->z1->ViewValue = "Yes";
$yescounter++;
echo $yescounter
?>
</td>
      </tr>
      <tr class="ewTableAltRow">
            <td class="ewTableHeader">2</td>
            <td<?php echo $sm4analysis->z2->CellAttributes() ?>>
<div<?php echo $sm4analysis->z2->ViewAttributes() ?>><?php echo $sm4analysis->z2->ViewValue ?></div>
<?php
$sm4analysis->z2->ViewValue = "Yes";
$yescounter++;
echo $yescounter
?>

Above only on first two records
And further down...at the end:

<?php

echo $yescounter
?>
first, don't forget the ";" after your echo, as you may hav problems later on with syntax errors :)

as for incrementing regardless of zX, is this not what you wanted? to count all the "Yes" answers? regardless of zX we still have to count that YES answer right?
At this link: http://www.sm4software.com/sm4analysis/sm4analysisview.php?uniqueid=6
The number 2 under the first NO
==
No
2
==
is a result of echo $yescounter;

Because this is a NO, yescounter should still be 1, not 2.

I did check the database and am getting 0 and 1 properly in the fields.

To check more...click on sm4analysis and view any data set.
AH...
since the actual data is 0 (no) and 1 (yes) should "Yes" be replaced with 1 in my code?
no i don't think it has anything to do with that...
$sm4analysis->z2->ViewValue = "Yes";
this obviously says that the value is YES, so how come it is printint out a NO?

do you have the code where it is printing out a NO?

Obviously if it is a NO then the counter should not increment.
I think this the pertinent code:

<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js");
//-->

</script>
<p><span class="phpmaker">View TABLE: sm 4analysis
<br><br>
<a href="sm4analysislist.php">Back to List</a>&nbsp;
<a href="sm4analysisadd.php">Add</a>&nbsp;
</span>
</p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
      $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<p>
<form>

<table class="ewTable">
      <tr class="ewTableRow">
            <td class="ewTableHeader">1</td>
            <td<?php echo $sm4analysis->z1->CellAttributes() ?>>
<div<?php echo $sm4analysis->z1->ViewAttributes() ?>><?php echo $sm4analysis->z1->ViewValue ?></div>
<?php
$yescounter=0;
$sm4analysis->z1->ViewValue = "Yes";
$yescounter++;
echo $yescounter;
?></td>
      </tr>
      <tr class="ewTableAltRow">
            <td class="ewTableHeader">2</td>
            <td<?php echo $sm4analysis->z2->CellAttributes() ?>>
<div<?php echo $sm4analysis->z2->ViewAttributes() ?>><?php echo $sm4analysis->z2->ViewValue ?></div>
<?php
$sm4analysis->z2->ViewValue = "Yes";
$yescounter++;
echo $yescounter;
?></td>
      </tr>
      <tr class="ewTableRow">
            <td class="ewTableHeader">3</td>
            <td<?php echo $sm4analysis->z3->CellAttributes() ?>>
<div<?php echo $sm4analysis->z3->ViewAttributes() ?>><?php echo $sm4analysis->z3->ViewValue ?></div>
<?php
$sm4analysis->z3->ViewValue = "Yes";
$yescounter++;
?></td>
      </tr>
      <tr class="ewTableAltRow">
            <td class="ewTableHeader">4</td>
            <td<?php echo $sm4analysis->z4->CellAttributes() ?>>
<div<?php echo $sm4analysis->z4->ViewAttributes() ?>><?php echo $sm4analysis->z4->ViewValue ?></div>
<?php
$sm4analysis->z4->ViewValue = "Yes";
$yescounter++;
?></td>
      </tr>
</table>
<p>The code for #2 is</p>
<p>&lt;?php<br />
  $sm4analysis-&gt;z2-&gt;ViewValue = &quot;Yes&quot;;<br />
  $yescounter++;<br />
  echo $yescounter;<br />
  ?&gt; </p>
<p>

<?php

?>
</p>
</form>
<?php

echo $yescounter;
?>
<p>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->
Here is the whole code
<?php
define("EW_PAGE_ID", "view", TRUE); // Page ID
define("EW_TABLE_NAME", 'sm4analysis', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "sm4analysisinfo.php" ?>
<?php include "userfn50.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php
 
// Open connection to the database
$conn = ew_Connect();
?>
<?php
 
// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php
 
// Page load event, used in current page
Page_Load();
?>
<?php
$sm4analysis->Export = @$_GET["export"]; // Get export parameter
$sExport = $sm4analysis->Export; // Get export parameter, used in header
$sExportFile = $sm4analysis->TableVar; // Get export file, used in header
?>
<?php
if (@$_GET["uniqueid"] <> "") {
	$sm4analysis->uniqueid->setQueryStringValue($_GET["uniqueid"]);
} else {
	Page_Terminate("sm4analysislist.php"); // Return to list page
}
 
// Get action
if (@$_POST["a_view"] <> "") {
	$sm4analysis->CurrentAction = $_POST["a_view"];
} else {
	$sm4analysis->CurrentAction = "I"; // Display form
}
switch ($sm4analysis->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // Set no record message
			Page_Terminate("sm4analysislist.php"); // Return to list
		}
}
 
// Set return url
$sm4analysis->setReturnUrl("sm4analysisview.php");
 
// Render row
$sm4analysis->RowType = EW_ROWTYPE_VIEW;
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "view"; // Page id
 
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
 
// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->
 
</script>
<p><span class="phpmaker">View TABLE: sm 4analysis
<br><br>
<a href="sm4analysislist.php">Back to List</a>&nbsp;
<a href="sm4analysisadd.php">Add</a>&nbsp;
</span>
</p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<p>
<form>
 
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">1</td>
		<td<?php echo $sm4analysis->z1->CellAttributes() ?>>
<div<?php echo $sm4analysis->z1->ViewAttributes() ?>><?php echo $sm4analysis->z1->ViewValue ?></div>
<?php
$yescounter=0;
$sm4analysis->z1->ViewValue = "Yes";
$yescounter++;
echo $yescounter;
?></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">2</td>
		<td<?php echo $sm4analysis->z2->CellAttributes() ?>>
<div<?php echo $sm4analysis->z2->ViewAttributes() ?>><?php echo $sm4analysis->z2->ViewValue ?></div>
<?php
$sm4analysis->z2->ViewValue = "Yes";
$yescounter++;
echo $yescounter;
?></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">3</td>
		<td<?php echo $sm4analysis->z3->CellAttributes() ?>>
<div<?php echo $sm4analysis->z3->ViewAttributes() ?>><?php echo $sm4analysis->z3->ViewValue ?></div>
<?php
$sm4analysis->z3->ViewValue = "Yes";
$yescounter++;
?></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">4</td>
		<td<?php echo $sm4analysis->z4->CellAttributes() ?>>
<div<?php echo $sm4analysis->z4->ViewAttributes() ?>><?php echo $sm4analysis->z4->ViewValue ?></div>
<?php
$sm4analysis->z4->ViewValue = "Yes";
$yescounter++;
?></td>
	</tr>
</table>
<p>The code for #2 is</p>
<p>&lt;?php<br />
  $sm4analysis-&gt;z2-&gt;ViewValue = &quot;Yes&quot;;<br />
  $yescounter++;<br />
  echo $yescounter;<br />
  ?&gt; </p>
<p>
 
<?php
 
?>
</p>
</form>
<?php
 
echo $yescounter;
?>
<p>
<script language="JavaScript" type="text/javascript">
<!--
 
// Write your table-specific startup script here
// document.write("page loaded");
//-->
 
</script>
<?php include "footer.php" ?>
<?php
 
// If control is passed here, simply terminate the page without redirect
Page_Terminate();
 
// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;
 
	// Page unload event, used in current page
	Page_Unload();
 
	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();
 
	 // Close Connection
	$conn->Close();
 
	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php
 
// Load row based on key values
function LoadRow() {
	global $conn, $Security, $sm4analysis;
	$sFilter = $sm4analysis->SqlKeyFilter();
	if (!is_numeric($sm4analysis->uniqueid->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@uniqueid@", ew_AdjustSql($sm4analysis->uniqueid->CurrentValue), $sFilter); // Replace key value
 
	// Call Row Selecting event
	$sm4analysis->Row_Selecting($sFilter);
 
	// Load sql based on filter
	$sm4analysis->CurrentFilter = $sFilter;
	$sSql = $sm4analysis->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values
 
			// Call Row Selected event
			$sm4analysis->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}
 
// Load row values from recordset
function LoadRowValues(&$rs) {
	global $sm4analysis;
	$sm4analysis->z1->setDbValue($rs->fields('1'));
	$sm4analysis->z2->setDbValue($rs->fields('2'));
	$sm4analysis->z3->setDbValue($rs->fields('3'));
	$sm4analysis->z4->setDbValue($rs->fields('4'));
	$sm4analysis->total->setDbValue($rs->fields('total'));
	$sm4analysis->uniqueid->setDbValue($rs->fields('uniqueid'));
}
?>
<?php
 
// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $sm4analysis;
 
	// Call Row Rendering event
	$sm4analysis->Row_Rendering();
 
	// Common render codes for all row types
	// 1
 
	$sm4analysis->z1->CellCssStyle = "";
	$sm4analysis->z1->CellCssClass = "";
 
	// 2
	$sm4analysis->z2->CellCssStyle = "";
	$sm4analysis->z2->CellCssClass = "";
 
	// 3
	$sm4analysis->z3->CellCssStyle = "";
	$sm4analysis->z3->CellCssClass = "";
 
	// 4
	$sm4analysis->z4->CellCssStyle = "";
	$sm4analysis->z4->CellCssClass = "";
 
	// total
	$sm4analysis->total->CellCssStyle = "";
	$sm4analysis->total->CellCssClass = "";
 
	// uniqueid
	$sm4analysis->uniqueid->CellCssStyle = "";
	$sm4analysis->uniqueid->CellCssClass = "";
	if ($sm4analysis->RowType == EW_ROWTYPE_VIEW) { // View row
 
		// 1
		if (!is_null($sm4analysis->z1->CurrentValue)) {
			switch ($sm4analysis->z1->CurrentValue) {
				case "1":
					$sm4analysis->z1->ViewValue = "Yes";
					break;
				case "0":
					$sm4analysis->z1->ViewValue = "No";
					break;
				default:
					$sm4analysis->z1->ViewValue = $sm4analysis->z1->CurrentValue;
			}
		} else {
			$sm4analysis->z1->ViewValue = NULL;
		}
		$sm4analysis->z1->CssStyle = "";
		$sm4analysis->z1->CssClass = "";
		$sm4analysis->z1->ViewCustomAttributes = "";
 
		// 2
		if (!is_null($sm4analysis->z2->CurrentValue)) {
			switch ($sm4analysis->z2->CurrentValue) {
				case "1":
					$sm4analysis->z2->ViewValue = "Yes";
					break;
				case "0":
					$sm4analysis->z2->ViewValue = "No";
					break;
				default:
					$sm4analysis->z2->ViewValue = $sm4analysis->z2->CurrentValue;
			}
		} else {
			$sm4analysis->z2->ViewValue = NULL;
		}
		$sm4analysis->z2->CssStyle = "";
		$sm4analysis->z2->CssClass = "";
		$sm4analysis->z2->ViewCustomAttributes = "";
 
		// 3
		if (!is_null($sm4analysis->z3->CurrentValue)) {
			switch ($sm4analysis->z3->CurrentValue) {
				case "1":
					$sm4analysis->z3->ViewValue = "yes";
					break;
				case "0":
					$sm4analysis->z3->ViewValue = "No";
					break;
				default:
					$sm4analysis->z3->ViewValue = $sm4analysis->z3->CurrentValue;
			}
		} else {
			$sm4analysis->z3->ViewValue = NULL;
		}
		$sm4analysis->z3->CssStyle = "";
		$sm4analysis->z3->CssClass = "";
		$sm4analysis->z3->ViewCustomAttributes = "";
 
		// 4
		if (!is_null($sm4analysis->z4->CurrentValue)) {
			switch ($sm4analysis->z4->CurrentValue) {
				case "1":
					$sm4analysis->z4->ViewValue = "Yes";
					break;
				case "0":
					$sm4analysis->z4->ViewValue = "no";
					break;
				default:
					$sm4analysis->z4->ViewValue = $sm4analysis->z4->CurrentValue;
			}
		} else {
			$sm4analysis->z4->ViewValue = NULL;
		}
		$sm4analysis->z4->CssStyle = "";
		$sm4analysis->z4->CssClass = "";
		$sm4analysis->z4->ViewCustomAttributes = "";
 
		// total
		$sm4analysis->total->ViewValue = $sm4analysis->total->CurrentValue;
		$sm4analysis->total->CssStyle = "";
		$sm4analysis->total->CssClass = "";
		$sm4analysis->total->ViewCustomAttributes = "";
 
		// uniqueid
		$sm4analysis->uniqueid->ViewValue = $sm4analysis->uniqueid->CurrentValue;
		$sm4analysis->uniqueid->CssStyle = "";
		$sm4analysis->uniqueid->CssClass = "";
		$sm4analysis->uniqueid->ViewCustomAttributes = "";
 
		// 1
		$sm4analysis->z1->HrefValue = "";
 
		// 2
		$sm4analysis->z2->HrefValue = "";
 
		// 3
		$sm4analysis->z3->HrefValue = "";
 
		// 4
		$sm4analysis->z4->HrefValue = "";
 
		// total
		$sm4analysis->total->HrefValue = "";
 
		// uniqueid
		$sm4analysis->uniqueid->HrefValue = "";
	} elseif ($sm4analysis->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($sm4analysis->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($sm4analysis->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}
 
	// Call Row Rendered event
	$sm4analysis->Row_Rendered();
}
?>
<?php
 
// Set up Starting Record parameters based on Pager Navigation
function SetUpStartRec() {
	global $nDisplayRecs, $nStartRec, $nTotalRecs, $nPageNo, $sm4analysis;
	if ($nDisplayRecs == 0) return;
 
	// Check for a START parameter
	if (@$_GET[EW_TABLE_START_REC] <> "") {
		$nStartRec = $_GET[EW_TABLE_START_REC];
		$sm4analysis->setStartRecordNumber($nStartRec);
	} elseif (@$_GET[EW_TABLE_PAGE_NO] <> "") {
		$nPageNo = $_GET[EW_TABLE_PAGE_NO];
		if (is_numeric($nPageNo)) {
			$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
			if ($nStartRec <= 0) {
				$nStartRec = 1;
			} elseif ($nStartRec >= intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1) {
				$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
			}
			$sm4analysis->setStartRecordNumber($nStartRec);
		} else {
			$nStartRec = $sm4analysis->getStartRecordNumber();
		}
	} else {
		$nStartRec = $sm4analysis->getStartRecordNumber();
	}
 
	// Check if correct start record counter
	if (!is_numeric($nStartRec) || $nStartRec == "") { // Avoid invalid start record counter
		$nStartRec = 1; // Reset start record counter
		$sm4analysis->setStartRecordNumber($nStartRec);
	} elseif (intval($nStartRec) > intval($nTotalRecs)) { // Avoid starting record > total records
		$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1; // Point to last page first record
		$sm4analysis->setStartRecordNumber($nStartRec);
	} elseif (($nStartRec-1) % $nDisplayRecs <> 0) {
		$nStartRec = intval(($nStartRec-1)/$nDisplayRecs)*$nDisplayRecs+1; // Point to page boundary
		$sm4analysis->setStartRecordNumber($nStartRec);
	}
}
?>
<?php
 
// Page Load event
function Page_Load() {
 
	//echo "Page Load";
}
 
// Page Unload event
function Page_Unload() {
 
	//echo "Page Unload";
}
?>

Open in new window

where does it actually set the variable to a NO?
Look at lines 117 to 121
$sm4analysis->z2->ViewValue = "Yes";
$yescounter++;
echo $yescounter;

isn't that setting it to a Yes? sorry just trying to understand your code.
Originally I just cut your code from the top of this thread.

It then occurred to me that I might, indeed be setting the variable to YES

So I attempted
if ( $sm4analysis->z2->ViewValue = "Yes" ) {
      $yescounter++;
}
echo $yescounter;
?>

That did not work either
i had a look through your whole code, and the only reference i can find a "no" assigned is here. should we be doing something like this?


if ($sm4analysis->RowType == EW_ROWTYPE_VIEW) { // View row
 
		// 1
		if (!is_null($sm4analysis->z1->CurrentValue)) {
			switch ($sm4analysis->z1->CurrentValue) {
				case "1":
					$sm4analysis->z1->ViewValue = "Yes";
$yescounter++;
					break;
				case "0":
					$sm4analysis->z1->ViewValue = "No";
					break;
				default:
					$sm4analysis->z1->ViewValue = $sm4analysis->z1->CurrentValue;
			}
		} else {
			$sm4analysis->z1->ViewValue = NULL;
		}
		$sm4analysis->z1->CssStyle = "";
		$sm4analysis->z1->CssClass = "";
		$sm4analysis->z1->ViewCustomAttributes = "";
 
		// 2
		if (!is_null($sm4analysis->z2->CurrentValue)) {
			switch ($sm4analysis->z2->CurrentValue) {
				case "1":
					$sm4analysis->z2->ViewValue = "Yes";
$yescounter++;
					break;
				case "0":
					$sm4analysis->z2->ViewValue = "No";
					break;
				default:
					$sm4analysis->z2->ViewValue = $sm4analysis->z2->CurrentValue;
			}
		} else {
			$sm4analysis->z2->ViewValue = NULL;
		}
		$sm4analysis->z2->CssStyle = "";
		$sm4analysis->z2->CssClass = "";
		$sm4analysis->z2->ViewCustomAttributes = "";
 
		// 3
		if (!is_null($sm4analysis->z3->CurrentValue)) {
			switch ($sm4analysis->z3->CurrentValue) {
				case "1":
					$sm4analysis->z3->ViewValue = "yes";
$yescounter++;
					break;
				case "0":
					$sm4analysis->z3->ViewValue = "No";
					break;
				default:
					$sm4analysis->z3->ViewValue = $sm4analysis->z3->CurrentValue;
			}
		} else {
			$sm4analysis->z3->ViewValue = NULL;
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My PC had crashed...sorry for the delay.
However... I cannot get this to work.
Will try more later.