Foreach loop using 1 array within 4 more

Results 1 to 3 of 3
  1. #1
    Account Upgraded | Title Enabled! passie is offline
    True MemberRank
    Jan 2005 Join Date
    The NetherlandsLocation
    710Posts

    Foreach loop using 1 array within 4 more

    Foreach loop using 1 array within 4 more
    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
    'Coding is not just a hobby, its a sort of lifestyle'


  2. #2
    Super Moderator Biesmen is online now
    Super ModRank
    Apr 2007 Join Date
    2,468Posts

    Re: Foreach loop using 1 array within 4 more

    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). https://www.php-fig.org/psr/psr-2/
    $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 WriteCodeOnline - PHP | write and run php code online 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']
    Forum Rules | Account Support | Subscribe
    RaGEZONE Facebook

    "The reason why people give up so fast is because they tend to look at how far they still have to go, instead of how far they have gotten."

  3. #3
    Account Upgraded | Title Enabled! passie is offline
    True MemberRank
    Jan 2005 Join Date
    The NetherlandsLocation
    710Posts

    Re: Foreach loop using 1 array within 4 more

    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.
    'Coding is not just a hobby, its a sort of lifestyle'



Advertisement