RI1 Posted October 12, 2019 Share Posted October 12, 2019 The submit button won't validate all data and save the data to the necessary arrays for processing and Total Households Surveyed will process the arrays and display the total households surveyed by state and then by county via HTML, Average Household Income will process the arrays and display the average household income by state and then by county via HTML, and Percentage Below Poverty will process the arrays and display the Percentage Below Poverty for each state and their counties via HTML.Poverty is based on Household of 1 and less than $12,000, Household of 2 and less than $18,000,Household of 3 and less than $25,000,Household of 4 and less than $30,000, Household of 5 or more and less than $40,000 this is my code: <?php $DateofSurvey = $_POST["Date of Survey"]; $CountyState = $_POST["County State"]; $NumberinHousehold = $_POST["Number in Household"]; $HouseholdyearlyIncome = $_POST["Household yearly Income"]; session_start(); value="<?PHP print$HouseholdyearlyIncome;?>" $_SESSION['Submit']=array(); array([StateWithCounty]=> array([0]=>'Hamilton, Oh',[1]=>'Butler, Oh',[2]=>'Boone, Ky',[3]=>'Kenton, Ky')); $Submit=count(StateWithCounty); for( $i=0; $i<$Submit ; $i++){ $TotalHouseholdsSurveyed += $Submit } $Submit=count(StateWithCounty); for( $i=0; $i<$Submit ; $i++){ $AverageHouseholdsIncome += $HouseholdyearlyIncome/$Submit } $Submit=count(StateWithCounty); for( $i=0; $i<$Submit ; $i++){ if (NumberinHousehold = 1 and HouseholdyearlyIncome < 12000 ){ $Submit=100% } elseif(NumberinHousehold = 2 and HouseholdyearlyIncome < 18000){ $Submit=100% } elseif(NumberinHousehold = 3 and HouseholdyearlyIncome < 25000){ $Submit=100% } elseif(NumberinHousehold = 4 and HouseholdyearlyIncome < 30000){ $Submit=100% } else{ $Submit=100% }} Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/ Share on other sites More sharing options...
requinix Posted October 12, 2019 Share Posted October 12, 2019 Please post your actual code instead of... whatever that is. Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570543 Share on other sites More sharing options...
RI1 Posted October 13, 2019 Author Share Posted October 13, 2019 I'm still a beginner. That is my code. Only comments I need is the ones that are gone tell me how to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570548 Share on other sites More sharing options...
requinix Posted October 13, 2019 Share Posted October 13, 2019 The code you posted is full of syntax errors that would prevent PHP from executing it at all. Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570549 Share on other sites More sharing options...
RI1 Posted October 13, 2019 Author Share Posted October 13, 2019 This is the same problem just done in vb.net and I'm trying to do it in PHP: Option Strict On Public Class Form1 Dim strState() As String = {"Hamilton, Oh", "Butler, Oh", "Clermont, Oh", " Warren, Oh", "Campbell, Ky", "Boone, Ky", "Kenton, Ky"} ' selecting from combo box from these Dim dblIncomes() As Double = {0, 0, 0, 0, 0, 0, 0} Dim intCount() As Integer = {0, 0, 0, 0, 0, 0, 0} Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click Dim strCounty As String Dim dblIncome As Double Dim valid As Boolean = True txtIncome.BackColor = Color.White Try strCounty = cboBox.SelectedItem.ToString 'Validation If Validation(txtIncome.Text, dblIncome) = True Then 'loop through the array to get input from each state For i = 0 To strState.Length - 1 If strState(i) = strCounty Then dblIncomes(i) = dblIncomes(i) + dblIncome intCount(i) = intCount(i) + 1 End If Next cboBox.SelectedItem = "" ' Clear txtbox for income txtIncome.Clear() End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Public Function Validation(ByVal Income As String, ByRef IncomeNumber As Double) As Boolean If IsNumeric(Income) Then IncomeNumber = CDbl(Income) Else MessageBox.Show("Please enter numbers only and greater than 0 ") ' they aren't entering number or they have 0 for income txtIncome.BackColor = Color.Yellow txtIncome.Focus() Return False End If If cboBox.SelectedIndex = -1 Then MessageBox.Show("Please select a State.") ' they aren't selecting state Return False End If Return True End Function Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click 'Close program Close() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'load the combo box items collection using the state in array For i = 0 To strState.Length - 1 cboBox.Items.Add(strState(i)) Next End Sub Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click Dim dblOhioAverage As Double Dim dblKentuckyAverage As Double Dim intOhioTotal As Integer Dim intKentuckyTotal As Integer ' Loop the total households surveyed For i = 0 To strState.Length - 1 Select Case strState(i) Case "Hamilton, Oh" dblOhioAverage += dblIncomes(i) intOhioTotal += intCount(i) Case "Butler, Oh" dblOhioAverage += dblIncomes(i) intOhioTotal += intCount(i) Case "Clermont, Oh" dblOhioAverage += dblIncomes(i) intOhioTotal += intCount(i) Case "Warren, Oh" dblOhioAverage += dblIncomes(i) intOhioTotal += intCount(i) Case "Campbell, Ky" dblKentuckyAverage += dblIncomes(i) intKentuckyTotal += intCount(i) Case "Boone, Ky" dblKentuckyAverage += dblIncomes(i) intKentuckyTotal += intCount(i) Case "Kenton, Ky" dblKentuckyAverage += dblIncomes(i) intKentuckyTotal += intCount(i) End Select Next 'Display Total MessageBox.Show("Ohio:" & vbTab & vbTab & FormatCurrency(dblOhioAverage / intOhioTotal) & vbNewLine & " Hamilton:" & vbTab & FormatCurrency(dblIncomes(0)) & vbNewLine & " Butler:" & vbTab & vbTab & FormatCurrency(dblIncomes(1)) & vbNewLine & " Clermont:" & vbTab & FormatCurrency(dblIncomes(2)) & vbNewLine & " Warren:" & vbTab & vbTab & FormatCurrency(dblIncomes(3)) & vbNewLine & "Kentucky:" & vbTab & FormatCurrency(dblKentuckyAverage / intKentuckyTotal) & vbNewLine & " Boone:" & vbTab & vbTab & FormatCurrency(dblIncomes(5)) & vbNewLine & " Campbell:" & vbTab & FormatCurrency(dblIncomes(4)) & vbNewLine & " Kenton:" & vbTab & vbTab & FormatCurrency(dblIncomes(6))) End Sub Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click Dim intOhioTotal As Integer Dim intKentuckyTotal As Integer ' Loop the total households surveyed For i = 0 To strState.Length - 1 Select Case strState(i) Case "Hamilton, Oh" intOhioTotal += intCount(i) Case "Butler, Oh" intOhioTotal += intCount(i) Case "Clermont, Oh" intOhioTotal += intCount(i) Case "Warren, Oh" intOhioTotal += intCount(i) Case "Campbell, Ky" intKentuckyTotal += intCount(i) Case "Boone, Ky" intKentuckyTotal += intCount(i) Case "Kenton, Ky" intKentuckyTotal += intCount(i) End Select Next 'Display Total MessageBox.Show("Ohio:" & vbTab & vbTab & intOhioTotal & vbNewLine & " Hamilton:" & vbTab & intCount(0) & vbNewLine & " Butler:" & vbTab & vbTab & intCount(1) & vbNewLine & " Clermont:" & vbTab & intCount(2) & vbNewLine & " Warren:" & vbTab & vbTab & intCount(3) & vbNewLine & "Kentucky:" & vbTab & intKentuckyTotal & vbNewLine & " Boone:" & vbTab & vbTab & intCount(5) & vbNewLine & " Campbell:" & vbTab & intCount(4) & vbNewLine & " Kenton:" & vbTab & vbTab & intCount(6)) End Sub End Class Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570550 Share on other sites More sharing options...
requinix Posted October 13, 2019 Share Posted October 13, 2019 The PHP you showed and that WebForms-based code are completely different... How about this: what is the HTML form you need to support, and how are you going to present the results to the user? Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570551 Share on other sites More sharing options...
RI1 Posted October 13, 2019 Author Share Posted October 13, 2019 There are 4 HTML for this.I think I would need a PHP for each one of them? The main one is : <!DOCTYPE html> <html> <head> <meta > <title> Project1.html </title> <link rel="stylesheet" href="Project 1 WEB_RI.CSS"> <script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6/html5shiv.min.js"></script> </head> <body> <h3>Data Collected: </h3> <form method="post"action="Project 1 WEB_RI.PHP "> <table border="1"> <tr > <td rowspan="5" colspan = "2"><h3 style="color:black" ; align="center"> <button style="width:50%" type="submit" name= "Submit" Value= "Save Change">Submit</button> <br><br> <button style="width:50%" type="reset" name= "Reset" Value= "Reset">Reset</button><br><br> <a href="http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Poverty.html">Percentage Below Poverty</a><br><br> <a href=" http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Average.html">Average Household Income</a><br><br> <a href="http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Total.html">Total Households Surveyed</a> </td> <td> <label class="three" for="fSurvey"><span>Date of Survey:*</span></label><br> <input colspan= "4" type="date" width="150" id="fSurvey" name="Survey" placeholder="Date of Survey.."></tr> </td> <tr><td> <label class="three" for="fCS"><span>County and State:*</span></label><br> <select County State ="selCS" name="CS" width="150"> <option selected= "Selected" value=""> Select County, State</option> <option value= "Hamilton, OH">Hamilton, OH</option> <option value= "Bulter, OH">Bulter, OH</option> <option value= "Boone, KY">Boone, KY</option> <option value= "Kenton, KY">Kenton, KY</option> </select> </td></tr> <tr><td> <label class="three" for="fHousehold"><span>Number in Household:*</span></label><br> <input colspan= "4" type="number" min="1" width="150" id="fHousehold" name="Household" placeholder="Number of Household.."></tr> </td> </td></tr> <tr><td> <label class="three" for="fyearlyIncome"><span>Household yearly Income:*</span></label><br> <input colspan= "4" type="number" min="1" width="150" id="fyearlyIncome" name="yearlyIncome" placeholder="Household yearly Income.."></tr> </td> </td></tr> <tr> <td class="three" border="thin single black collapse" width="150" colspan= "2" align="right" > <span>*Required Field</span><br> </td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570560 Share on other sites More sharing options...
RI1 Posted October 13, 2019 Author Share Posted October 13, 2019 The results would be presented to the user in different pages for each Total, Average, and Below Poverty: Average Household Income Ohio: $30,000 Hamilton: $40,000 Butler: $20,000 Kentucky: $35,000 Boone: $40,000 Kenton: $35,000 Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570562 Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 I'm surprised you don't appear store the data anywhere, except maybe in SESSION, so you have to reenter all the data each time you want to view the analyses. And why 4 pages and not just Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570564 Share on other sites More sharing options...
requinix Posted October 13, 2019 Share Posted October 13, 2019 Yeah, something more needs to change here. The form only supports one entry at a time. To support more than one, you'll have to (a) store each entry somewhere, best choice being a database, and then have the user hit some sort of Finish button so they can view the results, or (b) alter the form so that the user can enter more than one at a time. The second option is probably better: the user does all the work on one page, and when they submit you can show the results immediately. What do you want to do? Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570606 Share on other sites More sharing options...
RI1 Posted October 16, 2019 Author Share Posted October 16, 2019 we still aren't connecting to the data base, but this is what I have so far and I think my biggest issue is making the submit button save the answers when they enter input save it then display what ever they choose(Total, Average, below poverty): <html> <head> <title> Project1.html </title> <link rel="stylesheet" href="Project 1 WEB_RI.CSS"> </head> <body> <h3>Data Collected: </h3> <form action="Project 1 WEB_RI.PHP " method="POST"> <table border="1"> <tr><td> <div> <a href="Project 1 WEB_RI.html">Home</a> <a href="http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Total.html">Total Households Surveyed</a> <a href=" http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Average.html">Average Household Income</a> <a href="http://itd1.cincinnatistate.edu/PHP-IbrahimR/Content/Week6/RI_Project1Poverty.html">Percentage Below Poverty</a> </div> </td></tr> <tr><td> <label>Date of Survey:</label> <input type="date" name="surveyDate" value="2019-01-01" required /> <br /> </td></tr> <tr><td> <label>County and State: </label> <select name="countyState" required> <option>Hamilton, Oh</option> <option>Butler, Oh</option> <option>Boone, Ky</option> <option>Kenton, Ky</option> </select> <br /> </td></tr> <tr><td> <label>Number in Household: </label> <input type="number" name="noOfHouseHold" min="1" required /> <br /> </td></tr> <tr><td> <label>The household yearly income: </label> <input type="number" name="yearlyIncome" min="1" required /> <br /> </td></tr> <tr><td> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </td></tr> </form> </table> </body> </html> <?php session_start(); if(!isset($_SESSION['county']) || !isset($_SESSION['state']) || !isset($_SESSION['noOfHousehold']) || !isset($_SESSION['yearlyIncome'])){ return; } //census_process.php <?php session_start(); if(!isset($_SESSION['noOfSurveys'])){ $_SESSION['noOfSurveys']=0; } $index = $_SESSION['noOfSurveys']; $surveyDate = $_POST['surveyDate']; $countyState = explode(",", $_POST['countyState']); $noOfHousehold = $_POST['noOfHouseHold']; $yearlyIncome = $_POST['yearlyIncome']; if(!isset($_SESSION['county'])){ $_SESSION['county'] = array(); } $_SESSION['county'][$index] = $countyState[1]; if(!isset($_SESSION['state'])){ $_SESSION['state'] = array(); } $_SESSION['state'][$index] = $countyState[0]; if(!isset($_SESSION['noOfHousehold'])){ $_SESSION['noOfHousehold'] = array(); } $_SESSION['noOfHousehold'][$index] = $noOfHousehold; if(!isset($_SESSION['yearlyIncome'])){ $_SESSION['yearlyIncome'] = array(); } $_SESSION['yearlyIncome'][$index] = $yearlyIncome; $_SESSION['noOfSurveys']++; header("Location: Project 1 WEB_RI.html"); <?php include_once('.php'); $incomeByState = array(); $incomeByCountry = array(); for($i=0;$i<$_SESSION['noOfSurveys'];$i++){ if(!array_key_exists($_SESSION['country'][$i], $incomeByCountry)){ $incomeByCountry[$_SESSION['country'][$i]] = array(); } if(!array_key_exists($_SESSION['state'][$i], $incomeByState)){ $incomeByState[$_SESSION['state'][$i]] = array(); } array_push($incomeByCountry[$_SESSION['country'][$i]], $_SESSION['yearlyIncome'][$i]); array_push($incomeByState[$_SESSION['state'][$i]], $_SESSION['yearlyIncome'][$i]); } echo "Average household income by Country: <br/>"; foreach($incomeByCountry as $key => $value){ $avg = array_sum($value) / count($value); echo $key.": $".$avg."<br/>"; } echo "<br/> Average household income by State: <br/>"; foreach($incomeByState as $key => $value){ $avg = array_sum($value) / count($value); echo $key.": $".$avg."<br/>"; } <?php include_once("head.php"); $noOfSurveysByState = array_count_values($_SESSION['state']); $noOfSurveysByCountry = array_count_values($_SESSION['country']); echo "No of survery by State: <br/>"; foreach ($noOfSurveysByState as $key => $value) { echo $key.": ".$value."<br/>"; } echo "<br/><br/>No of survery by Country: <br/>"; foreach ($noOfSurveysByCountry as $key => $value) { echo $key.": ".$value."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570716 Share on other sites More sharing options...
requinix Posted October 16, 2019 Share Posted October 16, 2019 I can't tell what code is in what file. Try this with a post: Put the filename on a line, hit the Code <> button and put the file's code in there, and repeat for the other files. That way everything is clean and separate and has syntax highlighting and doesn't force me to scroll so much. Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570717 Share on other sites More sharing options...
Barand Posted October 17, 2019 Share Posted October 17, 2019 (edited) This is my version of your script. Data is stored in "census.csv". The analysis results are dynamically updated as each new record id entered <?php const DATAFILE = 'census.csv'; $counties = ["Hamilton, Oh", "Butler, Oh", "Clermont, Oh", "Warren, Oh", "Campbell, Ky", "Boone, Ky", "Kenton, Ky"]; // defaults $hhcounty = ''; $hhsize = 1; $hhincome = 0; $errors = []; $messages = ''; $analysis = ''; // // HAS FORM BEEN POSTED // if ($_SERVER['REQUEST_METHOD']=='POST') { $hhcounty = $_POST['hhcounty'] ?? ''; $hhincome = $_POST['hhincome'] ?? 0; $hhsize = $_POST['hhsize'] ?? 0; if (!in_array($hhcounty, $counties)) { $errors[] = "Not a valid county/state"; } if (!ctype_digit($hhsize) || $hhsize==0) { $errors[] = "Not a valid household size"; } if (!ctype_digit($hhincome)) { $errors[] = "Not a valid income amount"; } if ($errors) { foreach ($errors as $e) { $messages .= "<p class='err'>$e</p>\n" ; } } else { // data OK - save in csv file // (at this point we'd normally save to a database, but we don't have one) $fp = fopen(DATAFILE, 'a'); $rec = [$hhcounty, $hhsize, $hhincome ]; fputcsv($fp, $rec); fclose($fp); header("Location: #"); exit; } } // // analyse data // $data = []; if (file_exists(DATAFILE)) { $fp = fopen(DATAFILE, 'r'); while (list($hhc, $hhs, $hhi) = fgetcsv($fp)) { $data[$hhc][] = [ 'size' => $hhs, 'income' => $hhi]; } fclose($fp); } uksort($data, function ($a, $b) { $x = substr($b, -2) <=> substr($a, -2); // state DESC if ($x==0) return ($a <=> $b); // county ASC return $x; }); foreach ($data as $cty => $households) { $tothh = count($households); $totpov = count(array_filter($households, function ($v) { return isPoverty($v['size'], $v['income']); })); $pcpov = number_format($totpov*100/$tothh, 2) . ' %'; $avincome = array_sum(array_column($households, 'income'))/$tothh; $avsize = array_sum(array_column($households, 'size'))/$tothh; $avgi = number_format($avincome, 0); $avgsz = number_format($avsize, 2); $analysis .= "<tr><td style='text-align: left'>$cty</td><td>$tothh</td><td>$avgsz</td><td>$avgi</td><td>$totpov</td><td>$pcpov</td></tr>\n" ; } /** * compare income agains threshold values for household size * * @param int $num - household size * @param int $inc - income amount * @return boolean true if below threshold */ function isPoverty($num, $inc) { $thresholds=[ 1 => 12000, 2 => 18000, 3 => 25000, 4 => 30000, 5 => 40000 ]; if ($num > 5) $num = 5; return $inc < $thresholds[$num]; } /** * build conty/state option menu * * @param array $counties array of values * @param string $current current value */ function countyOptions(&$counties, $current) { $opts .= "<option value=''>- select county, state -</option>\n"; foreach ($counties as $c) { $sel = $c==$current ? 'selected' : ''; $opts .= "<option $sel>$c</option>\n"; } return $opts; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Household Survey</title> <meta name="creation-date" content="10/13/2019"> <style type="text/css"> body { font-family: verdana, sans-serif; font-size: 11pt;} label { display: inline-block; width: 200px; font-weight: 600; padding: 8px;} fieldset{ padding: 16px; margin: 20px 50px;} .err { font-style: italic; color: #F00; } table { width: 80%; border-collapse: collapse; margin: 30px 50px; } th { padding: 8px; background-color: #396; color: #FFF; } td { padding: 4px 8px; text-align: right;} p { font-size: 9pt; font-style: italic;} </style> </head> <body> <h1>Household Survey</h1> <p>As you enter records the table of analysis results below is updated</p> <fieldset> <form method="post"> <label>County, State</label> <select name="hhcounty"><?=countyOptions($counties, $hhcounty)?></select><br> <label>Number in household</label> <input type="number" name="hhsize" value="<?=$hhsize?>" size="5"><br> <label>Household income</label> <input type="number" name="hhincome" value="<?=$hhincome?>" size="5"><br> <label> </label><input type="submit" name="btnSub" value="Submit"> </form> <?=$messages?> </fieldset> <h3>Analysis of Data</h3> <table> <tr><th>County/State</th><th>Households</th><th>Average Size</th><th>Average Income</th><th>Below Poverty Line</th><th>Percent Below</th></tr> <?=$analysis?> </table> </body> </html> Edited October 17, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570793 Share on other sites More sharing options...
RI1 Posted October 19, 2019 Author Share Posted October 19, 2019 Thank you. This code does submit and save, but the values won't show up in the table. Would I need to use java script so it can show up?? Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570823 Share on other sites More sharing options...
Barand Posted October 19, 2019 Share Posted October 19, 2019 It should function as posted (at least, it does for me). You should have a "census.csv" file that resembles this... "Hamilton, Oh",3,30000 "Hamilton, Oh",1,25000 "Butler, Oh",1,22000 "Butler, Oh",1,44000 "Clermont, Oh",1,65000 "Clermont, Oh",1,15000 "Clermont, Oh",2,11000 "Warren, Oh",3,20000 "Warren, Oh",1,100000 "Campbell, Ky",2,50000 "Kenton, Ky",8,20000 "Kenton, Ky",4,15000 "Kenton, Ky",4,18000 "Warren, Oh",1,70000 "Warren, Oh",2,100000 ...which gets processed and output by lines 51 -75 (Output table is stored in "$analysis" variable and output in the HTML section - 5 lines from the end) Quote Link to comment https://forums.phpfreaks.com/topic/309362-census-bureau/#findComment-1570827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.