Page 1 of 1

Need Help with PHP code

Posted: Tue Oct 13, 2009 9:10 am
by mandp
Hi I'v recently installed a website but got warnings on site not sure how to fix.
Page titled menuvertical.php
<?php

set_time_limit(120);

//echo $number_articles_menu;
//echo "random=".$random_list;

//$file_handle = fopen(ARTICLES_DIR.".txt", "rb");

//echo ARTICLES_DIR;

$results = array();

// create a handler for the directory
$handler = opendir(ARTICLES_DIR);

// keep going until all files in directory have been read
$num_art=0;
while ($file = readdir($handler)) {

// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..') {
$results[] = $file;

//title here
$file_handle = fopen(ARTICLES_DIR.$file, "rb");

$i=0;
$letCount=-1;

while (!feof($file_handle) || $i==0) {

$line_of_text = fgets($file_handle);

$line_of_text = trim($line_of_text);

if ($i==0 && $letCount==-1) {
$letCount = strlen($line_of_text);


if ($letCount>5) {
$i=1;
$title=$line_of_text;
break;
} else {
$i=0;
$letCount=-1;
} //if $letCount


} //if i & letcount

} //while !feof


fclose($file_handle);
$desc[]=$title;

//title here


$num_art++;
}

}

// tidy up: close the handler
closedir($handler);









echo "<ul>";
if ($num_art>$number_articles_menu) {

if ($random_list==1) {

$low = 0;
$high = $number_articles_menu-1;
$step = 1;

$numbers = range($low,$high,$step);
shuffle($numbers);

//print_r($numbers);

$inum=0;

while ($inum<=$high) {

echo "<li><a href=\"article-".clean_url(substr($results[$numbers[$inum]],0,-3)).".html\" alt=\"".$desc[$numbers[$inum]]."\">" . $desc[$numbers[$inum]] ."</a></li>";$inum++;
}

} else {


$inum=0;
while ($inum<=$num_art-3) {
echo "<li><a href=\"article-".clean_url(substr($results[$inum],0,-3)).".html\" alt=\"".$desc[$inum]."\">" . $desc[$inum] ."</a></li>";$inum++;
}

} //random

} else {

$inum=0;

while ($inum<=$num_art-3) {

echo "<li><a href=\"article-".clean_url(substr($results[$inum],0,-3)).".html\" alt=\"".$desc[$inum]."\">" . $desc[$inum] ."</a></li>";$inum++;


}
}

echo "</ul>";
?>

This is the warnings I'm getting.
Warning: Wrong parameter count for range() in /home/mandp0/public_html/computer/menuvertical.php on line 88

Warning: shuffle() expects parameter 1 to be array, null given in /home/mandp0/public_html/computer/menuvertical.php on line 89

If anyone could help I would greatly appreciate it.
Thanks
mandp

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:13 am
by Weiry
Please edit your post and wrap your code with the

Code: Select all

[ /php] tags.
It's very time consuming to count 88 lines

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:15 am
by Eric!
Please use the [syntax=php]paste your code here[/syntax] tags so people can read your code.

You didn't post either function range or shuffle. There is probably something not correct being sent to range and thus it doesn't return an array which causes shuffle to generate a warning.

I would guess that the value of $number_articles_menu is 0 or 1 that is causing the problem.

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:21 am
by mandp
Page titled menuvertical.php

Code: Select all

<?php
 
set_time_limit(120);
 
//echo $number_articles_menu;
//echo "random=".$random_list;
 
//$file_handle = fopen(ARTICLES_DIR.".txt", "rb");
 
//echo ARTICLES_DIR;
 
$results = array();
 
// create a handler for the directory
$handler = opendir(ARTICLES_DIR);
 
// keep going until all files in directory have been read
$num_art=0;
while ($file = readdir($handler)) {
 
// if $file isn't this directory or its parent, 
// add it to the results array
if ($file != '.' && $file != '..') {
$results[] = $file;
 
//title here
$file_handle = fopen(ARTICLES_DIR.$file, "rb");
 
$i=0;
$letCount=-1;
 
while (!feof($file_handle) || $i==0) {
 
$line_of_text = fgets($file_handle);
 
$line_of_text = trim($line_of_text);
 
if ($i==0 && $letCount==-1) {
$letCount = strlen($line_of_text);
 
 
if ($letCount>5) {
$i=1;
$title=$line_of_text;
break;
} else {
$i=0;
$letCount=-1;
} //if $letCount
 
 
} //if i & letcount
 
} //while !feof
 
 
fclose($file_handle);
$desc[]=$title;
 
//title here
 
 
$num_art++;
}
 
}
 
// tidy up: close the handler
closedir($handler);
 
 
 
 
 
 
 
 
 
echo "<ul>";
if ($num_art>$number_articles_menu) {
 
if ($random_list==1) {
 
$low = 0;
$high = $number_articles_menu-1;
$step = 1;
 
$numbers = range($low,$high,$step);
shuffle($numbers);
 
//print_r($numbers);
 
$inum=0;
 
while ($inum<=$high) {
 
echo "<li><a href=\"article-".clean_url(substr($results[$numbers[$inum]],0,-3)).".html\" alt=\"".$desc[$numbers[$inum]]."\">" . $desc[$numbers[$inum]] ."</a></li>";$inum++;
}
 
} else {
 
 
$inum=0;
while ($inum<=$num_art-3) {
echo "<li><a href=\"article-".clean_url(substr($results[$inum],0,-3)).".html\" alt=\"".$desc[$inum]."\">" . $desc[$inum] ."</a></li>";$inum++;
}
 
} //random
 
} else {
 
$inum=0;
 
while ($inum<=$num_art-3) {
 
echo "<li><a href=\"article-".clean_url(substr($results[$inum],0,-3)).".html\" alt=\"".$desc[$inum]."\">" . $desc[$inum] ."</a></li>";$inum++;
 
 
}
}
 
echo "</ul>";
?>
This is the warnings I'm getting.
Warning: Wrong parameter count for range() in /home/mandp0/public_html/computer/menuvertical.php on line 88

Warning: shuffle() expects parameter 1 to be array, null given in /home/mandp0/public_html/computer/menuvertical.php on line 89

Is this what they mean by wrap the code?

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:25 am
by Eric!
To repeat:
Eric! wrote:You didn't post either function range or shuffle. There is probably something not correct being sent to range and thus it doesn't return an array which causes shuffle to generate a warning.

I would guess that the value of $number_articles_menu is 0 or 1 that is causing the problem.

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:29 am
by Weiry
in the code you have posted, you have not declared what $number_articles_menu value is.
As such, your range() will not work because it requires an input from $high which is $number_articles_menu-1... but currently i do not know if $number_articles_menu even has a value

What happens if you manually set $number_articles_menu to a predefined number (for debugging purposes only)

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 9:49 am
by mandp
I'v tried changing $number_articles_menu to a number value and nothing changes

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 10:04 am
by Weiry
Well im not sure what your problem could be... because i just tested this and it worked fine.

Code: Select all

$number_articles_menu = 20;
$low = 0;
$high = $number_articles_menu-1;
$step = 1;
 
$numbers = range($low,$high,$step);
print "<pre>";
print_r($numbers);
print "</pre>";
with the output:

Code: Select all

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
    [15] => 15
    [16] => 16
    [17] => 17
    [18] => 18
    [19] => 19
)

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 10:49 am
by mandp
I've tried putting in what you had there and still getting this warning.
Warning: Wrong parameter count for range() in /home/mandp0/public_html/car/menuvertical.php on line 89

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 1:58 pm
by Eric!
I had never seen the range() function before, so I didn't recognize it as a php function.

It appears you are using an older version of PHP something less than version 5. PHP4 doesn't support the STEP parameter. You could either remove the step parameter or upgrade your php. http://php.net/manual/en/function.range.php

Re: Need Help with PHP code

Posted: Tue Oct 13, 2009 3:18 pm
by mandp
Thank you Eric

All I had to do was delete PHP5 on my hosting and then reinstalled it and everything is working.

Thanks again
mandp