Jump to content

Barand

Moderators
  • Posts

    24,551
  • Joined

  • Last visited

  • Days Won

    821

Community Answers

  1. Barand's post in select between rate range at set time. was marked as the answer   
    So for it to work as it did with a separate time column, are the time elements the same in the two datetime columns?
     
    When comparing a date with a datetime column you need to use only the date portion of the datetime.
     
    So if my opening assumption is true
    SELECT id , schedule_start , schedule_end WHERE     UTC_DATE() BETWEEN DATE(schedule_start) AND DATE(schedule_end)         AND EXTRACT(HOUR_MINUTE FROM UTC_TIME()) = EXTRACT(HOUR_MINUTE FROM schedule_start)  
  2. Barand's post in Populate array from another array was marked as the answer   
    Change
    'options'      => array_values($category_str) to
    'options'      => $category_str;
  3. Barand's post in Myphpadmin - DD MM YYYY ? was marked as the answer   
    You could try reading thara's post again and use the function he gave you.
     
    edit - alternatively you can do it in PHP
    $dtobj = new DateTime($row['date']); echo $dtobj->format('d/m/Y'); //--> 23/01/2017, for example
  4. Barand's post in Database Diagram was marked as the answer   
    The table you needed to add is the "reserva_hora" to give a many-to-many relationship between reserva and hora.

  5. Barand's post in pdo count where was marked as the answer   
    Syntax error in the query. Remove the comma after "as total".
     
    As COUNT(*) now has the column alias "total" you should refer to it as $row['total'] and not $row['COUNT(*)'];
     
    It's easier to give expressions like that a column alias, especially for complex expressions.
  6. Barand's post in PHP Search results - Side by side rather than stacked was marked as the answer   
    Easiest way is to put the results in divs with float:left and width ~= 25%
  7. Barand's post in How to remove comma from last row in mysql query in php was marked as the answer   
    Put into an array then join()
    $Q = "SELECT length, width FROM statxyx WHERE siteid='$siteid'"; $R = mysqli_query($DB,$Q); //start loop //while or foreach $results=[]; // define array while($row = mysqli_fetch_assoc($R)){ $results[] = "['7C6Buh',{$row['length']},{$row['width']}]"; // add to array } $final = join(",\r\n", $results);
  8. Barand's post in ranking based on score and time elapsed was marked as the answer   
    test data
    insert into ranking (id_jogo, id_user, pontuacao, data, tempo) VALUES (5141, 11 , 15, '2016-12-27', '00:00:03'), (7001 , 9 , 10, '2016-12-27', '00:00:06'), (2519 , 7 , 5, '2016-12-27', '00:00:07'), (4585 , 6 , 15, '2016-12-27', '00:00:04'), (4585 , 5 , 10, '2016-12-27', '00:00:05'), (4585 , 4 , 10, '2016-12-27', '00:00:07'), (4585 , 3 , 6, '2016-12-27', '00:00:07'), (4585 , 2 , 7, '2016-12-27', '00:00:07'), (4585 , 1 , 10, '2016-12-27', '00:00:06'); query
    SELECT id_user , pontuacao , data , tempo , rank FROM ( SELECT id_user , data , @row := @row+1 as row , @rank := IF(@prevpont = pontuacao AND @prevtemp = tempo, @rank, @row) as rank , @prevpont := pontuacao as pontuacao , @prevtemp := tempo as tempo FROM ranking JOIN (SELECT @prevtemp:='0:00:00', @prevpont:=0, @row:=0, @rank:=0) init ORDER BY pontuacao DESC, tempo ) calc ; results
    +---------+-----------+------------+----------+--------+ | id_user | pontuacao | data | tempo | rank | +---------+-----------+------------+----------+--------+ | 11 | 15 | 2016-12-27 | 00:00:03 | 1 | | 6 | 15 | 2016-12-27 | 00:00:04 | 2 | | 5 | 10 | 2016-12-27 | 00:00:05 | 3 | | 9 | 10 | 2016-12-27 | 00:00:06 | 4 | = | 1 | 10 | 2016-12-27 | 00:00:06 | 4 | = | 4 | 10 | 2016-12-27 | 00:00:07 | 6 | | 2 | 7 | 2016-12-27 | 00:00:07 | 7 | | 3 | 6 | 2016-12-27 | 00:00:07 | 8 | | 7 | 5 | 2016-12-27 | 00:00:07 | 9 | +---------+-----------+------------+----------+--------+
  9. Barand's post in How to filter Select option to ALL and Show Data Based on ALL Select Option was marked as the answer   
    Spot the difference?
  10. Barand's post in Sum row in UNION was marked as the answer   
    For a start, too many "UNIONS".
    SELECT MONTH(due_date) as month , SUM(amount_paid) as total FROM ( SELECT due_date, amount_paid FROM table1 UNION ALL SELECT due_date, amount_paid FROM table2 )x GROUP BY month If you are having to do that, it looks like your table1 and table2 should be a single table with an additional identifier column.
  11. Barand's post in Query multiple databases in the same query was marked as the answer   
    Yes, that's OK. So long as they are both on the same server. The connection is to the server. And, as you said, you have privileges to access all databases in the query.
  12. Barand's post in user defined functions in separate file or not was marked as the answer   
    Horses for courses. It depends on where you will use the function. If it is specific to one page, define it in that page. If it is used by more than one page then define it in a separate file.
     
    So some will be defined in the page, some in an application-specific function file and some in general function file.
  13. Barand's post in retrieve data from mysql using a search field in wordpress was marked as the answer   
    The content data is JSON encoded, so you need to decode it then access the content fields
    if (isset($_GET['zoeknummer'])) { $sql = "SELECT lead_content FROM lead"; $res = $pdo->query($sql); // where $pdo is your db connection while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo $data->komplex . ' : ' . $data->plaats . '<br>'; } } }
  14. Barand's post in stratotime inbetween dates was marked as the answer   
    try
    $timestamp = $output2['data'][$acc_id]['last_battle_time']; $dt1 = new DateTime(); $dt1->setTimestamp($timestamp); $dt2 = new DateTime(); echo ceil($dt1->diff($dt2)->days / 7) * 7;
  15. Barand's post in how to change the date formate in excel from 01.01.2017 to 01.01.17 was marked as the answer   
    Select date cells and right click
    Select "format cells"
    Choose "custom"
    enter "mm.dd.yy"
  16. Barand's post in Foreach array (sum array + condition) was marked as the answer   
    This uses the above model to produce your table output
    <?php // PDO connection $db = pdoConnect('foo'); $sql = "SELECT course,certlevel FROM course ORDER BY certlevel,course_id"; $res = $db->query($sql); $courses = []; foreach ($res as $crs) { $courses[ $crs['certlevel'] ][] = $crs['course']; } // build table heading and empty table row array $thead = "<tr><th>Name</th>"; $newarray = []; foreach ($courses as $cert=>$crss) { $cclass = "L$cert"; $thead .= "<th class='$cclass'>" . join("</th><th class='$cclass'>",$crss) . "</th><th class='$cclass'>Certificate $cert</th>"; foreach ($crss as $cname) { $newarray[$cert][$cname] = 0; } } $thead .= "</tr>\n"; // get the student marks $sql = "SELECT s.name , c.course , c.certlevel , e.marks FROM student s LEFT JOIN enrolment e USING (student_id) LEFT JOIN course c USING (course_id) ORDER BY e.student_id, c.certlevel, e.course_id"; $res = $db->query($sql); $prevname=''; $tdata = ''; $studata = $newarray; foreach ($res as $row) { if ($row['name'] != $prevname) { if ($prevname) { $tdata .= "<tr><td>$prevname</td>"; foreach ($studata as $cert=>$marks) { $cclass = "L$cert"; $certres = min($marks) >= 50 ? "Entitle for Cert $cert" : 'Complete only'; foreach ($marks as $m) { $tdata .= "<td class='$cclass'>$m</td>"; } $tdata .= "<td class='cert$cclass'>$certres</td>"; } $tdata .= "</tr>\n"; } $studata = $newarray; $prevname = $row['name']; } $studata[$row['certlevel']][$row['course']] = $row['marks']; } // last student $tdata .= "<tr><td>$prevname</td>"; foreach ($studata as $cert=>$marks) { $cclass = "L$cert"; $certres = min($marks) >= 50 ? "Entitle for Cert $cert" : 'Complete only'; foreach ($marks as $m) { $tdata .= "<td class='$cclass'>$m</td>"; } $tdata .= "<td class='cert$cclass'>$certres</td>"; } $tdata .= "</tr>\n"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> body { font-family: sans-serif; font-size: 10pt; } table { border-collapse: collapse; } td, th { border: 1px solid #888; padding: 3px; min-width: 40px; } td.L1 { background-color: #ccf; text-align: right; } td.L2 { background-color: #fcc; text-align: right; } td.certL1 { background-color: #ccf; font-weight: 600; } td.certL2 { background-color: #fcc; font-weight: 600; } th.L1 { background-color: #aaf; } th.L2 { background-color: #faa; } </style> </head> <body> <table> <thead> <?=$thead?> </thead> <tbody> <?=$tdata?> </tbody> </table> </body> </html> NOTE The pdo connect function (in an included file) is
    define("HOST",'localhost'); define("USERNAME",'*****************'); define("PASSWORD",'*****************'); function pdoConnect($dbname) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; }
  17. Barand's post in PHP multiple date condition was marked as the answer   
    try
    <?php // get data date range $date = new DateTime(); // now $dateto = $date->format('Y-m-01'); $datefrom = $date->sub(new DateInterval('P1Y'))->format('Y-m-01'); // minus 1 year // initialize array $dp = new DatePeriod($date, new DateInterval('P1M'),12); $data = []; foreach ($dp as $d) { $data[$d->format('F')] = [ 'reg' => 0, 'app' => 0 ]; } // get data and accumulate counts in array $sql = "SELECT registerdate , approvedate FROM datetest WHERE registerdate >= ? AND registerdate < ? ORDER BY registerdate"; $stmt = $pdo->prepare($sql); $stmt->execute( [$datefrom, $dateto] ); while ($row = $stmt->fetch()) { if ($row['registerdate']) { $rm = (new DateTime($row['registerdate']))->format('F'); $data[$rm]['reg']++; } if ($row['approvedate']) { $am = (new DateTime($row['approvedate']))->format('F'); $data[$am]['app']++; } } // assemble the output $tabledata = ''; foreach ($data as $month=>$vals) { $tabledata .= "<tr><td>$month</td><td>{$vals['reg']}</td><td>{$vals['app']}</td></tr>\n"; } ?> <table> <thead> <tr><th>Month</th><th>Registered</th><th>Approved</th></tr> </thead> <tbody> <?=$tabledata?> </tbody> </table> Note that this processes the previous year's data. Set your date range as required.
  18. Barand's post in Pull up last entries on a product inventory table was marked as the answer   
    SELECT product
      , MAX(timestamp) as lastsale
    FROM inventory
    GROUP BY product
    ORDER BY lastsale DESC
    LIMIT 4
  19. Barand's post in rotating letters in a dynamic image was marked as the answer   
    try
    $chars = '16849'; $im = imagecreate(500,100); $bg = imagecolorallocate($im,0,0,0); $fg = imagecolorallocate($im,0,0xFF,0xFF); for ($c=0; $c<5; $c++) { $angle = $c*10; $ch = $chars[$c]; imagettftext($im, 60, $angle, $c*100+10, 90, $fg, 'c:/windows/fonts/Arial.ttf', $ch); } header("Content-type: image/png"); imagepng($im); imagedestroy($im);
  20. Barand's post in Divide by zero warning was marked as the answer   
    Setting a variable $return_value does not return the value.
    function get_query_value ($con,$query){ $con->real_query($query); $result=$con->use_result(); $row=$result->fetch_row(); $result->close(); return $row[0]; //<---- return the value } @ginerjm - the function is closing the result set, NOT the connection.
  21. Barand's post in storing a resized image on the server was marked as the answer   
    imagepng
    imagepng($dst, 'path/to/file');
  22. Barand's post in get the serial number from the file name was marked as the answer   
    Both these will do it
    $serialno = strstr($filename, '_', true);    // get chars before the '_' $serialno = substr($filename, 0, 4);         // get first four chars  
  23. Barand's post in problem with parsing xml was marked as the answer   
    $xml = simplexml_load_string($responseXml);

    foreach ($xml->Messages->Message as $mes) {
    echo $mes->MessageID . '<br>';
    }

  24. Barand's post in using php objects in strings was marked as the answer   
    I suspect it's because it uses "complex string syntax", just as $str = "ABC{$array[1][2]}"; requires the {..} whereas $str = "ABC$array[3]"; does not.
     
    http://uk1.php.net/manual/en/language.types.string.php#language.types.string.parsing
  25. Barand's post in phpexcel csv export 2 decimel place format was marked as the answer   
    Have you tried number_format()?
×
×
  • 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.