Jump to content

Looping through an array an not extracting any duplicates...


mkosmosports

Recommended Posts

Well, initially thats what Im doing:
$alltransferdates = @mysql_query("SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') FROM sf_team_player");

Then:

while($row = mysql_fetch_assoc($alltransferdates))
{
$transferdate[] = $row["DATE_FORMAT(START_DATE,'%Y%m')"];
$transferyear[] = substr($row["DATE_FORMAT(START_DATE,'%Y%m')"],0,4);
}

I need to have the full date in an array hence $transferdate[].
Then Im dropping the %Y values into a separate array $transferyear[] because I also need to extract all the unique year values later on, and I couldnt figure out another way to do it.
Link to comment
Share on other sites

Well, I think thorpe may have the best idea, then, but let me suggest this to you as an alternative: try selecting the month and year separately and combining them into your first array, then inserting only the year into the second, if it's not already there:
[code]
<?php
$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player";
$sql = mysql_query($q);
for ($i = 0; $i < mysql_num_rows($sql); $i++) {
  $date = mysql_result($sql, $i, 'date');
  $year = mysql_result($sql, $i, 'year');
  $transferdate[] = $date;
  if (!in_array($year, $transferyear)) $transferyear[] = $year;
}
?>
[/code]

You end up with the same results, but it's just a different approach. Hope this helps!
Link to comment
Share on other sites

Thanks Obsidian. I appreciate the time youve taken to suggest this method to me.

Ive tried the code you suggested originally and I get a php error on this line though:

if (!in_array($year, $transferyear)) $transferyear[] = $year; php is saying $transferyear is an undefined variable, I assume because its used before the array is defined? (I have global variables turned off, wonder if that could be why)

Then I thought you made a type, since shouldnt it check if $year exists in the $transferdate array:

  if (!in_array($year, $transferdate)) $transferyear[] = $year;

But that cant be it, because the $transfer year array no longer contains unique year values, it contains all of them.

I guess its the global variables then. You think?
Link to comment
Share on other sites

No, I think that all you need to do is declare your empty variables outside of the loop:
[code]
<?php
$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player";
$sql = mysql_query($q);
$transferdate = array();
$transferyear = array();
for ($i = 0; $i < mysql_num_rows($sql); $i++) {
  $date = mysql_result($sql, $i, 'date');
  $year = mysql_result($sql, $i, 'year');
  $transferdate[] = $date;
  if (!in_array($year, $transferyear)) $transferyear[] = $year;
}
?>
[/code]

Also, what the code is doing is [b]only[/b] adding the $year to the $transferyear array if it does not already exist in there. That's how it makes it a unique listing.
Link to comment
Share on other sites

Youre the man obsidian. And dont worry, soon I wont be asking about what I asked about in my last post...lol. Soon Ill be figuring those out. I should of figured it out this time around.

Your method is better for me because there is one more situation Id like to cover with it, and I have a gut feeling it will be easier.
The reason I need to know all of the unique years is because there is a page where Id like to give the user links to all available data in the years that I have covered in by db. Now, there is one more thing though. Those urls containing the unique year dates also contain month parameters. I want to find a way I can get the max month from each unique year value. So,
$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year FROM sf_team_player WHERE OLDCLUB_ID IS NOT NULL ORDER BY START_DATE DESC";
$alltransferdates = mysql_query($q);
$transferyear = array();
for ($i = 0; $i < mysql_num_rows($alltransferdates); $i++)
{
  $date = mysql_result($alltransferdates, $i, 'date');
  $year = mysql_result($alltransferdates, $i, 'year');
  $transferdate[] = $date;
  if (!in_array($year, $transferyear)) $transferyear[] = $year;
}

Then on the page where I want the dynamic links I have a loop which returns the number of unique years in the form of links.

foreach ($transferyear as $tryear)
{
echo("<a href=\"players.html?view=transfers&season=$tryear&in=$in\">$tryear</a>");
}

Now though, Id like to replace the $in parameter with the max month for that unique year.

Now, Ive tried some things, but Ive had no success. Im thinking I need to make $transferyear a multidimensional array containing also the max month for each year, but I cant get it together. Any ideas?

Thanks!
Link to comment
Share on other sites

OK, I'll try not to leave you haning ;) ... Since you're already ordering by date DESC, you know that the first record you come across (the one from which you are populating your [b]year[/b] array) is going to have the latest date for that year. With that in mind, just modify your query to return the month as well, and you can have both together:
[code]
<?php
$q = "SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, YEAR(START_DATE) AS year, MONTH(START_DATE) AS month FROM sf_team_player WHERE OLDCLUB_ID IS NOT NULL ORDER BY START_DATE DESC";
$alltransferdates = mysql_query($q);
$transferyear = array();
$maxmonth = array();
for ($i = 0; $i < mysql_num_rows($alltransferdates); $i++) {
  $date  = mysql_result($alltransferdates, $i, 'date');
  $year  = mysql_result($alltransferdates, $i, 'year');
  $month = mysql_result($alltransferdates, $i, 'month');
  $transferdate[] = $date;
  if (!in_array($year, $transferyear)) {
    $transferyear[] = $year;
    $maxmonth[] = $month
  }
}
?>
[/code]

Then, once you have the two arrays, you can loop through them together and associate the year to the max month:
[code]
<?php
foreach ($transferyear as $key => $val) {
  $month = $maxmonth[$key];
  echo "$month is the max month for the year $val<br />\n";
}
?>
[/code]

Does this help some?
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.