Reputation: 155
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.
Upvotes: 0
Views: 507
Reputation: 679
what u are asking is huge chunk to write. so here are steps:
create array with this SQL statement below and fetch assoc in PHP
select distinct on year * from table
now use foreach($years as $year) on array and fetch content
select content from table where year=$year
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/>";
}
Upvotes: 1
Reputation: 6016
Run the following query:
Select Content From table order by year
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;
}
Upvotes: 1