• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Foreach loop using 1 array within 4 more

Status
Not open for further replies.
Skilled Illusionist
Joined
Jan 14, 2005
Messages
395
Reaction score
2
Hello,

I need to make overview of a question list but somehow only 'fase2' is shown while there is data for fase1 but it loads in weirdly (see code bellow), fase2 but fase1 is shown up as a large empty array while fase2 does got data?

fase1 array data
Code:
Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

if anyone got a clue what i am doing wrong please let me know because i cant figure it out.

Code:
echo '<table width="40%" align="center" id="vragenlijst">
<tr>
<td width="50%">Vragen</td> 
<td width="10%" align="center">Fase 1</td> 
<td width="10%" align="center">Fase 2</td> 
<td width="10%" align="center">Fase 3</td> 
<td width="10%" align="center">Fase 4</td> 
</tr>';
						
foreach(fetchresults($naam, false) as $row){

if(!empty($row)){

print_r($row); //this does show fase1 and fase2 containing data

$fase1 = array();
$fase2 = array();
$fase3 = array();
$fase4 = array();

if($row['fase'] == '1'){	
	$fase1 = explode(",", $row['antwoorden']);
} elseif($row['fase'] == '2'){	
	fase2 = explode(",", $row['antwoorden']);
} elseif($row['fase'] == '3'){	
	$fase3 = explode(",", $row['antwoorden']);
}elseif($row['fase'] == '4'){	
	$fase4 = explode(",", $row['antwoorden']);
}

} else {
	echo "Geen resultaat.";
	$weergave = true;
}
					
}
					
foreach(vragenlijst(2) as $vraag){
						
$newvraag = explode("=", $vraag);
						
echo '<tr>
<td width="10%" align="left">'.$newvraag[0].'</td>
<td width="10%" align="center">'.$fase1[$i].'</td> 
<td width="10%" align="center">'.$fase2[$i].'</td>
<td width="10%" align="center">'.$fase3[$i].'</td>
<td width="10%" align="center">'.$fase4[$i].'</td> 
</tr>';
$i++;
}
						
echo '</table>';

print_r($row); result

Code:
Array ( [id] => 2 [medewerker] => 1 [antwoorden] => 1=1,2=1,3=1,4=1,5=1,6=1,7=1,8=1,9=1,10=1,11=1,12=1,13=1,14=1,15=1,16=1,17=1,18=1,19=1,20=1,21=1,22=1,23=1,24=1,25=1,26=1,27=1,28=1,29=1,30=1,31=1,32=1,33=1,34=1,35=1,36=1,37=a,38=1,39=a [fase] => 1 [date] => 2020-04-14 10:02:18 ) Array ( [id] => 3 [medewerker] => 1 [antwoorden] => 1=1,2=1,3=1,4=1,5=1,6=1,7=1,8=1,9=1,10=1,11=1,12=a,13=a,14=1,15=1,16=1,17=1,18=1,19=1,20=1,21=1,22=1,23=1,24=1,25=a,26=a,27=1,28=1,29=1,30=1,31=1,32=1,33=1,34=1,35=1,36=1,37=1,38=1,39=1,40=1,41=a,42=1,43=a,44=1,45=1 [fase] => 2 [date] => 2020-04-14 10:04:15 )

if something is unclear please ask,

thanks in advance.

- aristonia
 
Super-Moderator
Staff member
Super-Moderator
Joined
Apr 28, 2007
Messages
1,502
Reaction score
760
I assume you mean "implode" rather than "explode". Implode is to convert an array to a string, explode is to convert a string to an array.

Code:
$string = 'r,a,g,e,z,o,n,e';
$explodedString = explode(',', $string);
print_r($explodedString);
Returns:
Code:
Array ( [0] => r [1] => a [2] => g [3] => e [4] => z [5] => o [6] => n [7] => e )
Code:
$array = ['r','a','g','e','z','o','n','e']; // According to PSR-2, you can use the brackets [] instead of array(). Only use array() if you plan on supporting a PHP version lower than 5.4 (which is really outdated). [URL]https://www.php-fig.org/psr/psr-2/[/URL]
$implodedArray = implode(',', $array);
print_r($implodedArray);
Returns a string:
Code:
r,a,g,e,z,o,n,e

You can copy and paste my snippets in to test it yourself.


If you want $fase1 and $fase2 and such to be an array, you should only define the value of that variable as the value (result) of $row['antwoorden']:
Code:
$fase1 = $row['antwoorden']
 
Skilled Illusionist
Joined
Jan 14, 2005
Messages
395
Reaction score
2
Believe it or not but my script wasn't the issue; i was running OpenVPN and it messed up the $_SESSION which messed up everything.

topic can be closed.
 
Status
Not open for further replies.
Back
Top