Jump to content

Singular Year Ordering from query, help!


luminous

Recommended Posts

Hi,

 

I have a query which returns a list of results from a database. One of the values in the Array is a 'Year' value. What I'm attempting to do is I want to order the array by the year and echo it client-side but I want values with the same year to be displayed under the year just once. Example:

 

2009

 

Value

Value

Value

 

with my current function(below) i'm getting. Example

 

2009

Value

 

2009

Value

 

2009

Value

 

function exhibition_sort($artists)
{
	if(!empty ($artists[0]['exhibitioname']))
	{
		for($i=0; $i< count($artists) ; $i++)
			{
				if($artists[$i]['Year'] < 2009)
					{
							echo "<p class=\"Year\">".$artists[$i]['Year']."</p><br/>\n";
						echo "<strong>".$artists[$i]['exhibitioname']."</strong><br/>\n";
						echo "<strong>Start Date:</strong> ".$artists[$i]['startdate']."<br/>\n";
						echo "<strong>End  Date:</strong> ".$artists[$i]['enddate']."<br/>\n";
						echo "<strong>End  Date:</strong> ".$artists[$i]['gallery']."<br/>\n";
						echo "<stong>Address:</strong>".$artists[$i]['Address']."</br>\n";
						echo "<br/>\n";
					}else
					{
							echo "<p class=\"Year\">".$artists[$i]['Year']."</p><br/>\n";
						echo "<strong>".$artists[$i]['exhibitioname']."</strong><br/>\n";
						echo "<strong>Start Date:</strong> ".$artists[$i]['startdate']."<br/>\n";
						echo "<strong>End  Date:</strong> ".$artists[$i]['enddate']."<br/>\n";
						echo "<strong>End  Date:</strong> ".$artists[$i]['gallery']."<br/>\n";
						echo "<stong>Address:</strong>".$artists[$i]['Address']."</br>\n";
						echo "<br/>\n";
					}
			}

	}else	
			{
				return false;
			}

}

How would I go about getting things working according to the first example?

 

Thanks!

 

 

 

 

Link to comment
Share on other sites

There's a simple formula to follow for these kinds of outputs.

Have a holding value to remember the last value you are listing once (in this case the year)

Then before outputting the content for the value you want displaying once, make sure it doesn't equal the holding value.

If it doesn't then output the value and assign the value to the holding value. The next time you loop if the value equals the holding value it wont display it

 

$year = '';
while($row = mysql_fetch_assoc($result)) {
    if($row['year'] != $year) {
        echo '<h2>'.$year.'</h2>';
        $year = $row['year'];
    }
}

I've not integrated it into your code to give you a chance at doing it. It's not difficult even for a beginner so long as you understand the logic of it. Give it a try and see how you get on

Link to comment
Share on other sites

Hey i gave this ago just now with some better results however,  I'm now only getting one result under each of the years and the year won't display less than 2008!

 

function find_exhibitions()
{

	$query = sprintf("SELECT * FROM EXHIBITIONS");
	$result = mysql_query($query);
	$year = '2009';

	while($row = mysql_fetch_assoc($result))
	{

		for($i=0; $i < count($row) ; $i++)

			if($row[$i]['Year'] < 2009) 
				{
				 echo '<h2>'.$year.'</h2>';
				 echo $row[$i]['exhibitioname'];
				 $year = $row[$i]['Year'];

				}else
				{
					return false;
				}
		}

	}

can you tell me what i'm doing wrong?

Link to comment
Share on other sites

Try this

function find_exhibitions() {

$query = sprintf("SELECT * FROM EXHIBITIONS ORDER BY `Year`");
$result = mysql_query($query);
$year = ''; #Set the year holder to nothing by default

//Loop through each row of data
while ($row = mysql_fetch_assoc($result)) {
	//Check if the last year output is the same
	if ($year != $row['Year']) {
            //Assign year to holding var
		$year = $row['Year'];
            //echo Year heading
		echo '<h2>'.$year.'</h2>';
	}
        //Echo exhibition details
	echo $row['exhibitionname'];
}
}

I've also ordered by year.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.