Hi everyone, I hope you can save me from myself!
Usually when I have written a website, I have used predefined queries with a known number of fields in the results and it looks like this:
[php]<?php
$result = mysqli_query($link, ‘SELECT * FROM recipe’);
if (!$result)
{
$error = 'Error fetching recipes: ’ . mysqli_error($link);
include ‘includes/error.html.php’;
exit();
}
while ($row = mysqli_fetch_array($result))
{
$recipes[] = array('id' => $row['id'], 'title' => $row['title'], 'ingredients' => $row['ingredients'], 'method' => $row['method'], 'image' => $row['image'], 'alt' => $row['alt'], 'tags' => $row['tags']);
}
?>[/php]
[code]
<?php foreach ($recipes as $recipe): ?><?php echo $recipe['title']; ?>
- <?php $ingredients = str_replace("\n", "
- ", $recipe['ingredients']); echo $ingredients; ?>
<?php $steps = str_replace("\n", "
", $recipe['method']);
echo $steps; ?>
<?php echo $recipe['tags']; ?>
<?php endforeach; ?> [/code]
However, the site I am writing now allows the user to choose which fields they wish to generate a report on, so I can’t do the part where:
$recipes[] = array(‘id’ => $row[‘id’], ‘title’ => $row[‘title’], etc.
I tried just leaving it at:
[php]while ($row = mysqli_fetch_row($result))
{
$reports[]=array();
}[/php]
but that just sends arrays through to my html document.
So then I tried working with the numeric array keys, doing stuff like this:
[php]<?php
$columns = count($selections);
$rows = count($reports);
?>[/php]
[code]html>
<?php echo $report[($r*$columns) + $c]; $c++; ?> | <?php } $r++; ?>
This returned a table with a LOT of rows (I clicked out quickly in case infinity happened :o) with the word Array written in I guess as many cells as records were returned.
Does anybody have any suggestions?