Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point what must to be changed on this PHP code to make object values to be reasigned ?

Hi Experts

Could you point what must to be changed on this PHP code to make object values to be reasigned ?

Accordingly to:
<?

		if ( $bd1->qBD($query6) )
		{
	
			while ($reg6 = $bd1->obtenerRegA())
			{
				
				//print_r('---------------------');
				//print_r($reg6['nome_completo']);
				//print_r('---------------------');
				
				$id_prof = $reg6['id_profissional'];

				if($id_prof = $id_prof_anterior)
				{
						
					//----------------------------------------------
					$obj_sh = array(
							'date' => $reg6['data_entrada_realizada'],
							'period_name' => $reg6['tipo_profissional'],
							'tags' => array('ESC:' )
						);
					
					$data = $obj_sh;
					//----------------------------------------------
					
					$ct++;	
						
						
				}
				else
				{
						// Obtém estas informações apenas na 1.a passagem
						$name =  $reg6['nome_completo'];
						$nickname='';	
						$email= $reg6['email_profissional'];
						$cellphone= $reg6['celular_profissional'];
						$cooperativa = $reg6['cooperativa'];
						$tipo_profissional = $reg6['tipo_profissional'];
						$valor_prof =  $reg6['valor_prof'];
						$gender = $reg6['sexo_profissional'];

						//----------------------------------------------
						$obj_sh  = array(
								'date' => $reg6['data_entrada_realizada'],
								'period_name' => $reg6['tipo_profissional'],
								'tags' => array('ESC:' )
							);
				
						$data = $obj_sh;
						//----------------------------------------------
				}
				
				array_push($shifts, $data);	
				
				$id_prof_anterior = $id_prof;
			
		   }


			$profs = array('name'=>$name,
						  'nickname'=>'',
						  'email'=>$email,
						  'roles'=>array($papel),
						  'internal_id'=>"CONS:".$conselho.'-'.$identificador_interno,
						  'cellphone'=>$cellphone,
						  'tags'=>array("COOP:".$cooperativa,
								  "FUNC:".$tipo_profissional,
								  "TIPO_PAGAMENTO:".$tipo_pagamento,
								  "VALOR:R$".$valor_prof ),
						  "gender"=>$gender, 
						  "send_invite_when_submit"=>true,
						 'shifts' => $shifts);
						 

			$x = $profs;			 

			array_push($obj->professionals, $x);

			//print_r('---------------------');
			//print_r($obj->professionals);
			//print_r('---------------------');


			$schedules[] = $obj; //add each object to the array
		
			$cont++;
		}
	}
?>				

Open in new window

The object is always reasigned with the last value.

Thanks in advance
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

There are a lot of questions about this code.
I have reformatted the code and added my questions as comments.
Can't answer the question until those questions have been answered
<?php
if ($bd1->qBD($query6)) {
    while ($reg6 = $bd1->obtenerRegA()) {
        $id_prof = $reg6['id_profissional'];
        /* Did you mean to use '=' here instead of '==' or preferably '==='? */
        if($id_prof = $id_prof_anterior) {
            $obj_sh = [
                'date' => $reg6['data_entrada_realizada'],
                'period_name' => $reg6['tipo_profissional'],
                'tags' => ['ESC:']
            ];

            /* Why assigning to $data here? Why not use $obj_sh directly? */
            $data = $obj_sh;
            /* What is this used for */
            $ct++;	
        } else {
            $name =  $reg6['nome_completo'];
            $nickname='';	
            $email= $reg6['email_profissional'];
            $cellphone= $reg6['celular_profissional'];
            $cooperativa = $reg6['cooperativa'];
            $tipo_profissional = $reg6['tipo_profissional'];
            $valor_prof =  $reg6['valor_prof'];
            $gender = $reg6['sexo_profissional'];

            $obj_sh  = [
                'date' => $reg6['data_entrada_realizada'],
                'period_name' => $reg6['tipo_profissional'],
                'tags' => ['ESC:']
            ];

            /* Why assigning to $data here? Why not use $obj_sh directly? */
            $data = $obj_sh;
        }

        /* Where is  $shifts defined? */
        /* Why array_push and not $shifts[] = $data ? */
        array_push($shifts, $data);	
        $id_prof_anterior = $id_prof;
    }

    $profs = [
        'name'=>$name,
        'nickname'=>'',
        'email'=>$email,
        'roles'=>array($papel),
        'internal_id'=>"CONS:".$conselho.'-'.$identificador_interno,
        'cellphone'=>$cellphone,
        'tags'=>array("COOP:".$cooperativa,
        "FUNC:".$tipo_profissional,
        "TIPO_PAGAMENTO:".$tipo_pagamento,
        "VALOR:R$".$valor_prof ),
        "gender"=>$gender, 
        "send_invite_when_submit"=>true,
        'shifts' => $shifts
    ];

    /* Why assigning to $x here ? */
    $x = $profs;		
    /* Where does $obj come from */	 
    array_push($obj->professionals, $x);
    $schedules[] = $obj; //add each object to the array
    $cont++;
    /* Where is $shifts used ?*/
}

Open in new window

The one big glaring error though is the '=' in the if statement (line 3). That will always evaluate true - should be '==' ?
Avatar of Eduardo Fuerte

ASKER

Reply
<?php
if ($bd1->qBD($query6)) {
    while ($reg6 = $bd1->obtenerRegA()) {
        $id_prof = $reg6['id_profissional'];
        /* Did you mean to use '=' here instead of '==' or preferably '==='? */
        
		// Yes.
		if($id_prof === $id_prof_anterior) {
            $obj_sh = [
                'date' => $reg6['data_entrada_realizada'],
                'period_name' => $reg6['tipo_profissional'],
                'tags' => ['ESC:']
            ];

            /* Why assigning to $data here? Why not use $obj_sh directly? */
            $data = $obj_sh;
            /* What is this used for */
            
			//$ct++;	=> no use
        
		} else {
            $name =  $reg6['nome_completo'];
            $nickname='';	
            $email= $reg6['email_profissional'];
            $cellphone= $reg6['celular_profissional'];
            $cooperativa = $reg6['cooperativa'];
            $tipo_profissional = $reg6['tipo_profissional'];
            $valor_prof =  $reg6['valor_prof'];
            $gender = $reg6['sexo_profissional'];

            $obj_sh  = [
                'date' => $reg6['data_entrada_realizada'],
                'period_name' => $reg6['tipo_profissional'],
                'tags' => ['ESC:']
            ];

            /* Why assigning to $data here? Why not use $obj_sh directly? */
			// My missconception of your last reply        
			$data = $obj_sh;
        }

        /* Where is  $shifts defined? */
        /* Why array_push and not $shifts[] = $data ? */
		
		// My missconception of your last reply
        array_push($shifts, $data);	
        $id_prof_anterior = $id_prof;
    }

    $profs = [
        'name'=>$name,
        'nickname'=>'',
        'email'=>$email,
        'roles'=>array($papel),
        'internal_id'=>"CONS:".$conselho.'-'.$identificador_interno,
        'cellphone'=>$cellphone,
        'tags'=>array("COOP:".$cooperativa,
        "FUNC:".$tipo_profissional,
        "TIPO_PAGAMENTO:".$tipo_pagamento,
        "VALOR:R$".$valor_prof ),
        "gender"=>$gender, 
        "send_invite_when_submit"=>true,
        'shifts' => $shifts
    ];

    /* Why assigning to $x here ? */ 
	// My missconception of your last reply
    $x = $profs;		
    
	/* Where does $obj come from */	 
    array_push($obj->professionals, $x);
    $schedules[] = $obj; //add each object to the array
    
	
	//$cont++; // No use
    /* Where is $shifts used ?*/
}

Open in new window


Here is the complete code to know  the needed context, if you could check.
teste_EE_2.php
This
($reg6 = $bd1->obtenerRegA()) {

Open in new window

Must to be this way since it's a atribution. Atributes every table's line values.
(I incorrectly changed it on the code above)
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Out of office just now.
After changing the if to '==' the reasign problem didn't changed.
After your explanation I think if the object could created inside the loop the reasign problem could be corrected.

Since $obj  where $professionals is an array that is belonged for ($obj->professionals), is created outside the loop.
Doing that way it worked!!!

<?
		if ( $bd1->qBD($query6) )
		{

			while ($reg6 = $bd1->obtenerRegA())  //
			{

				$shifts_interno = array();
				$profs_interno = array();

				$id_prof = $reg6['id_profissional'];

			
				// Obtém estas informações apenas na 1.a passagem
				$name =  $reg6['nome_completo'];
				$nickname='';	
				$email= $reg6['email_profissional'];
				$cellphone= $reg6['celular_profissional'];
				$cooperativa = $reg6['cooperativa'];
				$tipo_profissional = $reg6['tipo_profissional'];
				$valor_prof =  $reg6['valor_prof'];
				$gender = $reg6['sexo_profissional'];


				$obj_sh  = array(
						'date' => $reg6['data_entrada_realizada'],
						'period_name' => $reg6['tipo_profissional'],
						'tags' => array('ESC:' )
					);

		
				$shifts_interno[] = $obj_sh;

				$id_prof_anterior = $id_prof;
				
			
				$profs_interno = array('name'=>$name,
						  'nickname'=>'',
						  'email'=>$email,
						  'roles'=>array($papel),
						  'internal_id'=>"CONS:".$conselho.'-'.$identificador_interno,
						  'cellphone'=>$cellphone,
						  'tags'=>array("COOP:".$cooperativa,
								  "FUNC:".$tipo_profissional,
								  "TIPO_PAGAMENTO:".$tipo_pagamento,
								  "VALOR:R$".$valor_prof ),
						  "gender"=>$gender, 
						  "send_invite_when_submit"=>true,
						 'shifts' => $shifts_interno);
				
				$profs_total[] = $profs_interno;
			
		   }  // Final do while 
		   

			array_push($obj->professionals, $profs_total);

			$schedules[] = $obj; //add each object to the array

		
			$cont++;
		}
	}
?>					

Open in new window


Just a matter to adjust the "if"
Everything OK now!

I think

The problem with this is that $obj always points to the same memory location.
So what I was saying was that you need to create a new object on each iteration - in other words - don't reuse an object that was created outside of the loop.

Are you still having a problem after changing the if to '=='?

Lend me to the solution.
@Julian

Thank you for that other excelent guidance!
You are welcome Eduardo.
One solution always leads us to another problem....

Could you give a look at that another question?