Jump to content

Barand

Moderators
  • Posts

    24,612
  • Joined

  • Last visited

  • Days Won

    834

Everything posted by Barand

  1. A single SQL query can do all the work for you, no need to mess with arrays. A potential problem is that there may be weeks with credits and no debits and vice versa. A way round this is to use a table subquery with a union to gather all the data, then total that subquery. Suppose you have this data CREDIT table DEBIT table +-----------+------------+--------+ +----------+------------+--------+ | credit_id | cr_date | amount | | debit_id | db_date | amount | +-----------+------------+--------+ +----------+------------+--------+ | 1 | 2015-12-25 | 50.00 | | 1 | 2015-12-15 | 30.00 | | 2 | 2015-12-26 | 40.00 | | 2 | 2015-12-15 | 35.00 | | 3 | 2015-12-27 | 60.00 | | 3 | 2015-12-22 | 55.00 | | 4 | 2015-12-28 | 20.00 | | 4 | 2015-12-30 | 30.00 | | 5 | 2016-01-04 | 30.00 | | 5 | 2016-01-05 | 25.00 | | 6 | 2016-01-05 | 45.00 | | 6 | 2016-01-06 | 50.00 | | 7 | 2016-01-06 | 55.00 | | 7 | 2016-01-07 | 45.00 | | 8 | 2016-01-11 | 25.00 | | 8 | 2016-01-12 | 20.00 | | 9 | 2016-01-13 | 70.00 | | 9 | 2016-01-14 | 65.00 | | 10 | 2016-01-18 | 65.00 | | 10 | 2016-01-16 | 70.00 | | 11 | 2016-01-21 | 55.00 | | 11 | 2016-01-17 | 30.00 | | 12 | 2016-01-25 | 50.00 | | 12 | 2016-01-30 | 80.00 | +-----------+------------+--------+ +----------+------------+--------+ then SELECT DATE_FORMAT(date,'%x') as yr , DATE_FORMAT(date,'%v') as wk , MIN(date - INTERVAL WEEKDAY(date) DAY) as startdate , SUM(credit) as credit , SUM(debit) as debit , SUM(credit - debit) as diff FROM ( SELECT cr_date as date , amount as credit , 0 as debit FROM credit UNION ALL SELECT db_date as date , 0 as credit , amount as debit FROM debit ) data GROUP BY yr,wk; gives you +------+------+------------+--------+--------+--------+ | yr | wk | startdate | credit | debit | diff | +------+------+------------+--------+--------+--------+ | 2015 | 51 | 2015-12-14 | 0.00 | 65.00 | -65.00 | | 2015 | 52 | 2015-12-21 | 150.00 | 55.00 | 95.00 | | 2015 | 53 | 2015-12-28 | 20.00 | 30.00 | -10.00 | | 2016 | 01 | 2016-01-04 | 130.00 | 120.00 | 10.00 | | 2016 | 02 | 2016-01-11 | 95.00 | 185.00 | -90.00 | | 2016 | 03 | 2016-01-18 | 120.00 | 0.00 | 120.00 | | 2016 | 04 | 2016-01-25 | 50.00 | 80.00 | -30.00 | +------+------+------------+--------+--------+--------+ Job done.
  2. You don't want the "form=" after the "?".
  3. Are the debit and credit items from a database?
  4. OTOH an ini file is easier for the client to maintain manually (php.ini for example)
  5. If you have the data in a database, sort it when you retrieve the date using an ORDER BY clause. Otherwise, don't use variables like $JOBDISPLAY10, $JOBDISPLAY11, use an array and sort the array. EG <?php $jobdisplay = [ 10 => [ 'title' => 'Job title 10', 'company' => 'Company name 10', 'country' => 'Somewhere', 'start' => '2016-05-01', 'finish' => 'present', 'duties' => 'description of duties ...' ], 11 => [ 'title' => 'Job title 11', 'company' => 'Company name 11', 'country' => 'Somewhere else', 'start' => '2014-01-01', 'finish' => '2014-12-31', 'duties' => 'another description of duties ...' ], 12 => [ 'title' => 'Job title 12', 'company' => 'Company name 12', 'country' => 'Somewhere', 'start' => '2016-04-01', 'finish' => 'present', 'duties' => 'yet another description of duties ...' ], 13 => [ 'title' => 'Job title 13', 'company' => 'Company name 13', 'country' => 'Somewhere other', 'start' => '2015-01-01', 'finish' => '2016-03-31', 'duties' => 'and yet another description of duties ...' ] ]; function customSort($a, $b) { $finA = $a['finish']=='present' ? date('Y-m-d') : $a['finish']; $finB = $b['finish']=='present' ? date('Y-m-d') : $b['finish']; $x = strcmp($finB, $finA); // if same finish dates, sort by start date if ($x==0) { return strcmp($b['start'], $a['start']); } return $x; } usort($jobdisplay, 'customSort'); echo '<pre>'; foreach ($jobdisplay as $j) { printf('%-20s | %-15s | %-15s<br>', $j['title'], $j['start'], $j['finish']); } echo '</pre>'; ? Gives Job title 10 | 2016-05-01 | present Job title 12 | 2016-04-01 | present Job title 13 | 2015-01-01 | 2016-03-31 Job title 11 | 2014-01-01 | 2014-12-31
  6. Have error_reporting ON in both environments In development, have display_errors ON In production, have log_errors ON instead.
  7. Use a hidden field in the form for the goalie value form.php <?php $goalie = 'someCalculatedValue'; // calculate the goalie value ?> <html> <body> <form method="post" action="penalty.php"> <input type="hidden" name="goalie1" value="<?=$goalie?>"> <!-- store the goalie value in a hidden field --> <input type="checkbox" name="angle" value="1"> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> penalty.php <?php $goalie = $_POST['goalie']; $angle = $_POST['angle'] ... ?>
  8. You will need to process the query results differently if you are not using PDO. Try <?php $sqlsn = "SELECT system_name FROM tbl_sub_systems WHERE section=\"21a\""; $rssn = mssql_query( $sqlsn, $conn) or die ("Cannot execute"); // // read data into an array first // $rows = []; while ($row = mssql_fetch_row($rssn)) { $rows[] = $row[0]; } // // now chunk this array // $data = array_chunk($rows,2); echo "<table border=\"1\">"; foreach ($data as $row) { echo "<tr>"; if (count($row)==2) { foreach ($row as $n) echo "<td>".$n."</td>"; } else { echo "<td colspan=\"2\">".$row[0]."</td>"; } echo "</tr>"; } echo "</table>"; ?>
  9. Am I right in my assumption that you are selecting as single field EG SELECT name FROM tablename; If, say, there are five rows then, instead of this +--------+ | name 1 | +--------+ | name 2 | +--------+ | name 3 | +--------+ | name 4 | +--------+ | name 5 | +--------+ you want +--------+--------+ | name 1 | name 2 | +--------+--------+ | name 3 | name 4 | +--------+--------+ | name 5 | +-----------------+ If so, I'd use array_chunk to put the data in pairs $sql = "SELECT CONCAT(fname,' ',lname) as name FROM pupil"; $res = $pdo->query($sql) ; $data = array_chunk($res->fetchAll(), 2); echo "<table border='1'>"; foreach ($data as $row) { echo "<tr>"; if (count($row)==2) { foreach ($row as $n) echo "<td>{$n['name']}</td>"; } else echo "<td colspan='2'>{$row[0]['name']}</td>"; echo "</tr>\n"; } echo "</table>";
  10. As it clearly states at the top of the page, "PHPFreaks.com Questions, Comments, & Suggestions" is not a help forum but a forum for comments about the site. Moving to PHP Coding Help forum. Please put you code inside [ code ] tags or use the <> button in the toolbar. And state the nature of the error, preferably the full error message. Just saying there is an error is not helpful.
  11. You will need bcmath library to do it in php $x = (37.739658 + 180) / 360; $xtile = bcmul($x, bcpow(2,3200), 0); echo $xtile; giving 1195697057310125832371140711633564800180786038375872658403318152095581962252386539444192207669999 4207959832759648879404409081079451419565470276854106090269574245270335849760856131899854524127642 7324964717602398404052293164714217486036187584311373189602241206357140904647698252514706029297316 9903133287668377476541891044796672937490793296463688346149780776864963757079645918149252840838799 6339961375594565001261410980667493879041252370996632123897458999476098059823194577935019128254436 6030860257855571048100823754022659657970273909948380147791379123616894368709592470891060276597854 8271325003793426334823472642940015323876156075156400473671902771191374590935488622949583607624016 9866685125773087182548515916284210254502245372017267188638444500873324474387826190977942219959682 1489421060559178529999373973134461270691821511678419523717048252856218976237026885921242952914162 8090268067144283996195298887019205345498122356598595705782092401101204314360135493325755304
  12. Check out the MySql manual for storage requirements http://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html
  13. How can we advise on how to join unknown tables? You have given us no information about your current table table structure and column names so how can we advise on how to join them. You haven't even given your current query .
  14. A captain is just another player, so why a separate table to complicate things. Just add a flag in the player table to indicate the captain. EG +--------------+ | team | +--------------+ | team_id (PK) |---+ +----------------+ +---------------+ | teamname | | | player | | shirt | +--------------+ | +----------------+ +---------------+ | | player_id (PK) |---+ +---| shirt_id (PK) | +--<| team_id | | | | description | | playername | | | +---------------+ | captain | | | +----------------+ | +----------------+ | | | player_shirt | | | +----------------+ | +--<| player_id (PK) | | | shirt_id (PK) |>--+ +----------------+ Note, the above will allow for multiple shirts per player. If there is only ever one each then lose the player_shirt table and put the shirt_id in the player table.
  15. http://uk1.php.net/manual/en/intro.pdo.php
  16. see http://dev.mysql.com/doc/refman/5.7/en/gone-away.html
  17. Escaping double quotes was shown to you back in January! Read the replies. http://forums.phpfreaks.com/topic/300694-mysql-full-text-search/?do=findComment&comment=1530578
  18. You could do this (where you enter AB21 1AB, say) SELECT Pcode , Latitude , Longitude FROM postcodes WHERE 'AB21 1AB' LIKE CONCAT(Pcode,'%') ; BUT there is a more efficient way. Put an index on Pcode (primary key?) and set its type to VARCHAR. A TEXT type can hold a novel and is overkill for 4 characters. Then, as the final part of a postcode is always 3 characters, remove those and trim off any space and search for the remaining Pcode = 'AB21'
  19. A confusing mix of JOIN syntaxes in that query. Any chance of a test dump of those three tables?
  20. you'll find answers on these two pages of the PHP manual http://uk1.php.net/manual/en/language.types.array.php http://uk1.php.net/manual/en/language.types.object.php
  21. Good idea! Do it that way then.
  22. If you get a duplicate key error , tell the user.
  23. To prevent duplicate values, put a UNIQUE index on the field. Use "ORDER BY" in the query to sort by the field.
  24. You are missing the [] after $json $json[] = array('a'=>$a, 'b'=>$b, 'c'=>$c, 'd'=>$d, 'e'=>$e, 'f'=>$f, 'g'=>$g, 'h'=>$h, 'i'=>$i);
  25. or you can escape the double quotes echo "\"$foo\"";
×
×
  • 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.