Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. As Kicken said, the row and rank are calculated. My score table looks like this: +-------+-------+ | name | score | +-------+-------+ | Bob | 75 | | David | 106 | | Jane | 75 | | Joe | 61 | | Mary | 59 | | Mike | 61 | | Sam | 76 | +-------+-------+
  2. I cannot see anything wrong with your use of the DateTime objects other than it unnecessary in the case of the date from the service records as that will be in Y-m-d format any way. If you are doing that to lose the time element of a datetime type column then it easier to "SELECT DATE(boosterdate) as boosterdate" to just get the date part. Your update query has no WHERE clause and so will set the value to 30 in every record in the table Also you don't use the terminating ";" (the one after 30) when running queries from PHP. To get the service recs where boosterdate is within the next 30 days you can either use the DateTime class to calc the dates SELECT * FROM service WHERE DATE(boosterdate) BETWEEN '$formattedDate' AND '$date30' or do entirely in SQL (easier IMHO) SELECT * FROM service WHERE DATE(boosterdate) BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY
  3. does this help? $xml = simplexml_load_string($str); $channelname = "CHANNEL"; // get from xml feed $lang = "LANG"; // get from xml feed $kbps = "KBPS"; // get from xml feed $acopy = array(); foreach ($xml->match as $match) { foreach ($match->linkset->link as $link) { $acopy[(string)$match->matchname][] = array($channelname, $lang, $kbps, (string)$link); } } echo '<pre>',print_r($acopy, true),'</pre>'; RESULT Array ( [Town Rovers vs Chanel] => Array ( [0] => Array ( [0] => CHANNEL [1] => LANG [2] => KBPS [3] => link1.html ) [1] => Array ( [0] => CHANNEL [1] => LANG [2] => KBPS [3] => link2.html ) [2] => Array ( [0] => CHANNEL [1] => LANG [2] => KBPS [3] => link3c.html ) [3] => Array ( [0] => CHANNEL [1] => LANG [2] => KBPS [3] => link4.html ) ) )
  4. Firstly, I would restructure your data. A single table like this mysql> SELECT * FROM homework; +------------+------------+---------+-------------+ | idhomework | hwdate | subject | description | +------------+------------+---------+-------------+ | 1 | 2013-11-02 | OTHER | Other100 | | 2 | 2013-11-02 | DANISH | Danish101 | | 3 | 2013-11-02 | ENGLISH | English102 | | 4 | 2013-11-02 | OTHER | Other103 | | 5 | 2013-11-03 | MATH | Math104 | | 6 | 2013-11-03 | MATH | Math105 | | 7 | 2013-11-03 | MATH | Math106 | | 8 | 2013-11-03 | DANISH | Danish107 | | 9 | 2013-11-04 | OTHER | Other108 | | 10 | 2013-11-04 | GERMAN | German109 | | 11 | 2013-11-04 | MATH | Math110 | | 12 | 2013-11-04 | OTHER | Other111 | | 13 | 2013-11-04 | GERMAN | German112 | | 14 | 2013-11-05 | MATH | Math113 | | 15 | 2013-11-06 | MATH | Math114 | | 16 | 2013-11-06 | DANISH | Danish115 | | 17 | 2013-11-06 | ENGLISH | English116 | | 18 | 2013-11-06 | MATH | Math117 | | 19 | 2013-11-06 | MATH | Math118 | | 20 | 2013-11-07 | GERMAN | German119 | | 21 | 2013-11-07 | DANISH | Danish120 | | 22 | 2013-11-07 | GERMAN | German121 | | 23 | 2013-11-07 | OTHER | Other122 | | 24 | 2013-11-07 | ENGLISH | English123 | | 25 | 2013-11-08 | DANISH | Danish124 | | 26 | 2013-11-08 | GERMAN | German125 | | 27 | 2013-11-08 | OTHER | Other126 | | 28 | 2013-11-09 | OTHER | Other127 | | 29 | 2013-11-09 | MATH | Math128 | | 30 | 2013-11-09 | ENGLISH | English129 | | 31 | 2013-11-09 | OTHER | Other130 | | 32 | 2013-11-09 | OTHER | Other131 | | 33 | 2013-11-10 | MATH | Math132 | | 34 | 2013-11-11 | DANISH | Danish133 | | 35 | 2013-11-11 | ENGLISH | English134 | | 36 | 2013-11-11 | OTHER | Other135 | +------------+------------+---------+-------------+ Then you can produce an HTML table like this (first bit of code creates test data) <?php include("/db_inc.php"); $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); /************************************************************** * Create table with normalized structure ***************************************************************/ $sql = "CREATE TABLE homework ( idhomework INT NOT NULL AUTO_INCREMENT PRIMARY KEY, hwdate DATE, subject VARCHAR(10), description VARCHAR(255) )"; // $db->query($sql); /************************************************************** * Create some random test data ***************************************************************/ $subjects = array('DANISH','GERMAN','MATH','ENGLISH','OTHER'); $recs = array(); $n = 100; $dt = new DateTime(); $dp = new DatePeriod($dt, new DateInterval('P1D'), 10); foreach ($dp as $date) { $k = rand(1,5); // how many assignments this day for ($i=0; $i<$k; $i++) { $subj = $subjects[array_rand($subjects)]; // get random subject $desc = ucfirst(strtolower($subj)) . $n++; // create a description $recs[] = sprintf("('%s', '%s', '%s')", $date->format('Y-m-d'),$subj, $desc); } } $sql = "INSERT INTO homework(hwdate,subject,description) VALUES\n" . join(",\n", $recs); // $db->query($sql); /**************************************************************** * Query the data and display in HTML table *****************************************************************/ $template = array_fill_keys($subjects, array()); // create empty array $tableHead = "<tr><th>Date</th><th>" . join('</th><th>', $subjects) . '</th></tr>'; echo "<table border='1' cellpadding='5' style='border-collapse:collapse'>$tableHead\n"; $sql = "SELECT hwdate, subject, description FROM homework ORDER BY hwdate, subject"; $res = $db->query($sql); $curdate = ''; while ($row = $res->fetch_assoc()) { if ($row['hwdate'] != $curdate) { if ($curdate) { echo "<tr style='vertical-align: top'><td>$curdate</td>"; foreach ($assignments as $arr) { echo '<td>' . join('<br>', $arr) . '</td>'; } echo "</tr>\n"; } $assignments = $template; $curdate = $row['hwdate']; } $assignments[$row['subject']][] = $row['description']; } // don't forget last date echo "<tr style='vertical-align: top'><td>$curdate</td>"; foreach ($assignments as $arr) { echo '<td>' . join('<br>', $arr) . '</td>'; } echo "</tr>\n"; echo "</table>\n"; ?> Which gives the attached output.
  5. Here's how to do it in a query mysql> SELECT name, -> @row := @row+1 as row, -> @rank := IF(score=@prev, @rank, @row) as rank, -> @prev := score as score -> FROM score -> JOIN (SELECT @row:=0, @rank:=0, @prev:=0) as init -> ORDER BY score DESC; +-------+------+------+-------+ | name | row | rank | score | +-------+------+------+-------+ | David | 1 | 1 | 106 | | Sam | 2 | 2 | 76 | | Bob | 3 | 3 | 75 | | Jane | 4 | 3 | 75 | | Joe | 5 | 5 | 61 | | Mike | 6 | 5 | 61 | | Mary | 7 | 7 | 59 | +-------+------+------+-------+
  6. You start off mentioning "columns" in your post then you switch to "tables". Can you be precise? What is the structure of the relevant tables in your database?
  7. Are you serious about having to click 72 buttons 100 times to give assessment scores of 100? And if you accidentally overshoot then go round again! Why not text input or dropdowns? This code will give you dropdowns. <?php $categories = array( 1 => 'Category 1', 2 => 'Category 2', 3 => 'Category 3', 4 => 'Category 4', 5 => 'Category 5', 6 => 'Category 6', 7 => 'Category 7', 8 => 'Category 8' ); $criteria = array( 1 => 'Crit 1', 2 => 'Crit 2', 3 => 'Crit 3', 4 => 'Crit 4', 5 => 'Crit 5', 6 => 'Crit 6', 7 => 'Crit 7', 8 => 'Crit 8', 9 => 'Crit 9' ); $tableHead = "<tr><th> </th><th>" . join('</th><th>', $criteria) . "</th></tr>\n"; $opts = ''; for($i=0; $i<=100; $i++) { $v = $i==0 ? '' : $i; $opts .= "<option value='$i'>$v</option>"; } ?> <html> <head> <meta name="expires" content= "Sat, 4 Dec 2128 00:00:00 GMT"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta name="generator" content="PhpED Version 8.1 (Build 8115)"> <title>Form sample</title> <meta name="author" content="Barand"> <meta name="creation-date" content="11/02/2013"> <style type="text/css"> select { width: 50px; margin: 3px; border: none; background-color: #eee; } th { color: white; font-weight: 600; background-color: #369; padding: 2px 4px; width: 10%; } td { background-color: #eee; padding: 2px 4px; } </style> </head> <body> <h3>Assessment</h3> <form method="post"> <table cellspacing="1"> <?php echo $tableHead; foreach ($categories as $catid=>$cat) { echo "<tr><td>$cat</td>"; foreach ($criteria as $cid=>$crit) { echo "<td><select name='assess[$catid][$cid]'>$opts</select></td>"; } echo "</tr>\n"; } ?> </table> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html>
  8. If I am correct about your goal then a single query should do it. As a test I created a file of dates from 10 days ago to 100 days in the future and with a boosterWithinDays field (all NULL) UPDATE dates SET boosterwithindays = CASE WHEN DATE(thedate) < CURDATE() THEN NULL WHEN DATE(thedate) < CURDATE()+INTERVAL 30 DAY THEN 30 WHEN DATE(thedate) < CURDATE()+INTERVAL 60 DAY THEN 60 WHEN DATE(thedate) < CURDATE()+INTERVAL 90 DAY THEN 90 ELSE NULL END The resulting table looked like this: However, why would you need store the calculated result? You can just query the table to get those due in the next 30 days or whenever or calc the result when outputting the list
  9. No need to split the array $learners=array( 'Eliza' => 87, 'Joe' => 81, 'Anne' => 69, 'Marley' => 39, 'Teddy' => 39, 'Jemma' => 90, 'Sylvia' => 87 ); arsort($learners); $ranking = array(); $k = $prev = 0; $rank = 1; foreach ($learners as $name => $score) { $k++; $rank = $score == $prev ? $rank : $k; $ranking[$name] = $rank; $prev = $score; } echo '<pre>',print_r($ranking, true),'</pre>'; /**** OUTPUT ********** Array ( [Jemma] => 1 [Sylvia] => 2 [Eliza] => 2 [Joe] => 4 [Anne] => 5 [Marley] => 6 [Teddy] => 6 ) ***********************/
  10. When using print_r() put it between <pre>..</pre> tags. It makes it so much more readable echo '<pre>', print_r($obj, true), '</pre>';
  11. $fp = fopen("webinvt.txt"); while ($line = fgetcsv($fp, 1024)) { if (conditions met) { # use it } else { # don't use it } } fclose($fp);
  12. or you could just use fgetcsv
  13. Name you checkboxes "search[]" then they will be posted as an array in $_POST['search']
  14. Something like this $db = new mysqli(HOST,USERNAME,PASSWORD,'databasename'); $sql = "SHOW TABLES"; $res = $db->query($sql); while ($row = $res->fetch_row()) { echo "$row[0]<br>"; $sql2 = "TRUNCATE TABLE `$row[0]`"; $db->query($sql2); }
  15. It is better to store the data outside the code and ideally a database would be my choice. However for a simple structure like the one you have an ini file format text file would suit. EG folders.txt containing [employment] 0='references' 1='starter-packs' 2='certificates' 3='health' [residents] 0='personal' 1='health' 2='notes' 3='signatures' To create your array you would then $folders = parse_ini_file('folders.txt', true); which would give an array like this: Folders Array ( [employment] => Array ( [0] => references [1] => starter-packs [2] => certificates [3] => health ) [residents] => Array ( [0] => personal [1] => health [2] => notes [3] => signatures ) ) To process this array foreach ($folders as $folder => $subfolders) { // create $folder folder foreach ($subfolders as $sub) { // create subfolder $folder/$sub } }
  16. If you do it clientside you have the overhead of always downloading the original large image
  17. and if you are just echoing the parameter that was passed to the function, why the SQL query? This would work $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); function admin($db) { function username($db) { $sql = "SELECT fullname FROM users WHERE account_type='admin'"; $res = $db->query($sql); if ($row = $res->fetch_assoc()) { $username = $row['fullname']; } else { $username = 'Not found'; } return $username; } return username($db); } echo admin($db);
  18. try SELECT name, entry, result, COUNT(*) as total FROM pomtom WHERE name='Tom' AND entry='A' GROUP BY name, entry, result ORDER BY name, entry, total DESC; Line 3 is optional. If omitted you get counts for all, with highest occurring results first for each
  19. I use MySQL Workbench - free from the MySQL site http://dev.mysql.com/doc/index-gui.html
  20. Then it becomes a pita to sort or search by result. I don't think extra effort occasionally required is sufficient excuse to denormalize the data
  21. Without the regexp you could use ...WHERE SUBSTRING(name, 1, 1) IN ('S', 'P', 'N');
  22. COUNT(DISTINCT sex) counts how many different sexes there are where the sex = F ie 1. You join "courses, students" is wrong. This will give a cartesian join, joining every course record with every student record. If you have 10 courses and 100 students you will get 1000 rows returned. You must specify how the tables are related eg FROM course JOIN student ON course.somefield = student.otherfield
  23. Why don't you query for those WHERE archive = 'N' ? If you have to store in array, use fetch_assoc() and not fetch_array(). As you may have noticed, fetch_array() gets all the values twice. EG while ($row = $db->fetch_assoc()) { $array[] = $row; }
  24. If that mess really is your csv format, you've got real problems
×
×
  • 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.