Percent values won't display

I have an output.php webpage to display the results of a survey.

For each question, the page shows a table with the number of answers for each gender and choice. Works fine, displays correctly.
Beneath that is another table which should display the % values for each gender and choice.

I can’t see what is wrong, but I cannot get the correct % values in the % table.
If I set this in the code below:

${"menP$i"}[$j][$key] = 55.55;

Then all the values in the % tables show up as 55.55, but when I try to use my $percent variable, I get zero where I should have a number or 100% where it should be less.

${"menP$i"}[$j][$key] = $percent;

If I echo $percent to the screen, it has the correct values, but $percent will not go correctly in my

${"menP$i"}[$j][$key]

I can’t see why not, because, if I use a number, there is no problem!
For say, Q1 row 1 I get this echo output, which is correct:

$percent is 33
$percent is 0
$percent is 67
$percent is 0
$percent is 100

Any tips or pointers as to what I am doing wrong please??

${"menP$i"} = displayAllQs($Qnr);
	$lenmenP = count(${"menP$i"});
	//echo '$lenmenP is ' . $lenmenP;
	//exit();
	for($j=0; $j < $lenmenP; ++$j) {					
		foreach(${"menP$i"} as $data) {	
		//print_r($data) . '<br>';
		//echo '<br>';		
		foreach ($data as $key=>$value) {
		//echo '$key is ' . $key . '<br>';
		//echo '$value is ' . $value . '<br>';
	        if(is_int($value)) {
			$Totals = $data['Totals'];
		       $percent = round($value/$Totals, 2)*100;
			echo '$percent is ' . $percent . '<br>';
			//$data[$key] = $percent;
			//${"menP$i"}[$j][$key] = $percent;
			${"menP$i"}[$j][$key] = 55.55;
			//echo '${"menP$i"}[$j][$key] is ' . ${"menP$i"}[$j][$key] . '<br>';
							}											
						}														
					}
				}
				print_r(${"menP$i"}) . '<br>';
				echo '<br>';

Id need to walk through to see what it is outputting.

Thanks for your reply!

PHP and me are not good friends! I find PHP much more difficult than say, Python. I thought, “I already have ${“men$i”}, it can’t be difficult to convert the values in that array to pecentages as ${“menP$i”} !”

But it took me a day to make it work!!

I got it working in the end, but I still don’t know why the code above didn’t work!!
This gets the results of our little poll from MySQL and writes the data in html tables.

if(isset($_POST['studentnum'])) {
		$count = count($questions);
		for($i=1; $i<=$count; $i++) {
				//echo '$i is ' . $i . '<br>';
				$Qnr = 'Q' . $i;
				//echo '$Qnr = ' . $Qnr . '<br>';
				${'Q'.$i} = $questions[$i-1];	
				//echo 'Question is ' . $frage . '<br>';
				// should be an array of 3 arrays, 3 results, 1 for each gender
				${"men$i"} = displayAllQs($Qnr);
				//echo 'This is ${"men$i"} <br>';
				//$name = 'men' . $i;
				//echo 'results array is ' . $name . '<br>';
				//print_r(${"men$i"});
				//echo '<br>';
				${"menP$i"} = displayAllQs($Qnr);
				$lenmenP = count(${"menP$i"});
				echo '$lenmenP is ' . $lenmenP . '<br>';
				//exit();
				//$j = 0;
				foreach (${"menP$i"} as $data) {
				for ($j=0; $j<$lenmenP; ++$j){
					foreach (${"menP$i"}[$j] as $key=>$value) {
							//echo '$key is ' . $key . '<br>';
							//echo '$value is ' . $value . '<br>';
							if(is_int($value)) {
								$Totals = ${"menP$i"}[$j]['Totals'];
								//echo '$j is '. $j . ' key is '. $key . ' value is ' . $value . ' Totals is ' .$Totals . '<br>'; 
								$percent = round($value/$Totals, 4)*100;
								${"menP$i"}[$j][$key] = $percent;								
							}																						
						}
					}													
				}
			}		
		}
				
		include 'outputMultiQdata_sWeek3.html.php';
		exit();
Sponsor our Newsletter | Privacy Policy | Terms of Service