0

I have two columns in my database, year and content i want to group the content by year (i have a lot of content and many years not only 2015 and 2016 as the example )

so i'll have this output in html

<div class="all">
  <div class="2016">
    2016
      <div class="content_2016">
          All the content of the column that is in the same ligne as 2016 in the database
    </div>
  </div>
  
  <div class="2015">
    2015
      <div class="content_2015">
          All the content of the column that is in the same ligne as 2015 in the database
    </div>
  </div>
</div> 

<?php
	$query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi ";
	$result = mysqli_query($connection, $query);

	$previous =0;
	while ($val = mysqli_fetch_array($result))
	{

		if ($previous <> $val['pub_year'])
		{
			$previous = $val['pub_year'];
			$year = $previous;

			echo $year;
			echo  '<br>';


			$Temp = highlight("person1",$val['pub_publi'],"0000FF");
			$Temp = highlight("person2",$Temp,"0000FF");
			$Temp = highlight("person3",$Temp,"0000FF");
			$Temp = highlight("person4",$Temp,"0000FF");
			$Temp = highlight("person5",$Temp,"0000FF");
			$Temp = highlight("person6",$Temp,"0000FF");
			$Temp = highlight("person7",$Temp,"0000FF");
			$Temp = highlight("person8",$Temp,"0000FF");
			$Temp = highlight("person9",$Temp,"0000FF");
			$Temp = highlight("person10",$Temp,"0000FF");


			echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in  for ' . $val['pub_publi'] . '"></a>';
   		echo $Temp;
			echo  '<br>';

			
					}
			
			
				}



?>

It is outputing the years right

2016

2015

2014

etc...

but it is only showing the first ligne of each year not all the content for each year.

4
  • First get data for year 2015 and show in 1st block then fetch data for year 2016 and show in 2nd block; Commented Mar 22, 2016 at 9:09
  • Yeah this is what i tried but i keep having the year multiple times with every new content or i get just one ligne for each year and not the others Commented Mar 22, 2016 at 9:20
  • Where is your SQL query? Show that please and include some info about the table structure. Commented Mar 22, 2016 at 9:29
  • i've added the SQL query Commented Mar 22, 2016 at 13:09

2 Answers 2

1
  1. Run the following query:

    Select Content From table order by year
    
  2. echo like following (or concat):

    while rs.hasNext(){
    if (currentYear != lastyear){ 
        echo div #with year As Class;
    } 
    echo row # a single entry the way you want it dispalyed;       
    lastYear = currentYear; 
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

what u are asking is huge chunk to write. so here are steps:

  1. create array with this SQL statement below and fetch assoc in PHP

    select distinct on year * from table

  2. now use foreach($years as $year) on array and fetch content

    select content from table where year=$year

  3. to sandwich your code output in SQL keep concatenating like

$html = "";

$html .= "<div class=\"all\">";

foreach($years as $year){

$html .= "<div class=\"content_$year\">";

//then content fetched from second sql query

$html .= "</div>";

}

$html .= "</div>";

how to fetch data in sql | PHP.NET MANUAL

how to join/concat in php | PHP.NET MANUAL

LATER ADDITIONS, SNIPPET FROM MY OWN CODE :

$letter = $_POST['letter'];
$query_out = "SELECT link_id,singer_url FROM letter_mapping WHERE letter = '$letter'";
if($result_out = $conn_out->query($query_out)){
    if($result_out->num_rows > 0){
        while($row = $result_out->fetch_assoc()) {
            $link_id = $row['link_id'];
            $singer_url = $row['singer_url'];
            /*
            foreach($row as $name=>$value){
            }
            */
        }
    }
$result_out->free();
}
if($conn_out->close()){
    // Message for disconnect
    //echo "Status : DISCONNECTED<br/>";
}

2 Comments

Would be better to just sort the Content by the year, and then check if the year in the current entry is different then the year in the last entry. would only be 1 SQL call
@DoktorOSwaldo very true i could have did the same but it was hard to tell in tutorial , i tried to broke it in as many part as i can , so atleast he can achieve his code with no extra questions lying over SO

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.