Jump to content

Blocks of PHP versus snippets


Levan

Recommended Posts

Hi all,

I am attempting to build a PHP page (based on 4.03) linking to a MYSQL data base.
I have coded a table to output the data in two ways. Basically using a while loop to repeat the table rows
I have include a variable $i that increments and using the % (modular??) to check for a remainder I change
the back ground of each cell to ease readibility in a long result.
In the first example I basically built the code entirely in php echoing all the html
Here it is
[code]
<table width="90%" height="100%" border="1" bordercolor="#003300" bgcolor="#FFFFFF">
    <?php $i=1;?>
  <?php
  /* Create a repeatable row of cells to hold both the categories and descriotions and an image using the catkey.  Images should be stored in ./images/ with the name category(cat number).gif.
    */      
    do {
    $bgcolor="#FFFFFF";
    if($i%2 == 0){$bgcolor="#FFFF99";};
    echo "<tr bgcolor=$bgcolor><td bgcolor=$bgcolor><blockquote><h3 align=\"left\">";
    echo "<a href=\"./brands.php?id_brand=",$row_categories['catkey'],"\">";
    echo "$i.";
    echo $row_categories['catname'];
    echo "</a></h3><blockquote><p align=\"left\">";
    echo $row_categories['catdesc'];
    $i++;
    echo "</p></blockquote></blockquote></td>
    <td width=10%><img src=\"images/category";
    echo $row_categories['catkey'];
    echo ".gif\" width=\"100\" height=\"100\" align=\"absmiddle\"/></td></tr>
    ";
    }
    while ($row_categories = mysql_fetch_assoc($categories)); ?>
  </table>
[/code]
This works as expected no problems
However in the next example the background doesnt change...
[code]
<table width="90%" border="1" bordercolor="#003300" bgcolor="#FFFFFF" id="brand">
      <?php $i=1;
          $bg_color="#FFFFFF";?>
    <?php do { ?>
    <?php if ($i % 2 == 0){$bg_color="#FFFF99";}
         $brandname=$row_brands['brandname'];
        $brandid=$row_brands['brandkey'];?>        
         <tr bgcolor=<?php echo $bg_color; ?>><td><div align="left">
        <h3>
        <a href="items.php?id_item=<?php echo $brandid; ?>&id_itemcat=<?php echo row_catcheck['catkey']; ?>"><?php echo $i; ?> . <?php echo $brandname; ?></a></a></h3>
      <blockquote>
        <p><?php echo $row_brands['branddesc']; ?>
            </p>
          </blockquote>
      </div>
  </td>
      <td width="10%"><div id="image"><a href="#nogo" class="p1"><img src="images/products/brand<?php echo $brandid; ?>.gif" alt="Hover for full image" width="95px" height="95px"/><img class="large" src="images/products/brand<?php echo $brandid; ?>.gif" alt="Hover for full image"/></div></td>
  </tr><?php $i++;?> <?php
      }
      while ($row_brands = mysql_fetch_assoc($brands)); ?>
  </table>
[/code]

So I used the same piece of code to change the background but in the second example it doesnt work ...the background changes to the second color and remains that way...

An explanation would be appreciated... I can link in the website if anyone needs explanation but I didnt think it was required.
Link to comment
https://forums.phpfreaks.com/topic/11303-blocks-of-php-versus-snippets/
Share on other sites

In your second code $bgcolor="#FFFFFF"; is before do {

try placing it after like this:

[code]
<table width="90%" border="1" bordercolor="#003300" bgcolor="#FFFFFF" id="brand">
<?php $i=1; ?>
<?php do {
$bg_color="#FFFFFF"; ?>
<?php if ($i % 2 == 0){$bg_color="#FFFF99";}
$brandname=$row_brands['brandname'];
$brandid=$row_brands['brandkey'];?>
<tr bgcolor=<?php echo $bg_color; ?>><td><div align="left">
<h3>
<a href="items.php?id_item=<?php echo $brandid; ?>&id_itemcat=<?php echo row_catcheck['catkey']; ?>"><?php echo $i; ?> . <?php echo $brandname; ?></a></a></h3>
<blockquote>
<p><?php echo $row_brands['branddesc']; ?>
</p>
</blockquote>
</div>
</td>
<td width="10%"><div id="image"><a href="#nogo" class="p1"><img src="images/products/brand<?php echo $brandid; ?>.gif" alt="Hover for full image" width="95px" height="95px"/><img class="large" src="images/products/brand<?php echo $brandid; ?>.gif" alt="Hover for full image"/></div></td>
</tr><?php $i++;?> <?php
}
while ($row_brands = mysql_fetch_assoc($brands)); ?>
</table>
[/code]



That work better?

Regards
Liam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.