Forum Moderators: coopster

Message Too Old, No Replies

Is this calculating correctly?

         

Numberman

12:04 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



<?php
function getNews($num)
{
$intNum = $num;
$intTotal = count($title);
$intResult = $intTotal - $intNum;
return $intResult;
}
?>

I have a MySQL database. The database has a table which contains 3 field; title, date, and content. The table is opened by my script and dumps the contents into three arrays; $title, $date, and $content. Now, I know it's putting it into the arrays because $title[1] contains the contents of the first row of the title field, and so on. The database has 4 rows.

Now, this is great, but it displays my news the wrong way round. I want it to display the last row first, so I made this function to calculate the last contents of the title array. As each array uses the same identifier (i.e. $title[1] goes with $date[1] goes with $content[1]) this should produce a result that works for all the arrays, but it doesn't.

If I have

<?php echo(getNews(0));?>
it actually prints '0'. If I do
<?php echo(getNews(1));?>
it prints '-1'. What the heck is going on?

Blackie

12:08 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



You forgot to make $title global so the value of it is zero each time the function is called.

make first line in the function:
global $title;

dreamcatcher

1:22 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also pass the value of $title into the function:

<?php
function getNews($num,$title)
{
$intNum = $num;
$intTotal = count($title);
$intResult = $intTotal - $intNum;
return $intResult;
}
?>

dc