Can not figure out this DATE query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DBookatay
Forum Newbie
Posts: 4
Joined: Fri Jan 21, 2011 10:53 am

Can not figure out this DATE query

Post by DBookatay »

Code: Select all

// Nav Tabs
   $query = "select DISTINCT YEAR(sold_date) FROM Sold";
   $result = mysql_query($query); 
   $numrows = mysql_num_rows($result); 
   while($row = mysql_fetch_array($result)){ 

      $year = substr($row['sold_date'], 0, -6);
      $navTab .= '<a class="miniTabOn" href="index.php?date='.$year.'">'.$year.'</a>';
   }


I am trying to create a link for each year of items in my database, yet the text is not showing up...
The "nav tab" is, because I can see the background image from the class="miniTabOn".

Can anyone spot an error?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can not figure out this DATE query

Post by John Cartwright »

Try

Code: Select all

   $query = "select DISTINCT YEAR(sold_date) AS `sold_date` FROM Sold";
DBookatay
Forum Newbie
Posts: 4
Joined: Fri Jan 21, 2011 10:53 am

Re: Can not figure out this DATE query

Post by DBookatay »

John Cartwright wrote:Try

Code: Select all

   $query = "select DISTINCT YEAR(sold_date) AS `sold_date` FROM Sold";

Same thing. The background images are showing up but no text showing the years.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can not figure out this DATE query

Post by John Cartwright »

Change your loop to this to inspect the results and post the results

Code: Select all

while($row = mysql_fetch_array($result)){ 
   echo '<pre>'. print_r($row, true) .'</pre>';
}
DBookatay
Forum Newbie
Posts: 4
Joined: Fri Jan 21, 2011 10:53 am

Re: Can not figure out this DATE query

Post by DBookatay »

John Cartwright wrote:Change your loop to this to inspect the results and post the results

Code: Select all

while($row = mysql_fetch_array($result)){ 
   echo '<pre>'. print_r($row, true) .'</pre>';
}

Code: Select all

Array
(
    [0] => 2008
    [sold_date] => 2008
)

Array
(
    [0] => 2007
    [sold_date] => 2007
)

Array
(
    [0] => 2006
    [sold_date] => 2006
)

Array
(
    [0] => 2009
    [sold_date] => 2009
)

Array
(
    [0] => 2010
    [sold_date] => 2010
)

Array
(
    [0] => 2011
    [sold_date] => 2011
)


User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can not figure out this DATE query

Post by John Cartwright »

OK I see now, it's not the query at fault, it's your substr() operation. You are asking it to select a string starting at position 0, but not greater than 6 characters from the last character position. Since your string is only 4 characters long, it will return empty. So just remove the substr() and you should be fine (and update the variable reference).
DBookatay
Forum Newbie
Posts: 4
Joined: Fri Jan 21, 2011 10:53 am

Re: Can not figure out this DATE query

Post by DBookatay »

John Cartwright wrote:OK I see now, it's not the query at fault, it's your substr() operation. You are asking it to select a string starting at position 0, but not greater than 6 characters from the last character position. Since your string is only 4 characters long, it will return empty. So just remove the substr() and you should be fine (and update the variable reference).
That was it... Thank you!
Post Reply