Display values from php mysql array separately

196

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];
4

Answer

Solution:

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;
}
629

Answer

Solution:

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.

People are also looking for solutions to the problem: php - PHPMailer via Cron job stopped working in July on Bluehost

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.