Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Function 1

Status
Not open for further replies.

dagger2002

Programmer
Nov 23, 2004
172
US
Hi all have a quest, for the holy grail, to fix this script i am working on. All it needs to do is run this function i am calling everytime it runs. right now it os just running it once but reusing the numbers. here is the code

Code:
while($row = mysql_fetch_array($result)){
		echo '<p><a href="' . $row[fil_path] . '">' . $row[fil_title] . '</a>, ' . fileSizer($row[fil_path]) . '</p>' . "\r\n";
	}

and here is the function
Code:
function fileSizer($size){
	$sizer = round(filesize($row[fil_path])/1024);
	
	if($sizer > 1){
		$size = 'Less then 1';
	}
	else{
		$size = $sizer;
	}
	
	return $size;
}
ty in advanced
 
Code:
<?php
while	($row = mysql_fetch_array($result)){
		//grab the output of the filesizer function
		//for easy use later
		$size = filesizer($row['fil_path']);
		
        //consider using the heredoc syntax
		//remember to disambiguate your arrays with curly braces
		//remember to enquote your array element keys
		echo <<<EOL
	<p>
		<a href="{$row['fil_path']}">
			{$row['fil_title']}
		</a>,
		$size
	</p>

EOL;	//end of heredoc syntax

}

function fileSizer($file) {
	
	//key thing is to pass the file in, not the size
	//remember that variables are not innately global
	//so for the old code to work you would have had to
	//globalise the $row variable: not efficient
	
	//first check that the file exists
	//good practice
	if (!is_file($file)){
		return "File not found";
	}
	
	//grab the file size in bytes
	$sizeBytes = filesize($file);
	
	//convert to kiloBytes
	$sizeKB = $sizeBytes/1024;
	
	//use a ternary operation to return the right text string
	return ($sizeKB < 1) ? 'Less than 1KB' : $sizeKB . "KB";
}
?>
 
Ok that didn't work here is what i gathered from your code.

Code:
<?php

//	db connect info

?>
<?php

	function filesizer($file){
//		Bytes Size
		$sizeBytes = filesize($file);

//		KB Size
		$sizeKB = $sizeBytes / 1024;
		
		if($sizer > 1){
			$size = 'Less then 1';
		}
		else{
			$size = $sizeKB;
		}
		
		return $size;
		
	}

?>
<?php

	while($row = mysql_fetch_array($result){
		$size = fileSizer({$row['fil_path']});
		
		echo "
			<p>\r
			\t	<a href='" . {$$row['fil_path']} . "'>" . {$row['fil_title']} . "</a>, " . $size . ", \r
			</p>
		";
	}

?>

I also trid putting in your code as is with just a couple changes like a couple "

i am getting a blank screen. any other thoughts?
 
if you are getting a blank screen I would think that the db connection may be going wrong or error suppression is turned off.
try this code after completing the blank variables
Code:
<?php
$host = '';
$user = '';
$pwd = '';
$dbname = '';
$dbtable = '';
error_reporting(E_STRICT);
ini_set('display_errors', true);
mysql_connect($host, $user, $pwd) or die (mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("Select fil_path, fil_title from dbtable") or die(mysql_error());

while ($row = mysql_fetch_array($result)){
        //grab the output of the filesizer function
        //for easy use later
        $size = filesizer($row['fil_path']);
        
        //consider using the heredoc syntax
        //remember to disambiguate your arrays with curly braces
        //remember to enquote your array element keys
        echo '
    <p>
        <a href="'.$row['fil_path'].'">
            '.$row['fil_title'].'
        </a>,
        $size
    </p>
';   

}

function fileSizer($file) {
    
    //key thing is to pass the file in, not the size
    //remember that variables are not innately global
    //so for the old code to work you would have had to
    //globalise the $row variable: not efficient
    
    //first check that the file exists
    //good practice
    if (!is_file($file)){
        return "File not found";
    }
    
    //grab the file size in bytes
    $sizeBytes = filesize($file);
    
    //convert to kiloBytes
    $sizeKB = $sizeBytes/1024;
    
    //use a ternary operation to return the right text string
    return ($sizeKB < 1) ? 'Less than 1KB' : $sizeKB . "KB";
}
?>
 
ok i am still getting nothing

Code:
	function filesizer($file){
//		Bytes Size
		$sizeBytes = filesize($file);

//		KB Size
		$sizeKB = $sizeBytes / 1024;
		
		if($sizer > 1){
			$size = 'Less then 1';
		}
		else{
			$size = $sizeKB;
		}
		
		return $size;
		
	}

?>
<?php

	while($row = mysql_fetch_array($result){
		$size = fileSizer({$row['fil_path']});
		
		echo "
			<p>\r
			\t	<a href='" . {$$row['fil_path']} . "'>" . {$row['fil_title']} . "</a>, " . $size . ", \r
			</p>
		";
	}

?>

I have again tried both yours and my code

any thoughts?
 
nothing is the one thing i don't think you can get!

please try this code. you will get some output. pls also make sure that all the error settings in your php.ini are set to output information to the screen.

Code:
<?php
$host = '';
$user = '';
$pwd = '';
$dbname = '';
$dbtable = '';
error_reporting(E_STRICT);
ini_set('display_errors', true);
mysql_connect($host, $user, $pwd) or die (mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("Select fil_path, fil_title from dbtable") or die(mysql_error());

if (mysql_num_rows($result) == 0) {die ('no records to output');
while ($row = mysql_fetch_array($result)){
        //grab the output of the filesizer function
        //for easy use later
        $size = filesizer($row['fil_path']);
        
        //consider using the heredoc syntax
        //remember to disambiguate your arrays with curly braces
        //remember to enquote your array element keys
        echo '
    <p>
        <a href="'.$row['fil_path'].'">
            '.$row['fil_title'].'
        </a>,
        $size
    </p>
';   

}

function fileSizer($file) {
    
    //key thing is to pass the file in, not the size
    //remember that variables are not innately global
    //so for the old code to work you would have had to
    //globalise the $row variable: not efficient
    
    //first check that the file exists
    //good practice
    if (!is_file($file)){
        return "File not found";
    }
    
    //grab the file size in bytes
    $sizeBytes = filesize($file);
    
    //convert to kiloBytes
    $sizeKB = $sizeBytes/1024;
    
    //use a ternary operation to return the right text string
    return ($sizeKB < 1) ? 'Less than 1KB' : $sizeKB . "KB";
}
?>
 
ok now i am getting out put but not the right stuff. I am getting what i got when i started.

here is the code, thanks to jpadie:

Code:
<html>
<head></head>
<body>
<h1>Hello?</h1>


<?php
$host = "127.0.0.1";
$user = "";
$pwd = "";
$dbname = "";
$dbtable = "files";


error_reporting(0);
ini_set('display_errors', true);

mysql_connect($host, $user, $pwd) or die ("error");

mysql_select_db($dbname) or die("error2");

$query = "SELECT * FROM `files` WHERE `fil_active` = '1' ORDER BY fil_title ASC";
$result = mysql_query($query) or die("error 3");

	if (mysql_num_rows($result) == 0) {
		die('no records to output');
	}

while ($row = mysql_fetch_array($result)) {
        //grab the output of the filesizer function
        //for easy use later
        $size = filesizer($row['fil_path']);
        
        //consider using the heredoc syntax
        //remember to disambiguate your arrays with curly braces
        //remember to enquote your array element keys
        echo '
        
    <p>
        <a href="'.$row['fil_path'].'">
            '.$row['fil_title'].'
        </a>, ' . 
        $size . '
    </p>
';   

}

	
	
function fileSizer($size){
	$sizer = round(filesize($row[fil_path])/1024);
	
	if($sizer < 1){
		$size = 'Less then 1';
	}
	else{
		$size = $sizer;
	}
	
	return $size;
}


?>


</body>
</html>

and here is my output.

Code:
Hello?

Cop car pic , Less then 1

Garden Cafe menu 2/5-3/2 , Less then 1

PHP Info , Less then 1

PHP outside PHP folder , Less then 1

cop Car is 30 kb
 
it is not going to work for the reasons i gave above: variable scope

change the function to the way i posted it above. if you want to add a rounding function then change the return line to

Code:
 return ($sizeKB < 1) ? 'Less than 1KB' : round($sizeKB) . "KB";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top