0

It's a bit hard to explain here but I'll try my best to include all related information. You can ask me if you don't understand.

enter image description here

Above is the data I got and I wish to combine the same number (e.g 2016) to be displayed as shown below.

enter image description here

This is my code.

<?php
    $years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news WHERE status<>'deleted' and status<>'draft' ORDER BY years DESC");

    while($page=mysql_fetch_array($years))
    { ?>
        <div class="date">
            <div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['years']; ?></a></div>

            <div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['months']; ?></a></div> 
        </div>
<?php } ?>

This is my database enter image description here enter image description here

May I know how to combine the year? I used "GROUP BY" but cannot, it hides the 2nd row of data. Or should I compare the value and combine it? But I have no idea how to do this. Please help, thank you.

2 Answers 2

1

Try this coding...

You are get result value and apply in your html content.

     <?php
        $years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news");

        $result = array();

        while($page=mysql_fetch_array($years))
        {

            $year = $page['years']; 
            $month = $page['months'];

            $result[$year][] = $page['months'];

        }   

        if(isset($result)) {

        foreach($result as $key=>$year_array) { ?>

           <div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $key; ?></a></div>
           <?php 
           if(isset($year_array) && count($year_array) != 0) { 
                foreach($year_array as $month) { ?>

                     <div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $month; ?></a></div> 

            <?php } }
         } }   ?>
Sign up to request clarification or add additional context in comments.

9 Comments

I got the error message --> Function name must be a string
what line display the error and post the updated coding ?
sorry that is print_r($result); I have updated coding
Opps. I also didn't realized about the print_r. Now I got Array ( )
check your query return result.
|
0

Update your code like this:

$years = mysql_query("SELECT YEAR(event_date) as years, GROUP_CONCAT(DATE_FORMAT(event_date,'%b') SEPARATOR '<br />') as months from news GROUP BY YEAR(event_date)");
while($page=mysql_fetch_array($years))
{ ?>
    <div class="year"><?php echo $page['years']; ?></div>
    <div class="month"><?php echo $page['months']; ?></div>  <?php 
} ?>

2 Comments

So sorry because I didn't include my code completely. I need to filter the information by clicking the month. So my code is shown above(edited). When I click the month to filter, my URL reflected index.php?year=2016&month=Feb<br%20/>Jan
Check whether the code fetches the required format. Html you can bind According to your requirement

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.