Note that there are some explanatory texts on larger screens.

plurals
  1. POGrouping Months Echos Missing Last Month Variables
    primarykey
    data
    text
    <p>I'm trying to get the output grouped by month using PHP and MYSQL. With some help from stack before I was provided a simple solution.</p> <pre><code>if ($last_month != $month) { $monthDiv = "&lt;div class='monthSalute'&gt;$month&lt;/div&gt;"; $last_month = "$month"; } else { $monthDiv = ""; } </code></pre> <p>Easy enough and it made a lot of sense. The only problem is the month variable and <code>$last_month</code> are skipping every once in a while. To explain I did some var dump for last month and month. Notice the skipped month.</p> <pre><code>string(8) "December" string(8) "December" string(7) "January" string(7) "January" string(0) "" string(0) "" string(7) "January" string(7) "January" string(5) "March" string(5) "March" string(5) "April" string(5) "April" </code></pre> <p>This ends up with an output like this:</p> <pre><code>**December -- header** dec1 - game dec5 - game dec31 - game **January -- header** jan4 - game jan7 - game jan26 - game **January -- header** jan27 - game jan31 - game </code></pre> <p>It should be </p> <pre><code>**January -- header** jan4 - game jan7 - game jan26 - game jan27 - game jan31 - game </code></pre> <p>Notice there is no second January Header.</p> <p>Here is the full code as a reference.</p> <pre><code>&lt;?php // Check to see the URL variable is set and that it exists in the database // Connect to the MySQL database include "../../includes/db_conx.php"; //-------------------------------------------------------------------------------------- $sqlcount = "SELECT * FROM discounts WHERE team='cyoram' ORDER BY date ASC"; $sql_countRam = mysqli_query($db_conx,$sqlcount); $calCount = mysqli_num_rows($sql_count); //-------------------------------------------------------------------------------------- if ($calCount &gt; 0) { // get all the product details $x=0; while($row = mysqli_fetch_assoc($sql_countRam)){ $id = $row["id"]; $opponent = $row["opponent"]; $team = $row["team"]; $notes = $row["notes"]; $month = $row["month"]; $buyLink = $row["buyLink"]; $time = $row["time"]; $date = $row["date"]; $formatted_date = date("l d F Y H:i A", strtotime($date)); $raw = "$formatted_date"; $xplod = explode(' ',$raw); /* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ */ include "includes/logos.php"; /* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ */ $classCssRam = "inline-block"; $x++; $classChangeRam = ($x%2 == 0)? 'whiteBackground': 'grayBackground'; /* Print month heading for new month */ if ($last_month != $month) { $monthDiv = "&lt;div class='monthSalute'&gt;$month&lt;/div&gt;"; $last_month = "$month"; } else { $monthDiv = ""; } $product_listRam .= " &lt;div class='aNew' style='clear:both;display:$classCssRam;'&gt; $monthDiv &lt;table width='400px' border='0'&gt; &lt;tr class='$classChangeRam'&gt; &lt;td rowspan='2' class='date' &gt;$xplod[1]&lt;/td&gt; &lt;td class='day'&gt;$xplod[0]&lt;/td&gt; &lt;td rowspan='2' class='centerLogo'&gt;&lt;div class='containImgPos'&gt;&lt;img src='$imgLogo' height='32px' style='position:relative;left:$posImgR;' /&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class='city'&gt;$city&lt;/td&gt; &lt;td rowspan='2' class='butt_pad'&gt;&lt;a href='calendar_edit.php?pid=$id'&gt;edit&lt;/a&gt; &amp;bull; &lt;a href='calendar_list.php?deleteid=$id'&gt;delete&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class='$classChangeRam'&gt; &lt;td class='time $classChangeRam'&gt;$time&lt;/td&gt; &lt;td class='opponent $classChangeRam'&gt;$opponent&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; "; }} ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload