Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
please can you help me
Old 05-05-2012, 04:12 PM please can you help me
Junior Talker

Posts: 1
Trades: 0
i want to calculate the summation of sal values as follow as an ex:
Code:
id             name          sal
1             ghada           4
2             ahmad           4
these data comes from csv file i did before,i wrote the code but something is wrong and this is the code:

PHP Code:
private function open(){
        
$sum=0;
        
$this->base.=$_REQUEST["item"];
        
$file_handler=fopen($this->base"r");
        while(
$row=fgetcsv($file_handler,1024,";")){
            
            
$this->data[]=$row;
            
            for (
$i 1$i <= 6$i++) {
                
                
$sum +=$this->data[$i][2];
            
                
                }
                
                
        }
        
        
print_r($sum);
        
        
        
$this->view="open.php";
    } 
but the result is 168 instead of 8

Last edited by Physicsguy; 05-05-2012 at 07:52 PM.. Reason: [code] tags
jarabaaghada is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-05-2012, 06:14 PM Re: please can you help me
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I don't really understand your code, it's kinda' messy, out of context and there's way to litle information given. My guess is that you don't want that for loop within the while loop, but rather outside.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-07-2012, 03:48 AM Re: please can you help me
King Spam Talker

Posts: 1,090
Name: Paul W
Trades: 0
Quote:
Originally Posted by lizciz View Post
I don't really understand your code, it's kinda' messy, out of context and there's way to litle information given. My guess is that you don't want that for loop within the while loop, but rather outside.
Or rather nowhere!

$row has your data, one row at a time, as print_r would show.
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 06-12-2012, 11:28 PM Re: please can you help me
Registered Unverified User

Posts: 4
Trades: 0
Hi jarabaaghada,

This is a simple code I used before which could be helpful to you. I modified it the way you can use, just change the variables that you need be changed, like the name of the file and I noticed you are using class, so just modify this the way you can access your variables.

PHP Code:

function open(){
    
$sum=0;
    
    
$file "sample.csv";
    
    
$filesize filesize($file);
    
    
$file_handler=fopen($file"r");
    
    
$sum 0;
    
    while(
$row=fgetcsv($file_handler$filesize",")){
            
$sum += (int)$row[2];
    }
    
    echo 
$sum;

You can test it locally by creating the sample csv file and calling the function I created.

Basically, you just have access the sal value by getting the value of index 2 of your $row since the $row already contains it, so you no more need to loop on it.

Hope this helps.

febrarian
febrarian is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to please can you help me
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 8.13571 seconds with 11 queries