0

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;
learningbyexample
  • 1,527
  • 2
  • 21
  • 42
  • 2
    you don't really need [variable variables](http://php.net/manual/en/language.variables.variable.php) to create `x1, x2, x3` naming, just put them inside an array (container), then by `foreach` just use the key `$key as $value`, then just add `+1` – Kevin Nov 27 '15 at 07:23
  • mysql extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. – Afsar Nov 27 '15 at 07:25
  • Possible duplicate of http://stackoverflow.com/questions/7266754/php-join-two-variable-names – arunrc Nov 27 '15 at 07:28

2 Answers2

0

Whenever you think you need variable variable names with increasing numbers, what you really want is an array:

$sql  = mysql_query(...);
$data = [];
while ($row = mysql_fetch_array($sql)) {
    $data[] = $row['opcRes'];
}

..

foreach ($data as $item) {
    printf('<input type="radio" name="ops" value="%1$s"> %1$s<br/>', htmlspecialchars($item));
}
deceze
  • 510,633
  • 85
  • 743
  • 889
0

if you really need that's variable (like as you want), you can use braces {} to create variable from joined strings.

while($row = mysql_fetch_array( $sql )) {
    ${'opc'.$num} = $row['opcRes'];                                                                                  
    $num++;
}

so you can access your variables

for($i = 1; $i < $num; $i++){
    echo "<input type=\"radio\" name=\"".${'opc'.$i}."\" value=\"".${'opc'.$i}."\">".${'opc'.$i}."<br/>";
}
check
  • 559
  • 4
  • 12