1

I am designing an event feed from a calender I made. I'm using the same data from the database but to match specific dates and times.

  1. I only want 4 events to show at once (why I specified length < 4)
  2. Where the database value 'showFeed' is true, it only displays those rows.
  3. and I want it to show by date time, I have odd id's for each value in the database, which might make them out of order.

My current code:

$sql = "SELECT `title`, `time`, `start`, `showFeed` FROM calender WHERE length('column') > '0'";

$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);

echo "<div class=\"eventfeed\">";
echo "<ul>";

foreach ($result as $row){

$show = $row['showFeed'];

if ($show == 1 && length.$result < 4){
    echo "<li>";
    echo $row['title']. "<br />";
    echo $row['start'].' '.$row['time'];
    echo "</li>";
} 
else {

    return false;
}
}

echo "</ul>";
echo "</div>";

$dbh = null;

echo json_encode($return);

?>

I'm getting results and no errors from the database, but I'm only seeing one return on $results.

I honestly, do not have a clue where else to go from here. I'm lost.

4

2 に答える 2

1

I don't know if this is your actual code, but you should determine the length of an array by doing $result.length instead of the other way around.

Also, you can limit the number of results in your query using 'LIMIT 4' at the end of your query. That way MySQL only returns 4 results and you don't have to worry about that in your code, just print everything.

于 2012-04-21T20:55:59.590 に答える
1

For 1+.2.+3. modify your query to SELECT title, time, start, showFeed FROM calender WHERE length('column') > '0' and showFeed=1 and time<current_timestamp ORDER BY time DESC LIMIT 0,3 and remove your if (...) statement.

于 2012-04-21T20:59:43.300 に答える