1

How to print all times, but that one whitch is in datbase with the same name (value) that should be printed has text attached, or value is bolded? Sorry for my bad english ;D

Here are the values

$a = '9.00';
$b = '10.00';
$c = '11.00';
$d = '12.00';
$e = '13.00';
$f = '14.00';
$g = '15.00';
$h = '16.00';
$i = '17.00';
$j = '18.00';

$date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");
while ($row = mysql_fetch_assoc($q)){
$time = $row['time'];
....

}

how to make it? with arrays? or foreach... HELP, please ;D

outcome would be:

9.00 - AVAIBLE -> this one is not in db 10.00 - NOT AVAIBLE -> this one is in database 11.00 - NOT AVAIBLE -> this one is in database 12.00 - NOT AVAIBLE -> this one is in database .....

2
  • To me it's not clear what you mean. Can you give some examples of input vs output? Commented Apr 20, 2012 at 13:42
  • Try to write your question more clear ... Commented Apr 20, 2012 at 13:45

2 Answers 2

3

If I understand, you want to print all the times $a..$j but if they occur in your query result to bold them or identify them.

// Store all times in an array rather than variables...
$times = array('9.00','10.00','11.00','12.00','13.00','14.00','15.00','16.00','17.00','18.00');

// Array to hold times from the database query
$dbtimes = array();

date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");
while ($row = mysql_fetch_assoc($q)){

  // Place database times onto the array
  $dbtimes[] = $row['time'];
}

// Iterate over all the times and if the same time occurs in the databse
// array list, output it with special marks as needed.
foreach ($times as $t) {
  if (in_array($t, dbtimes)) {
     // Print bolded time $t
     echo '<strong>$t</strong>';
  }
  else {
     // Print regular time $t
     echo $t;
  }
}

Note that 5/21/2012 is not a valid date format for MySQL datetime types (2012-05-12 is). Hopefully your column date is a proper datetime type and not a custom string like 5/21/2012. If it isn't, I recommend changing it to a proper datetime.

Sign up to request clarification or add additional context in comments.

Comments

0
// store all the times in an array
$mytimes = array('9.00', '10.00', ..., '18.00');

$date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");

while($row = mysql_retch_assoc($q){
    $time = $row['time'];

    // loop through the array, if the time matches the value in the database, 
    // make it bold
    foreach($mytimes as $timecheck){
        if($time == $timecheck){
            // make bold
            echo '<b>'.$timecheck.'</b><br>';
        }
        else{
            echo $timecheck.'<br>';
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.