Page 1 of 1

Inconsistent PHP Output

Posted: Thu Apr 22, 2010 10:29 pm
by jballou
Hey everyone,

I'm having a bit of a weird problem. I have a while loop that iterates 9 times. Each time it outputs the following PHP code:

Code: Select all

<div class="thumbnail"><a href="<?= $site_root?><?= str_replace(" ", "_", $name)?>/">
     <div class="thumbnail-pic"><img src="<?= $site_root ?>media/profiles/<?= $id ?>/main.jpg" /></div>
     <div class="thumbnail-info"><strong><?= $name ?></strong><br />
     <?= $city ?>, <?= $state ?></div></a>
</div>
Most times this outputs the correct html which is:

Code: Select all

<div class="thumbnail"><a href="/John_Damon/">
     <div class="thumbnail-pic"><img src="/media/profiles/6/p/47/382_tn.jpg"></div>
     <div class="thumbnail-info"><strong>John Damon</strong><br>Oakland, CA</div></a>
</div>
But every once in a while it outputs the following instead which totally breaks my layout:

Code: Select all

<div class="thumbnail"><a href="/John_Damon/"> </a>
     <div class="thumbnail-pic"><a href="/John_Damon/"><img src="/media/profiles/6/p/47/382_tn.jpg"></a></div>
     <a href="/John_Damon/"> </a> <div class="thumbnail-info"><a href="/John_Damon/"><strong>John Damon</strong><br>Oakland, CA</a></div></a>
</div>
It most often hits on the 8th iteration of the while loop. I have no idea why the HTML output would be inconsistent as to what I'm actually writing in PHP. I don't even know where it's getting the alternate HTML code from as it is nowhere to be found anywhere in my source code.

I'm stumped.

Re: Inconsistent PHP Output

Posted: Thu Apr 22, 2010 11:10 pm
by Eran
Please show the actual PHP code, including where the data comes from

Re: Inconsistent PHP Output

Posted: Thu Apr 22, 2010 11:19 pm
by jballou
Ok, here goes:

Code: Select all

<div class="headline"><img src="images/headline-cutie.jpg" width="619" height="30" /></div>
     <div class="thumbnails">
<?
                $strSQL = "select id, name, city, state from models where status='1' order by rand() LIMIT 9";
                $rst = mysql_query($strSQL);
                echo mysql_error();
        
                $i = 1;
                while ($row = mysql_fetch_array($rst, MYSQL_NUM)) {
                    $id = $row[0];
                    $name = $row[1];
                    $city = $row[2];
                    $state = $row[3];			
		    $url_name = str_replace(" ", "_", $name);
			?>



            <?
					if ($i % 3 == 1) { ?><div class="row<? if ($i == 1) { ?> first<? }?>"><? }
					if ($i % 3 != 0) {
			?>
                        <!-- PRINT THE NORMAL THUMBNAIL  -->

                      <div class="thumbnail"><a href="<?= $site_root.$url_name ?>/">
                      <div class="thumbnail-pic"><img src="<?= $site_root ?>media/profiles/<?= $id ?>/main.jpg" /></div>
                      <div class="thumbnail-info"><strong><?= $name ?></strong><br />
                      <?= $city ?>, <?= $state ?></div></a>
                      </div>
                      
                      
            <?
					
				}
				
				else {
					
					?>
                    
                    <!-- PRINT THE LAST THUMBNAIL  AND THE END OF THE ROW INFO  -->
                    
                      <div class="thumbnail last"><a href="<?= $site_root.$url_name ?>/">
                      <div class="thumbnail-pic"><img src="<?= $site_root ?>media/profiles/<?= $id ?>/main.jpg" /></div>
                      <div class="thumbnail-info"><strong><?= $name ?></strong><br />
                      <?= $city ?>, <?= $state ?></div></a>
                      </div>
                      
                      <div class="clear"></div>
                    </div>
                    
                    <?
					
				}
				
				$i++;
				
			} //endwhile
			?>

Re: Inconsistent PHP Output

Posted: Thu Apr 22, 2010 11:40 pm
by Eran
The logic inside the loop is somewhat off.
You open a div every 3 iterations

Code: Select all

if ($i % 3 == 1) { ?><div class="row<?  if ($i == 1) { ?> first<? }?>"><? }
However this div is never closed. After some iterations the browser closes it with one of the other closing </div> tags and tried to make sense of the rest
Also, please notice you cannot nest block level elements (div) inside inline level elements (anchors - a). This results in invalid markup, even though some browser ignore that and display it correctly. Have a look at the following link for more details -
http://www.w3.org/TR/html401/struct/global.html#h-7.5.3

Re: Inconsistent PHP Output

Posted: Sat Apr 24, 2010 3:08 pm
by jballou
Hey

Thanks for answering. I see what you're saying, but the <div> is indeed closed with this part of the code:

Code: Select all

else {
                                       
                                        ?>
                   
                    <!-- PRINT THE LAST THUMBNAIL  AND THE END OF THE ROW INFO  -->
                   
                      <div class="thumbnail last"><a href="<?= $site_root.$url_name ?>/">
                      <div class="thumbnail-pic"><img src="<?= $site_root ?>media/profiles/<?= $id ?>/main.jpg" /></div>
                      <div class="thumbnail-info"><strong><?= $name ?></strong><br />
                      <?= $city ?>, <?= $state ?></div></a>
                      </div>
                     
                      <div class="clear"></div>
                    </div>
As for nesting a <div> inside the <a> I understand what you mean about it being invalid code, so I'll find an alternative way to code this. Thanks for the heads up. I still don't understand the inconsistency of the output though. Have any thoughts about that?

Re: Inconsistent PHP Output

Posted: Sat Apr 24, 2010 3:33 pm
by Eran
It will only close that div if the loop reaches the 3rd thumbnail in the row. If the row ends on the 1st or 2nd thumbnail that div will not close and the HTML following it will be messed up. You need to add a condition after the loop that will close it in that case.