I want to make a new variable name in my while loop to store the value of the $row[''] array.
$sql = mysql_query(" SELECT * FROM table where idTable=5 ") or die(mysql_error());
$num = 1;
while($row = mysql_fetch_array( $sql )) {
echo 'opc'.$num = $row['opcRes'];//I want to create the new varible
//$opc1 then $opc2, $opc3 etc...
//that is why I increment $num
$num++;
}
then after creating the new variable I want to print the values onto the screen from a php html echo
echo '
<div class="entry">
<h3 class="pTitle">'.$preg.'</h3>
<form action="" method="post">
'.for($i = 1; $i < $num; $i++){
.'
<input type="radio" name="'.$opc.$num.'" value="'. echo $opc.$num; .'">'. echo $opc.$num; .'<br/>
'.}.'
<input type="submit" value="Submit">
<input type="hidden" name="idPreg" value="idPreg">
<input type="hidden" name="sent" value="1">
</form>
</div>
';
COMPLETE CODE:
<?php
function getPreguntas($dbc){
$sql = mysql_query(" SELECT * FROM pregunta ") or die(mysql_error());
if($sql){
while($row = mysql_fetch_array( $sql )) {
$idPreg = $row['idPregunta'];
$preg = $row['pregunta'];
if ($sql = mysql_query("SELECT opcRes FROM opciones where pregunta_idPregunta='$idPreg' ")) {
if (mysql_num_rows($sql) > 0) {
$opc = mysql_fetch_assoc($sql);
}
}
echo '
<div class="entry">
<h3 class="pTitle">'. $preg .'</h3>
<form action="" method="post">
'.
if (!empty($opc)) {
foreach ($opc as $key=>$value) {
.'
<input type="radio" name="'.$value.'" value="'.$value.'">'.$value.'<br/>
'.
}
}.'
<input type="submit" value="Submit">
<input type="hidden" name="idPreg" value="idPreg">
<input type="hidden" name="sent" value="1">
</form>
</div>
';
}
}
}
?>
tables
-- -----------------------------------------------------
-- Table `SISE`.`pregunta`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SISE`.`pregunta` (
`idPregunta` INT NOT NULL AUTO_INCREMENT,
`encuesta_idEncuesta` INT NOT NULL,
`ayudaDes` VARCHAR(300) NULL,
`pregunta` VARCHAR(500) NOT NULL,
`opcionesRes` VARCHAR(100) NOT NULL,
PRIMARY KEY (`idPregunta`, `encuesta_idEncuesta`),
INDEX `fk_preguntas_encuesta1_idx` (`encuesta_idEncuesta` ASC),
CONSTRAINT `fk_preguntas_encuesta1`
FOREIGN KEY (`encuesta_idEncuesta`)
REFERENCES `SISE`.`encuesta` (`idEncuesta`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SISE`.`opcion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SISE`.`opcion` (
`idopcion` INT NOT NULL AUTO_INCREMENT,
`pregunta_idPregunta` INT NOT NULL,
`pregunta_encuesta_idEncuesta` INT NOT NULL,
`opcRes` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idopcion`, `pregunta_idPregunta`, `pregunta_encuesta_idEncuesta`),
INDEX `fk_opcion_pregunta1_idx` (`pregunta_idPregunta` ASC, `pregunta_encuesta_idEncuesta` ASC),
CONSTRAINT `fk_opcion_pregunta1`
FOREIGN KEY (`pregunta_idPregunta` , `pregunta_encuesta_idEncuesta`)
REFERENCES `SISE`.`pregunta` (`idPregunta` , `encuesta_idEncuesta`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;