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



                $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



               $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 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 9: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
   
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 -
Sergey Alexandrovich Kryukov 18-Jun-16 15:06pm
   
Which part you don't understand? Do you know how to write and use classes? $this, $this->..?
Do you understand the first alternative: just passing all you need through parameters?
—SA
Moaz Walid Mabrok 18-Jun-16 15:13pm
   
no, i don't know how to write classes ,this is my tenth day since started to lean PHP
Sergey Alexandrovich Kryukov 18-Jun-16 17:54pm
   
All right, you will learn it later. And than, please come back to my answer.

Ten days is a very short period of time. Then I would advise to shift the way you learn. Until you read some PHP book or tutorial, without skipping a word, do as simple exercises as you can, mainly on the topics of each chapter you learn. It should come with learning the basics of Web and general programming. Only after that phase you will be able to do something more complicated more efficiently and ask questions in more productive way. Don't rush it, learn the fundamentals more thoroughly, focus on the fundamentals. It will pay off soon enough. And if you miss some fundamentals now, your future work may turn just a waste of time; don't make this mistake.

—SA
Moaz Walid Mabrok 19-Jun-16 4:43am
   
Thanks, can you recommend a place to get in-depth PHP and python ,I have been using udemy and lynda
Sergey Alexandrovich Kryukov 19-Jun-16 10:03am
   
Sorry, no. I directly used http://php.net and https://www.python.org, but what one needs depends on previous education/experience.
—SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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