Jump to content

Barand

Moderators
  • Posts

    24,337
  • Joined

  • Last visited

  • Days Won

    795

Everything posted by Barand

  1. The only reference I found was $time = date('h:i:a') in this thread Are you still storing your times as "08:15:pm" despite being told not to?
  2. 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)
  3. … And, for the benefit of those of us who can't see the report, how is that?
  4. How is the time stored in your table?
  5. Are you sure you know the name of the file? (Use the code <> button in the toolbar when posting code) EDIT: Stop using "@" to suppress errors - you want to know when things have gone wrong
  6. Are you trying to add a new user to the table or update an existing user's data? Either way, it's wrong. The query as it is would set every record to have identical contents as there is no WHERE clause to limit it to update a particular record. If you want add a new user you need an INSERT query. You aren't doing any checking to confirm the query did actually work, you just assume it did and output a success message. Easiest way to check for errors is to add the following line of code just before the line that creates your mysqli connection... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
  7. A variable is "empty" if it doesn't exist so the code will avoid "undefined index" notices. It's also empty if it is null, so the second condition is superfluous. Adding var_dump($_SESSION['product']) gives the expected error but not that line. However, contrary to makamo66's experience, it does print "else test" so the problem is probably caused elsewhere in the code.
  8. 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>&nbsp;</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>
  9. However If you are going to restructure your array then IMHO it would make more sense if you just had ... Array ( [A] => Array ( [1] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 ) [2] => Array ( [2] => 0 [3] => 0 [4] => 0 ) ) … where you can access a seat value with $val = $srs[$section][$row][$seat];
  10. I've just written a convert_array() function that uses that easier method $output = convert_array($srs); function convert_array($arr) { $new = []; foreach ($arr as $rec) { $new['sections'][$rec['section_name']]['rows'][$rec['row_name']]['seats'][] = $rec['seat_name']; } return $new; }
  11. Changing the column will not necesarily solve your problem. There is nothing in the table structure (a decimal(15,2) column) that would round 1.5 to 1, so my guess is somewhere in your code is rounding it to an integer value.
  12. I can't see any DB update/insert code in there either.
  13. You really expect me to scroll through all that and count 400+ lines?
  14. In that mass of code, I can not find anywhere that puts a value into "products_sort_order" in your table. (There is a hidden field with a comment that says it is always zero)
  15. Perhaps you want something like this $srs = []; foreach (range('A', 'D') as $section) { $srs['sections'][$section] = ['rows' => []]; for ($row = 1; $row < 3; $row++) { $srs['sections'][$section]['rows'][$row] = ['seats' => []]; for ($seat = 1; $seat < 4; $seat++) { $srs['sections'][$section]['rows'][$row]['seats'][] = $seat; } } } echo '<pre>', print_r($srs, 1), '</pre>';
  16. I see no such field in the table. What's the table like that contains the field you are having the problem with. FYI - next time you post code, use the code <> button in the tool bar
  17. That is telling you it is an array with zero elements, for example $a = [1,3,9]; var_dump($a); //--> array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(9) }
  18. In the absence of a crystal ball you will need to show us your current table structure.
  19. echo json_encode($srs); If that's not what you want then please provide an example of what you want to end up with.
  20. HTML? JavaScript? CSS? I was referring to web languages in general and the interaction between them - you seem to have conned your current employer into believing you know what you're doing. 😉
  21. Considering your lack of knowledge, please pass on my deepest sympathies to your clients.
  22. P.S. If you are talking about correcting your href in the other thread then all I ever saw was a variable called $col. There was no way I could know what was in it.
  23. I was thinking you were using city = google.loader.ClientLocation.address.city which just shows location of current user (or user's proxy) If anyone ever contacts it. It hasn't happened yet. I was thinking of contacting the Guiness Book of Records as the world's least visited site.
  24. Wondering why he would want to click on a link to see where he is.
×
×
  • 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.