1

What im trying to do is to save vales of "option" from all records in array to be able to echo them out later. I don't know how to display them seperatly. Feel stuck. Any sugestions?

My code:

$result = $mysqli->query("SELECT option FROM `myoptions` WHERE id IN (1, 7, 18, 24);");
$row = array();
while($row = $result->fetch_array())  { 
    echo $row[] = $row["option"];
}

echo $option1 = $row[0];
echo $option2 = $row[1]; 
echo $option3 = $row[2];
echo $option4 = $row[3];
5
  • 1
    what do you mean by 'display them separately'? Commented Aug 15, 2016 at 7:14
  • what is the error you facing?? Commented Aug 15, 2016 at 7:14
  • may be it will help 3v4l.org/ktGdG Commented Aug 15, 2016 at 7:18
  • i want to use my 'option' variables in a different part of the page and my problem is that variables are empty. Commented Aug 15, 2016 at 7:19
  • 1
    Can you show us the code, where you are trying to use it, and getting the empty variables? Commented Aug 15, 2016 at 7:21

2 Answers 2

1

YOu should store the value in an array

$cnt = 0;
while($row = $result->fetch_array())  { 

  $for_future_use[$cnt] = $row["option"]; 
  $cnt++; 
}

and then use later

foreach($for_future_use as $key=>$value) {
   echo $value;
}
Sign up to request clarification or add additional context in comments.

Comments

1

try this,

$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
$i = 1;
$option = "option";
foreach($my_array as $val)
{
    ${$option.$i} = $val;
    $i++;
}

echo "\$option1  =>  ".$option1."  \$option2 =>  ".$option2."  \$option3  =>  ".$option3;

DEMO : https://3v4l.org/OhQSm

i hope it will be helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.