Página dinâmica
Bom dia a todos.
Estou criando páginas dinâmicas para meus sistemas de forma que possa alimentá-las de acordo com a url.
Meu problema é na edição de dados onde não estou sabendo ao certo como resgatar os valores pelo id do registro e compará-los para manter os valores nos campos. Está tudo muito simples, mas funciona legal
A página de edição
<?php
// Get params from url
$id = filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
$filter = filter_input(INPUT_GET,'filter',FILTER_VALIDATE_INT);
$type = filter_input(INPUT_GET,'type',FILTER_DEFAULT);
$url = filter_input(INPUT_GET,'url',FILTER_DEFAULT);
$pageActual = filter_input(INPUT_GET, 'atual', FILTER_VALIDATE_INT);
// Objects
$viewCad = new ViewCadControl($type,$id);
$funcoes = new Valid;
?>
<section class="content">
<header class="header-pages">
<hgroup class="left">
<h1>Edição de <?= ucwords($type); ?></h1>
<h2>Você está em modo de edição, as alterações feitas aqui, são aplicadas imediatamente sobre o módulo ativo.</h2>
</hgroup>
</header>
<article>
<?phpforeach($viewCad->geteEditData() as $r):
echo 'Hi, i\'m a column of table. My name is: ' . $r . '<br>';
endforeach;
echo '<pre>';
var_dump($viewCad->geteEditData());
echo '</pre>';
?>
<form id="frms-edit-<?= $funcoes->retira_acentos(utf8_decode($type)); ?>" class="valid_ajax_form frms">
<input type="hidden" name="action" value="insert-new-<?= $funcoes->retira_acentos(utf8_decode($type)); ?>">
<?phpforeach($viewCad->getDataFromTable() as $fields):
if($fields->Field != 'id' && $fields->Field != 'data'):
?>
<!-- Select para clientes -->
<?php if($fields->Field == 'id_cliente'): ?>
<label>
<strong>Cliente</strong>
<select name="id_cliente" id="id_cliente">
<option value="0">Selecione</option>
<?php
$read->ExeRead('sis_clientes','id,nome,sobrenome','WHERE status = :status', "status=1");if($read->getResult()):
foreach($read->getResult() as $result):
?>
<option value="<?= $result->id; ?>" <?php /*if($viewCad->geteEditData()['id_cliente'] == $result->id): echo 'selected'; endif;*/?>><?= $result->nome . ' ' . $result->sobrenome; ?></option>
<?php
endforeach;else:
?>
<option value="0">Nenhum cliente disponível</option>
<?php endif; ?>
</select>
</label>
<!-- Select para clientes -->
<?php elseif($fields->Field == 'id_servico'): ?>
<label>
<strong>Serviço relacionado</strong>
<select name="id_servico" id="id_servico">
<option value="0">Selecione</option>
<?php
$read->ExeRead('sis_servicos','id,nome','WHERE status = :status', "status=1");if($read->getResult()):
foreach($read->getResult() as $result):
?>
<option value="<?= $result->id; ?>"><?= $result->nome; ?></option>
<?php
endforeach;else:
?>
<option value="0">Nenhum serviço disponível</option>
<?php endif; ?>
</select>
</label>
<!-- Select para status do registro -->
<?php elseif($fields->Field == 'status'): ?>
<label>
<strong>Status</strong>
<select name="status" id="status">
<option value="0">Selecione</option>
<option value="1">Ativo</option>
<option value="2">Inativo</option>
</select>
</label>
<!-- Select para status de aprovação do registro -->
<?php elseif($fields->Field == 'aprovado'): ?>
<label>
<strong>Aprovado</strong>
<select name="status" id="status">
<option value="0">Selecione</option>
<option value="1">Sim</option>
<option value="2">Não</option>
</select>
</label>
<!-- Select para definir se é parcelado ou não -->
<?php elseif($fields->Field == 'parcelado'): ?>
<label>
<strong>Parcelado</strong>
<select name="parcelado" id="parcelado">
<option value="0">Selecione</option>
<option value="1">Sim</option>
<option value="2">Não</option>
</select>
</label>
<!-- Máscara campos real -->
<?php elseif($fields->Field == 'valor' || $fields->Field == 'preco' || $fields->Field == 'v_parcelas'): ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" class="preco" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<!-- Input type data -->
<?php elseif($fields->Field == 'data' || $fields->Field == 'previsao' || $fields->Field == 'data_inicio' || $fields->Field == 'data_fim'): ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" class="datas" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<!-- Input type cep -->
<?php elseif($fields->Field == 'cep'): ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" class="cep" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<!-- Input type cnpj -->
<?php elseif($fields->Field == 'cnpj'): ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" class="cnpj" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<!-- Input type cpf -->
<?php elseif($fields->Field == 'cpf'): ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" class="cpf" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<?php else: ?>
<label>
<strong><?= ucfirst(preg_replace('/_/i',' ', $fields->Field)); ?></strong>
<input type="text" name="<?= $fields->Field; ?>" id="<?= $fields->Field; ?>">
</label>
<?php
endif;
endif;
endforeach;
?>
<button class="submit btn">Salvar alterações em <b><?= ucwords(substr($type, 0, strrpos($type, 's'))); ?></b></button>
<a href="relatorios?type=<?= $type; ?>">Voltar para listagem de <b><?= ucwords($type); ?></b></a>
</form>
</article>
</section>
A classe
<?php
/**
*
*
*/
class ViewCadControl {
/**
*
*
*/
private $table;
/**
*
*
*/
private $results;
/**
*
* @param string $table
*/
public function __construct($table, $id = null) {
$this->table = 'sis_' . $table;
$this->id = $id;
}
/**
*
*
*/
public function getDataFromTable() {
$read = new Read;
$read->FullRead("SHOW COLUMNS FROM {$this->table}");
if($read->getResult()):
$this->results = $read->getResult();
endif;
return $this->results;
}
/**
*
*/
public function geteEditData() {
$read = new Read;
$read->ExeRead($this->table, '*', 'WHERE id = :id LIMIT 1', "id={$this->id}");if($read->getResult()):
foreach($read->getResult() as $results):
$this->results = $results;
return $this->results;
endforeach;
endif;
}
}
Minha dúvida é como persistir os dados. Já tentei englobar com o foreach, as me parece bem feio e além do mais não funciona, porque entra em outro loop e duplica os campos do formulário. Preciso de algumas dicas que talvez a minha ansiedade pelo fato de ter que ir ao dentista não me deixa ver claramente.
O debug[incluso na view]
<?php
foreach($viewCad->geteEditData() as $r):
echo 'Hi, i\'m a column of table. My name is: ' . $r . '<br>';
endforeach;
echo '<pre>';
var_dump($viewCad->geteEditData());
echo '</pre>';
?>
Retorna
[code]
Hi, i'm a column of table. My name is: 1
Hi, i'm a column of table. My name is: Clientes
Hi, i'm a column of table. My name is: clientes
Hi, i'm a column of table. My name is: 0
Hi, i'm a column of table. My name is: 1
object(stdClass)#19 (5) {
["id"]=>
string(1) "1"
["nome"]=>
string(8) "Clientes"
["url"]=>
string(8) "clientes"
["type"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
Discussão (2)
Carregando comentários...