Forum Moderators: coopster
I have a newsfeed archive page and what I am trying to achieve is to display the month and all the news items for that month and so forth. I have managed to figure how to do this (see code below), however I've used a cool traverse function because I'm using a 2 dimensional array.
What happens is that it displays the month and all the news items, however it display the word 'Array', very annoying, and am trying to find a way of getting rid of this.
This is what happens when I run the script
April 2004
2004-04-06
test 1
2004-04-06
test 2
2004-04-05
Array
Array
March 2004
2004-03-31
test 3
2004-03-31
test 4
2004-03-31
test 5
2004-03-31
test 6
Array
Array
and so forth.
Code:
function traverse($array)
{
if (gettype($array) == "array")
{
while (list($index, $subarray) = each($array))
{
$items[]=$subarray;
traverse($subarray);
}
echo $items[4].'<br>';
echo '<a href='.$items[2].'.html>'.$items[3].'</a><br>';
$num=count($items);
for ($x=0;$x<$num; $x++)
{
$tmp1[]=" '$items[$x]'";
if ( strcmp($items[$x],"Array")==0 )
{
$flag=1;
}
}
}
else
{
#echo "<font color=#669900>".htmlspecialchars($array)."</font>\n";
}
}
$result=mysql_query("SELECT MONTHNAME(prop_date) AS month, YEAR(prop_date) AS year,itemid,heading,prop_date
FROM newsfeed
ORDER BY prop_date DESC");
while ( $row = mysql_fetch_assoc($result))
{
// Create an index for you array like February 2004
$index = "{$row['month']} {$row['year']}";
// Push the current row into that index
$news_list[$index][] = $row;
}
// when you print them out
$previous_index = '';
foreach ( $news_list as $current_index => $news_array )
{
if ( $previous_index == $current_index )
{
echo $news_array['heading'];
}
else
{
echo '<b>'.$current_index.'</b>' .'<br>';
// call to the traverse function
traverse($news_array);
}
$previous_index = $current_index;
}
Many Thanks
:o)
See code, I've used the substr function...
function traverse($array)
{
if (gettype($array) == "array")
{
while (list($index, $subarray) = each($array))
{
$items[]=$subarray;
traverse($subarray);
}
$num=count($items);
for ($x=0;$x<$num; $x++)
{
$tmp1[]=" '$items[$x]'";
if ( strcmp($items[$x],"Array")==0 )
{
$flag=1;
$items[$x]=substr($items[$x],0,-5);
}
}
echo $items[4].'<br>';
echo '<a href='.$items[2].'.html>'.$items[3].'</a><br>';
}
else
{
#echo "<font color=#669900>".htmlspecialchars($array)."</font>\n";
}
}
$result=mysql_query("SELECT MONTHNAME(prop_date) AS month, YEAR(prop_date) AS year,itemid,heading,prop_date
FROM newsfeed
ORDER BY prop_date DESC");
while ( $row = mysql_fetch_assoc($result))
{
// Create an index for you array like February 2004
$index = "{$row['month']} {$row['year']}";
// Push the current row into that index
$news_list[$index][] = $row;
}
// when you print them out
$previous_index = '';
foreach ( $news_list as $current_index => $news_array )
{
if ( $previous_index == $current_index )
{
echo $news_array['heading'];
}
else
{
echo '<b>'.$current_index.'</b>' .'<br>';
traverse($news_array);
}
$previous_index = $current_index;
}