-1

Need help on this: in my sql table I have this Schedule and is written in MWF, TThS, Sun. I want it to be filtered so that only the schedule for today will be the output...e.g Today is Tuesday so TThS schedule is being outputted. Hope you could help me.

1 Answers1

0

The problem with the way your schedules times are stored is that you can't do a simple LIKE %T%, as it would get schedules for Th as well.

A possible solution would be to use MySQL REGEXP for the specific cases of Tuesday (T not followed by h) and Saturday (S not followed by un).

Basically you would build an array of the where clauses that should be used depends of the week day:

$where_date_clauses = array(
    1 => 'LIKE "M%"',
    2 => 'REGEXP "T(?!h)"',
    3 => 'LIKE "%W%"',
    4 => 'LIKE "%Th%"',
    5 => 'LIKE "%F%"',
    6 => 'REGEXP "S(?!un)"',
    7=> 'LIKE "%Sun"'
);

The regexp here use the negative lookahead pattern - it will match only if the character is not immediately followed by the lookahead content.

And then build your query like this:

$sql = 'SELECT * FROM schedules_table WHERE schedule ' . $where_date_clauses[date('N')];

Where date('N') return the number of the week day.

Side note

Here is an example of how to set up your query using PDO:

try {
    $dbh = new PDO('mysql:dbname=your_db_name;host=your_db_host', 'mysql_user_name', 'mysql_user_password');
    $sth = $dbh->prepare('SELECT * FROM block_subject2 where id_number = ? ORDER BY timed DESC LIMIT 1');
    $sth->execute(array(
        (int) $_SESSION['stud_id']
    ));
    $row = $sth->fetch();
} catch(PDOException $e) {
    echo 'Failed to connect to DB: ' . $e->getMessage();
}
vard
  • 4,057
  • 2
  • 26
  • 46
  • is it compatible with what i am using right now? im using simple php codes... $result8=mysql_query("select * from block_subject2 where id_number='$_SESSION[stud_id]' AND day='$where_date_clauses[date('N')]' ORDER BY timed DESC limit 1"); – Ken Paul Miñoza Bahinting Feb 09 '16 at 13:24
  • $sql = 'SELECT * FROM schedules_table WHERE schedule ' . $where_date_clauses[date('N')]; how can i convert it to a code such as this> $result8=mysql_query("select * from block_subject2 where id_number='$_SESSION[stud_id]' AND day='$where_date_clauses[date('N')]' ORDER BY timed DESC limit 1"); coz it aint working :( – Ken Paul Miñoza Bahinting Feb 09 '16 at 13:27
  • You should not use the `=` operator in your query - the operator is built in the `$where_date_clauses` array (`LIKE`, `REGEXP`). Look at the query I wrote as example. – vard Feb 09 '16 at 13:30
  • By the way, you should avoid using `mysql_query` as it has been deprecated in PHP 5.5 and will be removed in PHP 7. You should better use mysqli or PDO instead. – vard Feb 09 '16 at 13:33
  • so how am I going to convert this > $result8=mysql_query("select * from block_subject2 where id_number='$_SESSION[stud_id]' ORDER BY timed DESC limit 1"); while($row8=mysql_fetch_array($result8)) { to PDO? any idea? – Ken Paul Miñoza Bahinting Feb 09 '16 at 13:41
  • I updated my answer to add an example of how to use PDO for your query. I suggest you to give some read to the [`PDO::construct`](http://php.net/manual/en/pdo.construct.php), [`PDO::prepare`](http://php.net/manual/en/pdo.prepare.php), [`PDOStatement::execute`](http://php.net/manual/en/pdostatement.execute.php) and [`PDOStatement::fetch`](http://php.net/manual/en/pdostatement.fetch.php) documentation to understand how it works. – vard Feb 09 '16 at 13:53
  • OMG! all of my codes are in that form :( it would take me forever to edit my codes...and my thesis defense is next week :( – Ken Paul Miñoza Bahinting Feb 10 '16 at 03:02
  • @KenPaulMiñozaBahinting That's allright if it's working using mysql_* function, keep it that way. But I suggest to notice in your thesis that you're aware that you're using deprecated code. Or, you can try to change to mysqli_* which syntax is almost the same as mysql_* functions - it's the same function but prefixed with mysqli instead of mysql, and you need to send the connector object to most of the functions. [How to change mysql to mysqli](http://stackoverflow.com/questions/1390607/how-could-i-change-this-mysql-to-mysqli) – vard Feb 10 '16 at 07:59