-1

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: Arrays for recipedatabase

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

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.

Münter
  • 179
  • 1
  • 2
  • 8
  • 1
    this is a poorly structured db, normalisation would help a lot –  Apr 03 '14 at 20:45
  • Don't have all the columns on there, just took a sreenshot of the array part – Münter Apr 03 '14 at 20:48
  • ideally you never have a comma separated list like that in a db field, you should have another table with recipe_id, ingredient, amount. –  Apr 03 '14 at 20:53
  • Ah okay. Pretty new to this, so thanks for info :-) Still learning – Münter Apr 03 '14 at 20:54
  • 2
    you could even have an ingredient(id, ingredient) table then another table with recipe_id, ingredient_id, amount. –  Apr 03 '14 at 20:56
  • 3
    You should look into [normalizing your data](https://en.wikipedia.org/wiki/Database_normalization). Putting comma separated lists into a record usually means that you have yet to learn how [*relational* databases](https://en.wikipedia.org/wiki/Relational_database) really work. – Sverri M. Olsen Apr 03 '14 at 21:08

2 Answers2

0

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 />";
}
Companjo
  • 1,789
  • 18
  • 24
-1

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>";
Yazid Erman
  • 1,166
  • 1
  • 13
  • 24
  • gets an error `Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting ']' in // on line 4` Array values line – Münter Apr 03 '14 at 21:13
  • You shouldn't copy paste the code as is by the way! you should have just got the Idea because I didn't test the syntax! – Yazid Erman Apr 04 '14 at 14:07
  • Anyway, Just replace the first line with the following: $array_of_values = array('ing1, ing2, ing3', 'amount1, amount2, amount3'); – Yazid Erman Apr 04 '14 at 14:14