-1

This is a continuation of an earlier question that was started here:

Pull Apart Date Range into Separate Cells in CSV, Excel or Spreadsheet

After consulting with a couple of developers, I decided to turn this into a PHP/MySql solution, instead of a Excel VBA solution. Here is the problem. I am a complete newbie with PHP. I can code html and css, (which is why I strayed from the Excel/VBA solution), but I start to go a bit cross-eyed after reading too much PHP. I am hoping you can help.

Here is the issue at hand:

I have a table with car parts. Each car part fits a year range. For each part, I need a listing (row) for each year in the range. For example, a muffler for a Ford Mustang might fit years 1972 to 1977. In the table, currently this is all one row of data. I need one row of data for each year in the range that the muffler fits. For this example, the table would go from one row to six, each row containing the year that the part fits the model.

I've been told to "write a loop" do carry this out for me. I have tried. I have pulled hair. Done more research on PHP.net and tried again. If you can help me go in the right direction with this problem it would be greatly appreciated!

Here is the code that created the table:

`enter code here`
<?php
$result = mysqli_query($con,"SELECT * FROM xxxxxxxxxxxx");
echo "<table border='1'>
<tr>
<th>ItemNo</th>
<th>Description</th>
<th>Model</th>
<th>YearBeg</th>
<th>YearEnd</th>
<th>YearRangeCount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ItemNumber'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['YearBeg'] . "</td>";
echo "<td>" . $row['YearEnd'] . "</td>";
echo "<td>" . $row['YearRange'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Community
  • 1
  • 1
bones_bare
  • 31
  • 1
  • 9
  • 1
    What is the problem you are getting? Are you sure your column names are correct in your database? To help your issue you can replace your `$result` with `$result = mysqli_query($con, $query) or die ("Error in query: $query. ".mysql_error());` to show the error if any – Bijan Sep 08 '14 at 22:00
  • 1
    @Bijan `("Error in query: $query. ".mysql_error());` should be `mysqli_error($con)` - Those two APIs do not mix. – Funk Forty Niner Sep 08 '14 at 22:12

1 Answers1

1

Just make another loop inside your while, that loops through each year in the range, like this

while($row = mysqli_fetch_array($result)) {
    for ($year = $row['YearBeg']; $year <= $row['YearEnd']; ++$year) {
        echo "<tr>";
        echo "<td>" . $row['ItemNumber'] . "</td>";
        echo "<td>" . $row['Description'] . "</td>";
        echo "<td>" . $row['Model'] . "</td>";
        echo "<td>" . $year . "</td>";
        echo "</tr>";
    }
}
Drecker
  • 1,215
  • 10
  • 24
  • This was the solution. I added the code and it worked exactly as described. Thank you. For anyone interested, I have added a new table with the updated code as a sample result with the correct answer: http://musclecarpartsdirect.com/twwoodward/Inventory_Sample_Updated.php – bones_bare Sep 08 '14 at 22:34