Hello everyone,

I'm creating a simpel survey tool'.
When people insert the question, they can also what kind of question type (checkbox or radiobutton)
Adding question and types in the mysql is oke..

I have a problem with showing the type & question

Problem: the questions only show if I remove the code if($row['question_type'] ..or remove type='.$type.

//db connection

$sql = mysqli_query($con, "SELECT `question`, `question_type` FROM  `questions`");
 while ($row = mysqli_fetch_assoc($sql))


 if ($row['question_type'] = 1)
{
 $type = "radio";   
}
else {
    $type ="checkbox";
}

// Associative array
{

      echo '<form method="post" action="">';
     echo '<tr>';
     echo '<td>'. '<input type='.$type.'     value= "'.$row['question'] . '" </td>';
      echo '</form>';
mysqli_close($con);
?> 

Thanks
Rem

The if statement has an error. Instead of assignment operator = you should use comparison operator ==, like:

if ($row['question_type'] == 1)

In the form html code you are missing some quotes (altough this should not be critical):

echo '<td>'. '<input type="'.$type.'" value= "'.$row['question'] . '" </td>';

@broj1 and others,

if statement changed -> === 1 and added the qoutes.

But the result is that only the checkbox is displayed. The question only shows as describes in the question (post 1). I'm stuck.
Any ideas?

thanks

By looking at your query you want to display all the questions from the questions table. Am I correct? In this case you have to rearrange code a little bit. First start the form, then start the table, then loop through rows using a while loop and in each iterration display a row and at the end close the table and the form. Something like (see comments in the code):

// first start the form
echo '<form method="post" action="#">';

// then start the table
echo "<table>";

// db connection
...

// query the database
$sql = mysqli_query($con, "SELECT `question`, `question_type` FROM  `questions`");

// loop through result - each db row is one table row
// note the curly bracket for a loop block
 while ($row = mysqli_fetch_assoc($sql)) {

    if ($row['question_type'] == 1) {
        $type = "radio";   
    } else {
        $type ="checkbox";
    }

    // start table row and cell
    echo '<tr><td>';

    // input element
    echo '<input type="'. $type .'" value= "' . $row['question'] . '">';

    // end table cell and row
    echo '</td></tr>';

} // <-- note the end of the while loop

// end the table
echo '</table>';

// end the form
echo '</form>';

// close connection
mysqli_close($con);
?> 

In your code you were missing the curly brackets alltogether to enclose the block of code to loop through.

BTW: if the type is either a checkbox or radio, shouldn't the question be displayed in a label element not as the value?

Also if you use radio, it is usually more than one value. Is your logic correct?

@broj1

I will look at this...you're right about the radio buttons and more than one value. Therefore I will create a fix. But first I will look at the code.

Let you know as soon as possible!
Thanks

@broj1 and others

Finally I had time to look at the solution.
But I still see only displayed the checkbox and radiobutton, without the question.
For the radiobutton I will make an fix, but for now I first want to get the script working. Any ideas why the question won't display display?

There are no error's in the script?

thanks

@all

Fixed:

// input element
    echo '<input type="'. $type .'" value= "' . $row['question'] . '">';

should be
// input element
    echo '<input type="'. $type .'">';
    echo $row['question'];

Why I don't understand, but now the inputtype and question are shown
Is this because of the while loop?

Thanks
And I will close this issue tomorrow.

Reminem
`

Noone noticed that little glitch :-). The checkbox value attribute is what gets carried over in request. The text that you want displayed next to the checkbox is just a HTML (not the value attribute).

@broj1

Thanks for the explanation and your support!
I will close the question

Reminem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.