Hello fellow daniwebians, I seem to have run into a problem echoing MySQL/HTML and am hoping one of you may be able to point me in the right direction.

I'm trying to echo portions of HTML(if there is an event for that date) inside HTML that's not echoed, so it should always show up.

I am writing a while loop on either side of the HTML in case there is more than one event on that date. The problem is, it won't display the HTML that's not echoed whenever I use the loop. If I remove the loop, it works fine.

I've used this technique on other sites with no problems in the past... hope I'm just missing something simple... I have to fix it 364 more times :)

<? $result = mysql_query("SELECT * FROM cal WHERE date='(2012-01-01)' "); ?>
   <?
      $num_rows = mysql_num_rows($result);
      $i = 0;
      while($i < $num_rows)
      {
         $row = mysql_fetch_array($result);
   ?>
   <td <? if (!empty($row['event'])){echo "class=\"date_has_event\"";} ?>> 1
      <? if (!empty($row['event'])){
         echo "<div class=\"events\">
            <ul>
               <li>
                  <span class=\"title\">".$row['title']."</span>
                  <span class=\"desc\">".$row['event']."</span>
               </li>
            </ul>
            </div>";}
      ?>
   <?$i++; }?>
   </td>

as you can see, the ultimate goal, if there is not an event...

<td>/*number*/</td>

and if there is one or more...

<td class="date_has_event">/*number*/
 <div class="events">
  <ul>
   <li>
    <span class="title"><?$row['title']?></span>
    <span class="desc"><?$row['event']?></span>
   </li>
   ...
  </ul>
 </div>
</td>

I've tried to put everything in an echo (both the stuff that should always be there and the conditional stuff) with the same result (no HTML at all)... Please, let me know if you have any ideas.

Side question... can I echo an if statement to echo something (or do something similar to cut lines of code)?

IE:

echo "something" . if (condition){echo \'something\'} . "something";

without breaking it up into

echo "something";
if (condition){echo "something";}
echo "something";

Ok the code needs to be tidied up. Instead of using loads of "echo statements", mixed with variables use printf like below. Only use echo for constant strings that you don't want to change. However you should not really mix php with html, or other sorts of design, as it can get really messy and easily break a page.

$result = mysql_query ("SELECT * FROM cal WHERE data = '(2012-01-01)' " ) ;
$events = mysql_num_rows($result) ;

if ( $events )
{
   echo "<td class = 'date_has_event'> <div class='events'> <ul> " ;
   	
   while ( $row = mysql_fetch_array ( $result ) )
   {
      printf("<li><span class='title'>%s</span><span class='desc'>%s</span></li>",$row['title'],$row ['event'] ) ;		   				   
   }
	   
    echo " </ul> </div>  </td>";
	
} else {
	
    echo "<td></td>";

}

As for your second question no your not able to do this, however if there was a way to do this, I would not recommend it, as it looks like it could get really messy. You want your code to be as clear as possible incase you need to go back to it in years to come.

Well I hope that this helps. :)

mikulucky, you are my savior! :). I continuously forget that PHP is a rich language (for some reason in my head I think of a shell of a language).

Your solution is exactly what I was looking for. By adapting your code to my scenario, I've managed to turn a daunting task of changing 365 days worth of coding into 12 short algorithms for each month with little modification.

<?
$n = 1;
$m = 1;
while($n<=31)
{
  $i = str_pad($n, 2, "0", STR_PAD_LEFT);
  $j = str_pad($m, 2, "0", STR_PAD_LEFT);
  if ($n == 8 ||$n == 15||$n == 22||$n == 29){echo "</tr><tr>";}
  $result = mysql_query ("SELECT * FROM cal WHERE data = '(2012-$j-$i)'" ) ;
  $events = mysql_num_rows($result) ;
  if ( $events )
  {
    printf("<td class='date_has_event'>%d<div class='events'><ul>", $n) ;
    while ( $row = mysql_fetch_array ( $result ) )
    {
      printf("<li><span class='title'>%s</span><span class='desc'>%s</span></li>",$row['title'],$row ['event'] ) ;
    }
    echo "</ul></div></td>";
  }
  else
  {
    printf("<td>%d</td>", $n) ;
  }
  if ($n == 31){echo "<td class='padding' colspan='4'></td>";}
  $n++;
}
?>

I'm sure if I toy with it for a second or so it won't be hard to make one block of code for all months, but that's for another time I suppose.

Once again, Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.