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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there anything wrong with the code

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
Hi, I am trying to learn OO php, is there any thing in the following code, which is totaly wrong.



private function get_data($filename,$table_columns)
{
$return_array;
//$row = 1;
$handle = fopen(".$filename.", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$num1 == count($table_columns);
if ($num == $num1){
$att=array();

for ($c=0; $c < $num; $c++) {

$att[$table-colums[$c]]-$data[$c];
}

}

else
{
throw new RssParserException("No of colums in the table do not match number of colums in the array");
}




}
fclose($handle);
$returnarray[]=$att;
}
 
First of all, this is no OOP. OOP is about classes and objects. This is just normal PHP.

Second, i would like to try to help you but i'm not going to copy-paste your code an try figuring out what's wrong. Please supply an error-code or formulate your question better.

mcvdmvs
&quot;It never hurts to help&quot; -- Eek the Cat
 
there are several things wrong and/or out of place in the code you've provided. here are some:

Code:
private function get_data($filename,$table_columns)
        {

        [red]this line does nothing.[/red]
        $return_array;
        //$row = 1;

        [red]why do you have quotes around the file name?  what are the periods for?[/red]
        $handle = fopen(".$filename.", "r");
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                   $num = count($data);
                   [red]following line does nothing. == is for logical tests, not value setting.[/red]
                   $num1 == count($table_columns);
                if ($num == $num1){
                $att=array();
                
                   for ($c=0; $c < $num; $c++) {
                     
                     [red]this line does nothing.  no value is being set.  what are you trying to do?[/red]
                     $att[$table-colums[$c]]-$data[$c];
                }
            
                }
                
                else
                {
                    throw new RssParserException("No of colums in the table do not match number of colums in the array");
                }
            
            
            
            
            }
        fclose($handle);
        $returnarray[]=$att;    
        }



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Additionally:
Code:
private function get_data($filename,$table_columns)
        {

        $return_array;
        //$row = 1;

        $handle = fopen(".$filename.", "r");
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                   $num = count($data);
                   $num1 == count($table_columns);
                if ($num == $num1){
                $att=array();
                
                   for ($c=0; $c < $num; $c++) {
                     
                     $att[[COLOR=red]$table-colums[/color][$c]]-$data[$c];  [COLOR=red]Highly mispelled variable[/color]
                }
            
                }
                
                else
                {
                    throw new RssParserException("No of colums in the table do not match number of colums in the array");
                }
            
            
            
            
            }
        fclose($handle);

        [COLOR=red] Following line will store only the last array pulled out of the file - your $att array has been overwritten in each loop. In order to store all of the rows you will need to move it inside the while loop at the end[/color]
        $returnarray[]=$att;    
        }

Also, it may be a good idea to check if the file_exists() before attempting to open and read it. And your code will fail if the user did not pass the exact number of columns as you have data. A better way to handle that would be to loop through the columns of data and, if there is a matching column name, use it, otherwise use the column number as a the key for that value.

-T

signature.png
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top