echo array as column mysqli?

2019-09-20 16:21发布

问题:

I am working on a recipe site for a school project, and I am trying to echo out two columns of arrays from my database in mysqli.

The arrays look like this:

And when I echo them out I would like them too look like this:

I have literally tried everywhere to find and answer.

My database name is "opskriftreg", and the connection to it works, the rest of the code comes out.

回答1:

You can use explode to make an array of it,

Example:

$data = "hi,hello,helooo";
$my_array = explode($data);
foreach($my_array AS $value)
{
    echo $value."<br />";
}


回答2:

You are trying to complicate things?! simply get the two columns using a normal MySQL query, and do the job through your language:

  • select column1, column2 from table;
  • get the result in a variable of your used language, $array_of_values for example.
  • for every field in the array explode the value by the delimiter ','.
  • draw your new table using a loop that goes through the new arrays and write the values in HTML.

For example in PHP:

$array_of_values = array('ing1, ing2, ing3', 'amount1, amount2, amount3');
$ings = explode(',',$array_of_values[0]);
$amounts = explode(',',$array_of_values[1]);
echo "<table>";
for ($i=0; $i < count($ings); $i++){
    echo "<tr>";
    echo "<td>".$ings[$i]."</td>";
    echo "<td>".$amounts[$i]."</td>";
    echo "<tr/>";
}
echo "</table>";