Is there any way to separate this file and call by ids

Asked

Viewed 55 times

-3

Galera good morning I am working on a survey program in php, I need to separate the id of a question, example take the id of the first question and relate to the second. Example 1st question How you evaluate the service if it is great,good,regular Ok ends, if it is Bad and bad go to second question Why you evaluate this option:Waiting time,Poor service, Others. But the 2 questions have to have the same ID.

<?php

include_once 'conecta_bd.php';

if(isset($_GET['id'])){
	


	$enquete_id = $_GET['id'];
	$query = $pdo->prepare("SELECT valor_opcao FROM opcao2 WHERE opcao_id = 1 AND enquete_id ='$enquete_id' ");
	$query->execute();
	$valores_opcoes = $query->fetchAll();


	$query = $pdo->prepare("SELECT nome_enquete, descricao FROM enquete WHERE enquete_id ='$enquete_id' ");
	$query->execute();
	$query = $query->fetch();
	$titulo_enquete    = $query['nome_enquete'];
	$descricao_enquete = $query['descricao']; /* VER DEPOIS COM CALMA COMO SEPARAR ISSO EM OUTRO ARQUIVO VER TEST1.PHP E TEST2.PHP*/

	echo '<h1>'.$titulo_enquete.'</h1>

	<form action="checa_data.php" method="POST" accept-charset="utf-8">';
	
	for ($i=0; $i < count($valores_opcoes); $i++) { 
		
		$valor_opcao = $valores_opcoes[$i]['valor_opcao'];
		echo'<input type="submit" name="valor_opcao" value="'.$valor_opcao.'">'.$valor_opcao.'
		<br>';
	}

	echo'<input type="hidden" name="enquete_id" value="'.$enquete_id.'">
		 <input type="submit" value="Votar!">
		 </form>
		
		<h3>breve descrição:<h4>'.$descricao_enquete.'</h4></h3>
		<h3><a href="enquete_info.php?id='.$enquete_id.'">ver informações da enquete</a></h3><h3><a href="main.php">voltar à página inicial</a></h3>';

}



else
	header("location: main.php");

?>

<!--<script type="text/javascript">
   setTimeout(function $btnruim() {
            window.location.href = "index.html";
        }, 5000);

</script>-->

<?php 
	/* Função para criar nova enquete, talves tenha necessidade de melhorar o
	código :| */

include_once 'conecta_bd.php';


if( !empty($_POST['titulo_enquete']) && !empty($_POST['descricao_enquete']) && !empty($_POST['data']) && !empty($_POST['opcao'])){
	
	$titulo_enquete 	= $_POST['titulo_enquete'];
	$descricao_enquete	= $_POST['descricao_enquete'];
	$data_enquete		= $_POST['data']; 
	$qtdade_opcoes 		= $_POST['numero'];
	$opcoes 	   		= $_POST['opcao'];

	$query = $pdo->prepare("INSERT INTO  enquete (nome_enquete, data_termino, descricao) VALUES ('$titulo_enquete','$data_enquete','$descricao_enquete') ");
	$query->execute();

	$query = $pdo->prepare("SELECT max(enquete_id) FROM enquete");
	$query->execute();
	$id = $query->fetch();

	for ($i=0; $i<$qtdade_opcoes ; $i++) { 
		
		$query = $pdo->prepare("INSERT INTO  opcao (enquete_id,valor_opcao,numero_votos) VALUES ('$id[0]','$opcoes[$i]','0') ");
		$query->execute();
	}

	header("location: sucesso_enquete_criacao.php?titulo=$titulo_enquete&id=$id[0]");
}

	/* Valida enquete de pergutas 1 Recepçao e condição de botão RUIM e PÉSSIMO 
	para segunda pergunta */

if(isset($_GET['valor_opcao'])){
	
	$valor_opcao 	= utf8_decode($_GET['valor_opcao']);
	$enquete_id  	= $_GET['enquete_id'];
	

$query = $pdo->prepare("UPDATE opcao SET numero_votos = numero_votos + 1 WHERE enquete_id ='$enquete_id' AND valor_opcao LIKE '$valor_opcao' ");
	$query->execute();

if($enquete_id == 1 ){

	if($_GET['valor_opcao'] == 'ÓTIMO' or $_GET['valor_opcao'] == 'BOM' or $_GET['valor_opcao'] == 'REGULAR'){

	header("location: sucesso.php?id=$enquete_id&valor=$valor_opcao");
                  
    }else{
      header("location: enquete1_2.php");
    }
}

if($opcao2 == 2 ){

	if($_GET['valor_opcao'] == 'TEMPO DE ESPERA' or $_GET['valor_opcao'] == 'EDUCAÇÃO DO PROFISSIONAL' or $_GET['valor_opcao'] == 'ESTRUTURA RUIM' or $_GET['valor_opcao'] == 'FALTA DE SERViÇO' or $_GET['valor_opcao'] == 'OUTROS'){

	header("location: sucesso.php?id=$enquete_id&valor=$valor_opcao");
                  
    }
}
                
}

else
	header("location: main.php");
?>

  • What I would do is an Ajax request that takes the answer from the first form, and receives the answer from the server that returns to ajax, which composes the text of the second question

  • Blz I’ll check that way.

1 answer

-2


Dude I might as well be wrong, I’m new to the stack and I’m rusty with php. But thinking logically, first you must exchange this if for a switch, where you take the answer and from it comes out a result. I didn’t understand why the questions need Ids, you could create a simple structure of the question text where you just change the variables. In the case I see, you just need to keep the result of the poll. For the second option, you would only need to show the second text given the conditions met. If I did not understand right send a feedback from my reply pf.

  • I understood maybe it would also be this I will verify is that when I speak id I refer to a single question, but with option of example requests only goes to second question if the user click on bad or bad, but I need to separate this automatically I know which manual is possible but not the ideal.

  • then, this separation can be very simple from the foma q said, make a switch, where if the answer is positive it keeps the level of satisfaction, if it is not, vc returns a modal or a new page with the new questionnaire.

  • I don’t think it’s a good idea to save the structure of the question in DB, especially if it’s a question that would serve other users, as I said before, create the structure and just save the answers, it will be a cleaner and more efficient code.

  • Got it thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.