简体   繁体   中英

PHP SQL events by Month and Year

UPDATED PHP

Seems to be something to do with Angle brackets...

My first time coding my own PHP and SQL, but having a bit of trouble, so not even plugged HTML in yet. I'd imagine I'm not escaping something right, or my formatting is miles off... or something REALLY simple.

I'm basically trying to get a list of events working, and many posts on it here, just can't put them all together to get a result.

So I'm looking for

YEAR

MONTH
EVENT

MONTH
EVENT
EVENT

YEAR etc

My HTML looks like this...

<div id="year">
<p class="vert">YEAR</p>
    <div id="month">
        <h1>MONTH</h1>
        <div class="event">
            <p>DATE START - DATEEND IF DIFFERENT/AVAILABLE</p>
            <div class="eventmain">
                <img class="flag" src="./img/defaultflag.png">
                <img class="open" src="./img/plus.png">
                <img class="close" src="./img/minus.png">
                <h2> Event Name </h2>
                <div class="eventdetails">
                    <p>FORMAT</p>
                    <p>INFO</p>
                    <p>CONTACT</p>
                    <p>EMAIL</p>
                    <p>WEBSITE</p>
                </div> <!-- close div event details -->
            </div> <!-- close div event main -->
        </div> <!-- close div event -->
    </div> <!-- close div month -->
</div> <!--close div year-->

With the following DB fields in the DB debate_calendar, table events event_name, event_startdate, event_enddate, event_flag, event_format, event_info,event_contactinfo, event_email, event_website, event_reg

I have the following PHP

<?php

$server = "localhost:8889";
$user = "root";
$passwd = "root";
$db_name = "debate_calendar";
$table_name = "events";

$conn = mysql_connect($server, $user, $passwd) or die("Couldn't connect to SQL Server on $server"); 
mysql_select_db($db_name, $conn);

    if (!$conn)
        {
        exit("Failed to Connect to $dbConnection");
        } 
    else
        {
        echo "Database Connected";
        }

$query = "SELECT * FROM `debate_calendar`.`events` WHERE event_date > NOW() ORDER BY event_date DESC";
$result = mysql_query( $query );

    $current_month = '';
    $current_year = '';

    while ($event = mysql_fetch_assoc($result)) {
         $year = date('y', $event['event_startdate']);
        if($current_year != $year) 
            {
                $current_year = $year;
                echo ("<h1>" .$current_year. "</h1>");
            }           
        $month = date('m', $event['event_startdate']);
        if($current_month != $month) 
            {
                $current_month = $month;
                echo '<h2>' . $current_month . '</h2>';
            }
        echo '<p>' . $event['event_name'] . '</p>';
    }   
        mysql_close($con);
?> 

But all it's generating is...

NOW() ORDER BY event_date DESC"; $result = mysql_query( $query ); $current_month = ''; $current_year = ''; while ($event = mysql_fetch_assoc($result)) { $year = date('y', $event['event_startdate']); if($current_year != $year) { $current_year = $year; echo ("
" .$current_year. "
"); } $month = date('m', $event['event_startdate']); if($current_month != $month) { $current_month = $month; echo '
' . $current_month . '
'; } echo '

' . $event['event_name'] . '
'; } mysql_close($con); ?> 

You didn't quote your last echo before $qry . Also, there are some extra characters in your query that will make it fail. This is how it should look:

SELECT * FROM `debate_calendar`.`events` WHERE event_date > NOW() 
ORDER BY event_date DESC

Also, you can get year/month/date parts of any DATE / DATETIME type field directly from SQL if you want:

SELECT year(event_date) AS event_year, month(event_date) AS event_month ...
echo <p> Your Database is Connected</p\><br/\>

You forgot to add the quotes ("")

try this:

echo "<p> Your Database is Connected</p\><br/\>";

The answer was a schoolboy error.

I had moved the site from one MAMP install to a fresh one, where the htaccess hadn't been changed to allow html files to be opened as PHP.

Still lots of errors in the code, but this was the underlying problem

Simple as that!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM