0

I have a bit of a problem to succesfully create variable combining string and other variable. Problem is when i echo one variable i get a number (1..4) and i need to add that number to string "odpoved" so the combined variable looks like this "odpoved1". Im not sure if i have problem with creating that variable or calling it´s echo result from database. Here is my code

<?php  $spr=$result['odpoved']; $spravne=['odpoved'.$spr]; echo $spravne;?>

when i call variable $spravne result is just "Array" but i need it to show result from database under column "odpoved1""odpoved2""odpoved3""odpoved4" Can somebody help me? Thanks

3 Answers3

0

This will work if $result['odpoved'] is a string or number.

<?php  $spr=$result['odpoved']; $spravne='odpoved'.$spr; echo $spravne;?>

Also check out these informations regarding the different quotes in php.

What is the difference between single-quoted and double-quoted strings in PHP?

C4pt4inC4nn4bis
  • 586
  • 1
  • 6
  • 18
0

To print content of an array variable use var_dump or print_r which are suitable for debugging purposes only..

However your approach seems to be a bit untidy and soon unmanageable.

It looks like you have wrong database design as if kept data in single row with plenty columns with answers.
Instead change the table into rows with four basic columns: question_id, questionnaire_id, answer_id (odpoved), question_text. This way you will be able to print the questionnaire and based from submitted data select all answers (odpoved) for given questionnaire (dotaznik). And finally each question will have its own question_id and answer value. Both you will be easily processed in PHP in any manner you would like (shown to user, print as table, etc.).
And what's more, once you decided to extend number of questions/answers you will not have to extend number of columns in your table ie no new columns such as odpoved5, odpoved6, etc.

ino
  • 2,345
  • 1
  • 15
  • 27
0

I think you are just missing the name of the variable, so ...

$spravne=['odpoved'.$spr]; 

is actually creating an array with the value 'odpoved'.$spr - which may be 'odpoved1'. You probably wanted...

$spravne = $result['odpoved'.$spr]; 

which is fetching the element from the result set.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55