Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The while loop works fine in this example.



PHP
                $query="SELECT * FROM `ex`";
    $result=mysqli_query($link, $query);

    $Q_X='Q1';
                $res=array(0,0,0,0,0);
            while($report=mysqli_fetch_array($result)){
                print_r($report[$Q_X].'<br>');

                if($report[$Q_X]=='X1'){
                    $res[0]=$res[0]+1;

                }elseif($report[$Q_X]=='X2'){
                    $res[1]=$res[1]+1;

                }elseif($report[$Q_X]=='X3'){
                    $res[2]=$res[2]+1;

                }elseif($report[$Q_X]=='X4'){
                    $res[3]=$res[3]+1;

                }elseif($report[$Q_X]=='X5'){
                    $res[4]=$res[4]+1;
                }
            }
list($Q1_1,$Q1_2,$Q1_3,$Q1_4,$Q1_5) = $res;

echo $Q1_1.',';
echo $Q1_2.',';
echo $Q1_3.',';
echo $Q1_4.',';
echo $Q1_5;



but when I try to put it in a function in doesn't work



PHP
               $query="SELECT * FROM `ex`";
    $result=mysqli_query($link, $query);

        function count_vote($Q_X) {
            $cont=0;

            $res=array(0,0,0,0,0);
            while($report=mysqli_fetch_array($result)){

                if($report[$Q_X]=='X1'){
                    $res[0]=$res[0]+1;

                }elseif($report[$Q_X]=='X2'){
                    $res[1]=$res[1]+1;

                }elseif($report[$Q_X]=='X3'){
                    $res[2]=$res[2]+1;

                }elseif($report[$Q_X]=='X4'){
                    $res[3]=$res[3]+1;

                }elseif($report[$Q_X]=='X5'){
                    $res[4]=$res[4]+1;
                }
            }
            if(mysqli_num_rows($result)==$cont){
            return $res;
            }
        }


count_vote("Q1");
list($Q1_1,$Q1_2,$Q1_3,$Q1_4,$Q1_5) = $res;

echo $Q1_1.',';
echo $Q1_2.',';
echo $Q1_3.',';
echo $Q1_4.',';
echo $Q1_5;

I using an array to get multiple values returned

and if to prevent the function from exiting before the while loop ending

I am trying to make it into a function to use it multiple times on Q1,Q2,..

can someone help me find the problem

-----------------mysql table-----------------

................... Q1 . Q2 . Q3 . Q4
----------------------------------------
......user1 | X1 . X3 . X5 . X4

......user2 | X2 . X2 . X3 . X2

......user3 | X1 . X1 . X1 . X1


I want to get this resort

Q1---> X1 >2
---> X2 >1
---> X3 >0
---> X4 >0
---> X5 >0

Q2---> X1 >1
---> X2 >1
---> X3 >1
---> X4 >0
---> X5 >0
Q3,Q4....


while loop does not work in function php

What I have tried:

PHP
 <?php include '../mysqCode/masterlink.php';mysqli_set_charset($link,'utf8');
	if (mysqli_connect_error()){
		die("Coud not connect to Database,server error");

	}else{
		
$query="SELECT * FROM `ex`";
$result=mysqli_query($link, $query);
			        
function count_vote($result ,$Q_X)
{
    $res = array(0, 0, 0, 0, 0);
    while ($report = mysqli_fetch_array($result)) {
        if ($report[$Q_X] == 'X1') {
            $res[0] = $res[0] + 1;

        } elseif ($report[$Q_X] == 'X2') {
            $res[1] = $res[1] + 1;

        } elseif ($report[$Q_X] == 'X3') {
            $res[2] = $res[2] + 1;

        } elseif ($report[$Q_X] == 'X4') {
            $res[3] = $res[3] + 1;

        } elseif ($report[$Q_X] == 'X5') {
            $res[4] = $res[4] + 1;
        }
    }
    if (mysqli_num_rows($result) > 1) {
        return $res;
    }
}



list($Q1_1, $Q1_2, $Q1_3, $Q1_4, $Q1_5) = count_vote($result ,"Q1");

echo $Q1_1 . ',';
echo $Q1_2 . ',';
echo $Q1_3 . ',';
echo $Q1_4 . ',';
echo $Q1_5;

list($Q2_1, $Q2_2, $Q2_3, $Q2_4, $Q2_5) = count_vote($result ,"Q2");

echo $Q2_1 . ',';
echo $Q2_2 . ',';
echo $Q2_3 . ',';
echo $Q2_4 . ',';
echo $Q2_5 . '<br>';
		
		
		    
		    
		    
		    
	}
	
	
		    
?>


the fist one work but the rest does not
Posted
Updated 18-Jun-16 8:19am
v8

1 solution

We don't know the states of the objects $report and $result at the moment of your function call.

If you want to see what exactly is wrong, or what makes the two cases different, use the debugger. The whole idea of passing data through the outer context is wrong, even when it might work. You really need to pass the reference to these objects as function parameters. A valid alternative way is passing data/reference through class members, either explicitly, via having a function parameter of some class type, or implicitly, via making your function the member of the same class as the objects used in the function implementation, which then would be accessed through "this".

—SA
 
Share this answer
 
v2
Comments
Moaz Walid Mabrok 18-Jun-16 14:00pm    
I Improved the question `What I have tried `
Sergey Alexandrovich Kryukov 18-Jun-16 14:02pm    
You made $result a parameter, but this is not enough.
Generally, your code is way too hard-coded to be maintainable.
—SA
Moaz Walid Mabrok 18-Jun-16 14:05pm    
can you fix the code as a Solution thanks
Sergey Alexandrovich Kryukov 18-Jun-16 14:11pm    
No, because I don't have all your context. I gave you exact instructions, please follow them. This is exactly what "answering a question" means.
—SA
Moaz Walid Mabrok 18-Jun-16 14:31pm    
`http://phpcodechecker.com` did not find anything wrong with the code, and I am sorry but I do not understand the part about the `class members` and `making your function the member of the same class as the objects` - If I know how to do it wouldn't ask -

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900