2

I am struggling with a concept that has been addressed in part here, but I am trying to extend it and am stumped. My query pulls data from three tables (I am using a stored procedure):

Select a.adID, a.typeID, l.description, l.available, l.cost, a.heading, 
a.adDate, a.adActive, a.plannerID, t.typename
From advertisements as a, advert_types as t, locations as l
Where a.plannerID = 1 And a.typeID = t.typeID And l.locationID = 
a.locationID
Order by a.adDate, a.typeID;

The output appears to be ok using a query editor within MySQL workbench, but I am trying to write the results out using the Month (a.adDate) as the first heading, then the field t.typename as the second heading, so it should look something like this:

January
___________________________________________________
Bistro    Heading    Date
------------------------------
E-newsletter  $100  ...
Digital Ad    $50   ...
Magazine Ad   $150  ...


February
___________________________________________________
Kitchen   Heading    Date
-------------------------------
Social Media   $200  ...
E-flyer Ad     $180  ...


March
___________________________________________________
Restaurant  Heading   Date
-------------------------------
Front cover   $500    ...
Back cover    $350    ...

Under each month there is a subheading (Bistro, Kitchen, Restaurant etc.) then there should be the items listed below each corresponding subheading, and also under the correct month.

My PHP code is:

$query = "Call listads_per_planner('$planner');";
$result = mysqli_query($conn, $query) or die("Query failed: " . 
                                             mysqli_error($conn));
$num_rows = mysqli_num_rows($result);

$priorDate = null;
$priorType = null;
$counter = 0;
while ($row = $result->fetch_assoc()) {
    $theDate = $row['adDate'];
    $thestrDate = strtotime($theDate);
    $theadYear = date('Y', $thestrDate);
    $theadMonth = date('F', $thestrDate);
    $adMonthInt = date('m', $thestrDate);
    $typeName = $row['typename'];
    $heading = $row['heading'];
    $thetypeID = $row['typeID'];

    if ($priorDate != $adMonthInt) {

        if ($priorDate != null) {
            echo "";
        }  
        if ($counter > 0){
            echo "<br /><br />\n";
        }

        echo "<h3>". $theadMonth ."</h3>\n";
        echo "<hr style=\"padding: 0; margin: 0\"/>\n";
        ($priorDate = $adMonthInt);
    }

    if ($thetypeID != $priorType){
        echo "<table border=\"0\" style=\"width: 100%; cellpadding: 2px; 
     cellspacing: 1px\" >\n";
        echo "<tr>\n";
        echo "<td style=\"background-color: #cccccc; heigh: 26px; width: 30%\">\n";
        echo "<span style=\"margin-left: 3px\"><strong>". $typeName ."</strong> 
     </span></td>\n";
        echo "<td style=\"background-color: #cccccc;\"><input type=\"text\" 
     name=\"heading\" size=\"22\" maxlength=\"100\" value=\"". $heading ."\"> 
     </td>\n";
        echo "<td style=\"background-color: #cccccc;\"><span style=\"margin-left: 
     2px\">Date: <select name=\"day\">\n";
        echo "<option value=\"1\">1</option>\n";
        echo "<option value=\"2\">2</option>\n";
        echo "<option value=\"3\">3</option>\n";
        echo "<option value=\"4\">4</option>\n";
        echo "<option value=\"5\">5</option>\n";
        //...                               
        echo "<option value=\"31\">31</option>\n";
        echo "</select>&nbsp;<select name=\"month\">\n";
        echo "<option value=\"1\">January</option>\n";
        echo "<option value=\"2\">February</option>\n";
        echo "<option value=\"3\">March</option>\n";
        echo "<option value=\"4\">April</option>\n";
        //...                       
        echo "<option value=\"12\">December</option>\n";
        echo "</select></span></td>\n";
        echo "<td style=\"background-color: #cccccc;\"><div align=\"right\"> 
     </td>\n";
        echo "<td style=\"background-color: #cccccc;\"><table border=\"0\" 
     style=\"width: 100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><div 
     align=\"right\"><button type=\"submit\" class=\"btn btn- 
     success\">Save</button></div></td><td></td><td><div align=\"right\"><button 
     type=\"button\" class=\"btn btn-danger\">Remove</button></td></tr></table> 
     </td>\n";
        echo "</tr>\n";
        echo "<tr>\n";
        echo "<td><strong>Location</strong> <a href=\"#\">> Add Location +</a></td> 
     <td><strong>Available</strong></td><td><strong>Cost</strong></td><td> 
     <strong>Default Cost</strong></td><td><strong>Delete?</strong></td>\n";
        echo "</tr>\n";
        $priorType = $thetypeID;
    }

    echo "<tr>\n";
    echo "<td style=\"height: 26px\">". $row['description'] ."</td>\n";
    echo "<td><input type=\"text\" name=\"avail\" size=\"2\" value=\"". 
        $row['available'] ."\"></td>\n";
    echo "<td><input type=\"text\" name=\"cost\" size=\"7\" maxlength=\"8\" 
     value=\"". $row['cost'] ."\"></td>\n";
    echo "<td>". $row['cost'] ."</td>\n";
    echo "<td><div align=\"left\" style=\"padding-left: 4px\"><a href=\"#\" 
     title=\"Delete this location?\"><img src=\"img/delete.png\" border=\"0\"> 
     </a></div></td>\n";
    echo "</tr>\n";

    $counter++;
}

echo "</table>\n";

I am seeing unexpected results in the browser output, with the subheadings not populating properly, and the list results being displayed under the wrong month. If anyone could shed some insights, I would be greatly appreciative. I can make it work with just the single heading, but can't seem to get it right with the subheading. Many thanks.

Update: Here is some sample data from the DB:

Query results

Here is a screenshot of the output I am getting: January should have two Locations listed, and November should have only one.

HTML output

Shadow
  • 33,525
  • 10
  • 51
  • 64
PaulT
  • 43
  • 6

1 Answers1

1

Whenever you start a new month, you need to end the table for the previous type ID, and reset $priorType to null. Otherwise, it won't start a new table if you have the same type ID in consecutive months.

When starting a new type ID, you need to end the table for the previous type ID.

    $query = "Call listads_per_planner('$planner');";
    $result = mysqli_query($conn, $query) or die("Query failed: " . 
                                                 mysqli_error($conn));
    $num_rows = mysqli_num_rows($result);

    $priorDate = null;
    $priorType = null;
    $counter = 0;
    while ($row = $result->fetch_assoc()) {
        $theDate = $row['adDate'];
        $thestrDate = strtotime($theDate);
        $theadYear = date('Y', $thestrDate);
        $theadMonth = date('F', $thestrDate);
        $adMonthInt = date('m', $thestrDate);
        $typeName = $row['typename'];
        $heading = $row['heading'];
        $thetypeID = $row['typeID'];

        if ($priorDate != $adMonthInt) {
            if ($priorDate != null) {
                if ($priorType != null) {
                    echo "</table>\n";
                    $prioType = null;
                }
                echo "";
            }  
            if ($counter > 0){
                echo "<br /><br />\n";
            }

            echo "<h3>". $theadMonth ."</h3>\n";
            echo "<hr style=\"padding: 0; margin: 0\"/>\n";
            ($priorDate = $adMonthInt);
        }

        if ($thetypeID != $priorType){
            if ($priorType != null) {
                echo "</table>\n";
            }
            echo "<table border=\"0\" style=\"width: 100%; cellpadding: 2px; 
         cellspacing: 1px\" >\n";
            echo "<tr>\n";
            echo "<td style=\"background-color: #cccccc; heigh: 26px; width: 30%\">\n";
            echo "<span style=\"margin-left: 3px\"><strong>". $typeName ."</strong> 
         </span></td>\n";
            echo "<td style=\"background-color: #cccccc;\"><input type=\"text\" 
         name=\"heading\" size=\"22\" maxlength=\"100\" value=\"". $heading ."\"> 
         </td>\n";
            echo "<td style=\"background-color: #cccccc;\"><span style=\"margin-left: 
         2px\">Date: <select name=\"day\">\n";
            echo "<option value=\"1\">1</option>\n";
            echo "<option value=\"2\">2</option>\n";
            echo "<option value=\"3\">3</option>\n";
            echo "<option value=\"4\">4</option>\n";
            echo "<option value=\"5\">5</option>\n";
            //...                               
            echo "<option value=\"31\">31</option>\n";
            echo "</select>&nbsp;<select name=\"month\">\n";
            echo "<option value=\"1\">January</option>\n";
            echo "<option value=\"2\">February</option>\n";
            echo "<option value=\"3\">March</option>\n";
            echo "<option value=\"4\">April</option>\n";
            //...                       
            echo "<option value=\"12\">December</option>\n";
            echo "</select></span></td>\n";
            echo "<td style=\"background-color: #cccccc;\"><div align=\"right\"> 
         </td>\n";
            echo "<td style=\"background-color: #cccccc;\"><table border=\"0\" 
         style=\"width: 100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><div 
         align=\"right\"><button type=\"submit\" class=\"btn btn- 
         success\">Save</button></div></td><td></td><td><div align=\"right\"><button 
         type=\"button\" class=\"btn btn-danger\">Remove</button></td></tr></table> 
         </td>\n";
            echo "</tr>\n";
            echo "<tr>\n";
            echo "<td><strong>Location</strong> <a href=\"#\">> Add Location +</a></td> 
         <td><strong>Available</strong></td><td><strong>Cost</strong></td><td> 
         <strong>Default Cost</strong></td><td><strong>Delete?</strong></td>\n";
            echo "</tr>\n";
            $priorType = $thetypeID;
        }

        echo "<tr>\n";
        echo "<td style=\"height: 26px\">". $row['description'] ."</td>\n";
        echo "<td><input type=\"text\" name=\"avail\" size=\"2\" value=\"". 
            $row['available'] ."\"></td>\n";
        echo "<td><input type=\"text\" name=\"cost\" size=\"7\" maxlength=\"8\" 
         value=\"". $row['cost'] ."\"></td>\n";
        echo "<td>". $row['cost'] ."</td>\n";
        echo "<td><div align=\"left\" style=\"padding-left: 4px\"><a href=\"#\" 
         title=\"Delete this location?\"><img src=\"img/delete.png\" border=\"0\"> 
         </a></div></td>\n";
        echo "</tr>\n";

        $counter++;
    }

if ($priorType != null) {
    echo "</table>";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612