Jump to content


Photo

Blocks of PHP versus snippets


  • Please log in to reply
2 replies to this topic

#1 Levan

Levan
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 06 June 2006 - 07:35 AM

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
<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>
This works as expected no problems
However in the next example the background doesnt change...
 <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>

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.

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 June 2006 - 12:43 PM

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

try placing it after like this:

<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>



That work better?

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Levan

Levan
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 06 June 2006 - 10:32 PM

Dont I feel light a right wally...

Thanks mate - such a simple fix





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users