Page 1 of 1
Can not figure out this DATE query
Posted: Fri Jan 21, 2011 11:11 am
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?
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 11:12 am
by John Cartwright
Try
Code: Select all
$query = "select DISTINCT YEAR(sold_date) AS `sold_date` FROM Sold";
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 11:42 am
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.
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 12:05 pm
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>';
}
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 12:14 pm
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
)
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 12:24 pm
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).
Re: Can not figure out this DATE query
Posted: Fri Jan 21, 2011 12:44 pm
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!