Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Are you invoking magic or do you have a more mundane method of storing and acquiring the username?
  2. if ($link == '1'){ include ('page.php'); } elseif ($link == '1-1'){ include ('page.php'); } elseif ($link == '1-2'){ include ('page.php'); } else { include ('home.php'); } // alternatively switch ($link) { case '1': include ('page.php'); break; case '1-1': include ('page.php'); break; case '1-2': include ('page.php'); break; default: include ('home.php'); break; }
  3. Alternatively (where $hours is 1, 6 or 12), INSERT INTO mytable (date_added, expire_date) VALUES (NOW(), NOW()+INTERVAL $hours HOUR);
  4. First thing you should do is turn on error reporting so you don't try running code with syntax errors
  5. Just correcting a typo. mysql_set_charset() should be mysqli_set_charset()
  6. I think I see what you want now. You need to find the highest id associated with each topic and sort on that. $sql = "SELECT topic , description , topid FROM news INNER JOIN ( SELECT topic , MAX(id) as topid FROM news GROUP BY topic ) tid USING (topic) ORDER BY topid DESC, id ASC"; $res = $db->query($sql); $prevtopid = ''; while ($row = $res->fetch_assoc()) { if ($prevtopid != $row['topid']) { echo "<b>{$row['topic']}</b><br>"; $prevtopid = $row['topid']; } echo " - {$row['description']}<br>"; } Results barand - test - test 2 jogom - naaaaa moo - meee Hahaha - ???? Test Heading - Test - Hmmmm 100%!!! - Test description
  7. Exactly.
  8. Advanced Member, do it like this $show = "SELECT FirstName, LastName FROM Members"; $result2 = mysqli_query($Garydb,$show) or die("Could not show record"); echo "<table>"; if (mysqli_num_rows($result2) > 0) { while ($row = mysqli_fetch_assoc($result2) ) { echo "<tr><td>" . $row['FirstName']. " " . $row['LastName'] . "</td></tr>"; } } else { echo "<tr><td>No display record</td></tr></table>"; } echo "<table>"; After 40+ posts you should have found the code tags by now.
  9. ORDER BY topic, id DESC or, if you want topics descending too, ORDER BY topic DESC, id DESC
  10. You connect to the mysql database server, not to PHPMyAdmin. The error you are getting is nothing to do with the db connection. Either the file does not not exist at all or it is not in the same folder as your script. You need to specify the correct path. The second error is because output was sent by the first error message and headers can only be sent before any output is sent to the browser.
  11. Yes, the values come from the select query. Yours will work only if the structures of the tables are identical. If not, you need to specify the the destination and source columns, for example INSERT INTO table1 (col1, col3, col2) SELECT x, y, z FROM table2 WHERE ...
  12. try $file = '/var/www/html/Cute_browser/abc/xyz.pdf'; $shortPath = join('/', array_slice(explode('/', $file),-3)); echo $shortPath; //--> Cute_browser/abc/xyz.pdf
  13. prev = ''; while fetch next row if prev != topic output topic prev = topic endif output news endwhile
  14. This should do it SELECT ko_time , c.home , c.away , av_goals , meets FROM championship c LEFT JOIN ( SELECT teams , AVG(home_score + away_score) as av_goals , COUNT(*) as meets FROM ( SELECT home_score , away_score , @seq := IF(@prev <> CONCAT(LEAST(home,away),GREATEST(home,away)),1,@seq+1) as seq , @prev := CONCAT(LEAST(home,away),GREATEST(home,away)) as teams FROM championship JOIN (SELECT @prev:='', @seq:=0) init WHERE match_date < '2015-09-19' ORDER BY CONCAT(LEAST(home,away),GREATEST(home,away)), match_date DESC ) last5 WHERE seq < 6 GROUP BY teams ) H ON teams = CONCAT(LEAST(c.home,c.away),GREATEST(c.home,c.away)) WHERE match_date = '2015-09-19' ORDER BY c.home, c.away; +---------+--------------------+---------------+----------+-------+ | ko_time | home | away | av_goals | meets | +---------+--------------------+---------------+----------+-------+ | 15:00 | Blackburn | Charlton | 2.8000 | 5 | | 15:00 | Brentford | Preston | NULL | NULL | | 15:00 | Bristol City | Reading | NULL | NULL | | 15:00 | Huddersfield | Bolton | 1.4000 | 5 | | 15:00 | Hull City | QPR | NULL | NULL | | 15:00 | Milton Keynes Dons | Leeds | NULL | NULL | | 12:30 | Nottingham | Middlesbrough | 2.6000 | 5 | | 15:00 | Rotherham | Cardiff | 4.0000 | 1 | | 15:00 | Sheffield Wed | Fulham | 3.0000 | 2 | | 15:00 | Wolves | Brighton | 3.0000 | 4 | +---------+--------------------+---------------+----------+-------+ 10 rows in set (0.08 sec)
  15. He used === false which means it must be the same value and type (boolean false, not 0, or any of the others that evaluate to false)
  16. BTW, your query is wrong. LIMIT limits the number of rows output by the query, it doesn't limit the input to the latest 5. That's why I had to use the subqueries with @seq to get the latest 5.
  17. The only way I could think of to get the two averages is to join twice to the subquery containing the team average calculation SELECT c.ko_time , c.home , c.away , ROUND(h.average,1) as home_av , ROUND(a.average,1) as away_av FROM championship c INNER JOIN ( SELECT team , AVG(score) as average FROM ( SELECT score , venue , @seq:=IF(team<>@prev,1,@seq+1) as seq , @prev:=team as team FROM ( SELECT 'H' as venue , match_date , home as team , home_score as score FROM championship WHERE match_date < CURDATE() UNION ALL SELECT 'A' as venue , match_date , away as team , away_score as score FROM championship WHERE match_date < CURDATE() ORDER BY team, match_date DESC ) scores JOIN (SELECT @prev:='', @seq=0) init ) av WHERE seq < 6 GROUP BY team ) h ON h.team = c.home INNER JOIN ( SELECT team , AVG(score) as average FROM ( SELECT score , venue , @seq:=IF(team<>@prev,1,@seq+1) as seq , @prev:=team as team FROM ( SELECT 'H' as venue , match_date , home as team , home_score as score FROM championship WHERE match_date < CURDATE() UNION ALL SELECT 'A' as venue , match_date , away as team , away_score as score FROM championship WHERE match_date < CURDATE() ORDER BY team, match_date DESC ) scores JOIN (SELECT @prev:='', @seq=0) init ) av WHERE seq < 6 GROUP BY team ) a ON a.team = c.away WHERE c.match_date = '2015-09-19' ORDER BY h.team; +---------+--------------------+---------------+---------+---------+ | ko_time | home | away | home_av | away_av | +---------+--------------------+---------------+---------+---------+ | 15:00 | Blackburn | Charlton | 0.6 | 1.0 | | 15:00 | Brentford | Preston | 0.6 | 0.6 | | 15:00 | Bristol City | Reading | 1.0 | 1.6 | | 15:00 | Huddersfield | Bolton | 0.6 | 0.6 | | 15:00 | Hull City | QPR | 1.0 | 1.6 | | 15:00 | Milton Keynes Dons | Leeds | 0.2 | 0.8 | | 12:30 | Nottingham | Middlesbrough | 1.0 | 1.6 | | 15:00 | Rotherham | Cardiff | 1.0 | 1.2 | | 15:00 | Sheffield Wed | Fulham | 0.6 | 1.4 | | 15:00 | Wolves | Brighton | 1.0 | 1.4 | +---------+--------------------+---------------+---------+---------+ 10 rows in set (0.07 sec) The second question you asked will require you to use a similar technique (though I think only a single subquery join will be necessary in that one)
  18. You might have to write the array contents to separate rows in a temporary table
  19. I had to change CURDATE() in the final WHERE clause (no Sunday matches) mysql> SELECT ko_time -> , team -> , AVG(score) -> FROM -> ( -> SELECT score -> , @seq:=IF(team<>@prev,1,@seq+1) as seq -> , @prev:=team as team -> FROM -> ( -> SELECT -> 'H' as venue -> , match_date -> , home as team -> , home_score as score -> FROM championship -> WHERE match_date < CURDATE() -> UNION ALL -> SELECT -> 'A' as venue -> , match_date -> , away as team -> , away_score as score -> FROM championship -> WHERE match_date < CURDATE() -> ORDER BY team, match_date DESC -> ) scores -> JOIN (SELECT @prev:='', @seq=0) init -> ) av -> INNER JOIN -> championship c ON av.team IN (c.home, c.away) -> WHERE c.match_date = '2015-09-19' AND seq < 6 -> GROUP BY team; +---------+--------------------+------------+ | ko_time | team | AVG(score) | +---------+--------------------+------------+ | 15:00 | Blackburn | 0.6000 | | 15:00 | Bolton | 0.6000 | | 15:00 | Brentford | 0.6000 | | 15:00 | Brighton | 1.4000 | | 15:00 | Bristol City | 1.0000 | | 15:00 | Cardiff | 1.2000 | | 15:00 | Charlton | 1.0000 | | 15:00 | Fulham | 1.4000 | | 15:00 | Huddersfield | 0.6000 | | 15:00 | Hull City | 1.0000 | | 15:00 | Leeds | 0.8000 | | 12:30 | Middlesbrough | 1.6000 | | 15:00 | Milton Keynes Dons | 0.2000 | | 12:30 | Nottingham | 1.0000 | | 15:00 | Preston | 0.6000 | | 15:00 | QPR | 1.6000 | | 15:00 | Reading | 1.6000 | | 15:00 | Rotherham | 1.0000 | | 15:00 | Sheffield Wed | 0.6000 | | 15:00 | Wolves | 1.0000 | +---------+--------------------+------------+ 20 rows in set (0.02 sec)
  20. try SELECT ko_time , team , AVG(score) FROM ( SELECT score , @seq:=IF(team<>@prev,1,@seq+1) as seq , @prev:=team as team FROM ( SELECT 'H' as venue , match_date , home as team , home_score as score FROM championship WHERE match_date < CURDATE() UNION ALL SELECT 'A' as venue , match_date , away as team , away_score as score FROM championship WHERE match_date < CURDATE() ORDER BY team, match_date DESC ) scores JOIN (SELECT @prev:='', @seq=0) init ) av INNER JOIN championship c ON av.team IN (c.home, c.away) WHERE c.match_date = curdate() AND seq < 6 GROUP BY team;
  21. Barand

    query help

    Sorry I couldn't be more helpful but I wasn't going to attempt to juggle data from five tables of unknown structure and content in my head.
  22. Barand

    query help

    Don't know, I can't see the data from here.
  23. You can use "WITH ROLLUP" to get the overall totals. Add it at the end of the quey . . . . . . GROUP BY date WITH ROLLUP gives +------------+---------+--------+-----------+-----------+-------+ | date | created | offers | contracts | purchases | sales | +------------+---------+--------+-----------+-----------+-------+ | 2015-09-01 | 1 | 0 | 0 | 0 | 0 | | 2015-09-02 | 1 | 2 | 0 | 0 | 0 | | 2015-09-03 | 1 | 0 | 1 | 0 | 0 | | 2015-09-04 | 1 | 2 | 3 | 0 | 0 | | 2015-09-05 | 1 | 1 | 1 | 0 | 0 | | 2015-09-12 | 0 | 0 | 0 | 1 | 0 | | 2015-09-18 | 0 | 0 | 0 | 0 | 1 | | 2015-10-16 | 0 | 0 | 0 | 1 | 0 | | 2015-10-26 | 0 | 0 | 0 | 1 | 0 | | 2015-11-06 | 0 | 0 | 0 | 1 | 0 | | 2015-12-01 | 0 | 0 | 0 | 1 | 0 | | 2015-12-15 | 0 | 0 | 0 | 0 | 1 | | 2015-12-30 | 0 | 0 | 0 | 0 | 1 | | 2016-01-15 | 0 | 0 | 0 | 0 | 1 | | 2016-02-01 | 0 | 0 | 0 | 0 | 1 | | NULL | 5 | 5 | 5 | 5 | 5 | +------------+---------+--------+-----------+-----------+-------+ Back at the original insertion of the data. That table has more of a spreadsheet flavour than a db table. Is it created by collecting dates from other tables?
  24. this is my query with my data SELECT date , SUM(IF(type='CR',1,0)) as created , SUM(IF(type='OF',1,0)) as offers , SUM(IF(type='CO',1,0)) as contracts , SUM(IF(type='PU',1,0)) as purchases , SUM(IF(type='SA',1,0)) as sales FROM ( SELECT 'CR' as type, created as date FROM property UNION ALL SELECT 'OF' as type, offer_date as date FROM property UNION ALL SELECT 'CO' as type, contract_date as date FROM property UNION ALL SELECT 'PU' as type, purchase_date as date FROM property UNION ALL SELECT 'SA' as type, sale_date as date FROM property ) props GROUP BY date; +------------+---------+--------+-----------+-----------+-------+ | date | created | offers | contracts | purchases | sales | +------------+---------+--------+-----------+-----------+-------+ | 2015-09-01 | 1 | 0 | 0 | 0 | 0 | | 2015-09-02 | 1 | 2 | 0 | 0 | 0 | | 2015-09-03 | 1 | 0 | 1 | 0 | 0 | | 2015-09-04 | 1 | 2 | 3 | 0 | 0 | | 2015-09-05 | 1 | 1 | 1 | 0 | 0 | | 2015-09-12 | 0 | 0 | 0 | 1 | 0 | | 2015-09-18 | 0 | 0 | 0 | 0 | 1 | | 2015-10-16 | 0 | 0 | 0 | 1 | 0 | | 2015-10-26 | 0 | 0 | 0 | 1 | 0 | | 2015-11-06 | 0 | 0 | 0 | 1 | 0 | | 2015-12-01 | 0 | 0 | 0 | 1 | 0 | | 2015-12-15 | 0 | 0 | 0 | 0 | 1 | | 2015-12-30 | 0 | 0 | 0 | 0 | 1 | | 2016-01-15 | 0 | 0 | 0 | 0 | 1 | | 2016-02-01 | 0 | 0 | 0 | 0 | 1 | +------------+---------+--------+-----------+-----------+-------+
  25. I am working on the query for, but, in the meantime I would advise you use DATE, DATETIME or TIMESTAMP type columns for date/times. Not only are these immediately useful without conversion to a usable format, but you can tell what the the date is when you view your data, instead of an unintelligible string of digits So while I am testing I am using DATE columns and inserted this data (to preserve your dates) INSERT INTO `property` (`id`, `created`, `offer_date`, `contract_date`, `purchase_date`, `sale_date`) VALUES (1, FROM_UNIXTIME(1441065600), FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441238400), FROM_UNIXTIME(1442016000), FROM_UNIXTIME(1442534400)), (2, FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1445817600), FROM_UNIXTIME(1450137600)), (3, FROM_UNIXTIME(1441238400), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1444953600), FROM_UNIXTIME(1451433600)), (4, FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1446768000), FROM_UNIXTIME(1452816000)), (5, FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1448928000), FROM_UNIXTIME(1454284800)); giving a readable +----+------------+------------+---------------+---------------+------------+ | id | offer_date | created | contract_date | purchase_date | sale_date | +----+------------+------------+---------------+---------------+------------+ | 1 | 2015-09-02 | 2015-09-01 | 2015-09-03 | 2015-09-12 | 2015-09-18 | | 2 | 2015-09-02 | 2015-09-02 | 2015-09-04 | 2015-10-26 | 2015-12-15 | | 3 | 2015-09-04 | 2015-09-03 | 2015-09-04 | 2015-10-16 | 2015-12-30 | | 4 | 2015-09-04 | 2015-09-04 | 2015-09-04 | 2015-11-06 | 2016-01-15 | | 5 | 2015-09-05 | 2015-09-05 | 2015-09-05 | 2015-12-01 | 2016-02-01 | +----+------------+------------+---------------+---------------+------------+
×
×
  • 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.