-
Posts
24,573 -
Joined
-
Last visited
-
Days Won
824
Everything posted by Barand
-
I can't understand why more people don't do that instead of using free tools like Workbench.
-
$age = (new DateTime($dob))->diff(new DateTime())->y; should work with PHP 5.3 or later. What is in $dob? Has the format that is being passed in $dob changed?
-
Since the query is aggregating totals, individual gametimes are irrelevant, so why the useless ORDER BY? You should also note that the data is currently not limited to a specific season. I don't know what your method queryForColumn() returns but this looks a little odd to me $missingPreds = " " . $this->dbo->queryForColumn($query) . ")";
-
Similarly, for the last part SELECT userid , lastgame - lastprediction as missed FROM ( SELECT MAX(id) as lastgame FROM games ) games CROSS JOIN ( SELECT userid , MAX(gameid) as lastprediction FROM prediction GROUP BY userid ) users
-
You would use a subquery to combine EG SELECT p.userid , COUNT(*) as predictions , totgames - COUNT(*) as missing , totgames FROM prediction p CROSS JOIN ( SELECT COUNT(*) as totgames FROM games ) tot GROUP BY p.userid
-
It's a recursive solution I had both open at once and meant to link to this other one - with a very similar title. http://forums.phpfreaks.com/topic/292827-display-data-in-tabular-format-by-year-and-month/?do=findComment&comment=1498183
-
You could apply a method similar to this one http://forums.phpfreaks.com/topic/301314-displaying-data-in-tabular-form/?do=findComment&comment=1533481 I'd also start by tidying up the SQL code. You wouldn't write your php in one single line. $sql = "SELECT student_exam.id as sid , student_exam.student_id , student_exam.enroll_no , student_exam.exam_id , student_exam.subject_id , student_exam.obtained_marks , exam_time_table.exam_name , exam_time_table.class , exam_time_table.section , exam_time_table.total_marks , subjects.subject , exam_type.exam , student.enroll_no , student.stud_name FROM student_exam INNER JOIN exam_time_table ON student_exam.exam_id=exam_time_table.id INNER JOIN subjects ON student_exam.subject_id=subjects.sub_id INNER JOIN exam_type ON exam_time_table.exam_name=exam_type.eid INNER JOIN student ON student_exam.enroll_no=student.enroll_no WHERE exam_time_table.class='$class' AND exam_time_table.section='$section' AND student_exam.enroll_no='$enroll_no' ";
-
Display Distinct data with each related data
Barand replied to samuel_lopez's topic in PHP Coding Help
The method (1) proposed by Jacques is certainly simpler. OTOH, storing in an array (method 2) gives added flexibility. For example, the ability to count prior to output. Method 1 code $sql = "SELECT brand, category, product FROM product ORDER BY brand, category"; $res = $mysqli->query($sql); $prevbrand = ''; $prevcat = ''; $tdata1 = ''; while (list($brand,$cat,$prod) = $res->fetch_row()) { $cat_output = $brand_output = ''; if ($brand != $prevbrand) { $brand_output = $brand; $cat_output = $cat; } elseif ($cat != $prevcat) { $cat_output = $cat; } $prevbrand = $brand; $prevcat = $cat; $tdata1 .= "<tr><td>$brand_output</td><td>$cat_output</td><td>$prod</td></tr>\n"; } Method 2 code $sql = "SELECT brand, category, product FROM product"; $res = $mysqli->query($sql); while (list($brand,$cat,$prod) = $res->fetch_row()) { $data[$brand][$cat][] = $prod; } // prepare output table $tdata2=''; foreach ($data as $brand => $bdata) { $kb = 0; foreach ($bdata as $cdata) { $kb += count($cdata); } $firstb=1; foreach ($bdata as $cat => $cdata) { $kc = count($cdata); if ($firstb) { $tdata2 .= "<tr><td rowspan='$kb'>$brand</td>"; } $firstc=1; foreach ($cdata as $prod) { if ($firstc) { if (!$firstb) $tdata2 .= "<tr>"; $tdata2 .= "<td rowspan='$kc'>$cat</td>"; } if (!$firstb && !$firstc) $tdata .= "<tr>"; $tdata2 .= "<td>$prod</td></tr>\n"; $firstc = 0; } $firstb = 0; } } -
Yes. If they choose "videos" or "images" then you have SELECT ... WHERE type = :type If they select "All" then you just omit the WHERE clause
-
Display Distinct data with each related data
Barand replied to samuel_lopez's topic in PHP Coding Help
Firstly, don't run queries inside loops, use a single query SELECT brand, category, product FROM tblproducts Store the results in a multidimensional array, EG $data[brand][category] = [product1, product2] Now you can loop through the array to build your output table. -
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.
-
You don't want the "form=" after the "?".
-
Are the debit and credit items from a database?
-
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
-
Have error_reporting ON in both environments In development, have display_errors ON In production, have log_errors ON instead.
-
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'] ... ?>
-
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>"; ?>
-
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>";
-
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.
-
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
-
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 .
-
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.
-
http://uk1.php.net/manual/en/intro.pdo.php