Page 1 of 1

Have a question Please?

Posted: Sat Apr 19, 2003 3:21 pm
by jackal
If i have 9 rows in my database, this outputs like this:
[ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ][ 7 ][ 8 ][ 9 ]

I have desperately tried 3 different tutorials for implementing columns but i have had no success.

Please modify my code so it outputs like this:
[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]
(loops the code 3 times then inserts a </tr><tr> then does 3 next rows in database and so on...)

Here is my code: (to view what the output currently looks like go to: http://www.logicalreef.com/zoos.php )

Code: Select all

<table border="0" cellpadding="0" cellspacing="0" width="580" class="global">
            <tr> 
              <td align="center">
               <table width="100%%" border="0">
                  <tr>
                    <?
mysql_pconnect("localhost","username","password");
mysql_select_db("databasename");
$result = mysql_query("select * from zoos ");
while($r=mysql_fetch_array($result))

&#123;
$zooID=$r&#1111;"zooID"];
$avail=$r&#1111;"avail"];
$price=$r&#1111;"price"];
?>
                    <?php

if($avail=="avail")
&#123;
$status = "<td align='center' class='greencell'>Available</td>";
&#125;
elseif($avail=="sold")
&#123;
$status = "<td align='center' class='redcell'>Sold</td>";
&#125;
elseif($avail=="hold")
&#123;
$status = "<td align='center' class='orangecell'>On Hold</td>";
&#125;

if($avail=="sold")
&#123;
$disabled = "#";
&#125;
elseif($avail=="hold")
&#123;
$disabled = "#";
&#125;
elseif($avail=="avail")
&#123;
$disabled = "query.php?zooID=$zooID&set=sold&userid=$userid";
&#125;
?>
                    <td align="center"> <table width="150" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                        <tr> 
                          <td><table width="150" border="0" cellspacing="0" cellpadding="0">
                              <tr> 
                                <td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                                    <tr> 
                                      <td align="left" bgcolor="#666666"><font color="#FFFFFF">&nbsp;<?php echo "$zooID" ?></font></td>
                                      <td align="right" bgcolor="#666666"><font color="#FFFFFF"><strong>$<?php echo "$price" ?>&nbsp;</strong></font></td>
                                    </tr>
                                  </table></td>
                              </tr>
                              <tr> 
                                <td><a href="sale/<?php echo "$zooID" ?>big.jpg"><img src="sale/<?php echo "$zooID" ?>.jpg" alt="<?php echo "$zooID" ?>" border="0"></a></td>
                              </tr>
                              <tr> 
                                <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr> <?php echo "$status" ?>
                                      </tr>
                                  </table></td>
                              </tr>
                            </table></td>
                        </tr>
                      </table></td>
                    <?php &#125; ?>
                  </tr>
                </table></td>
          </table>

Posted: Sat Apr 19, 2003 4:04 pm
by twigletmac
You can try something like this:

Code: Select all

<table border="0" cellpadding="0" cellspacing="0" width="580" class="global"> 
<tr> 
	<td align="center"> 
		<table width="100%" border="0"> 
		<tr> 
<?php
@mysql_pconnect('localhost', 'username', 'password') or die(mysql_error());
@mysql_select_db('databasename') or die(mysql_error());

// specifying exactly which fields you want to return from the database is good
// coding practise
$sql = "SELECT zooID, avail, price FROM zoos";
@$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

// calculate the number of rows returned
$num_rows = mysql_num_rows($result);
// calculate how many empty cells you're going to need
$empty_cells = (ceil($num_rows/3) * 3) - $num_rows;

// initialise a counter so that you can work out how may rows you've
// been through
$i = 1; 
while ($row = mysql_fetch_array($result)) { 
	$zooID = $row['zooID']; 
	$avail = $row['avail']; 
	$price = $row['price']; 

	if ($avail == 'avail') { 
		$status = 'Available';
		$status_class = 'greencell';
		$disabled = 'query.php?zooID='.$zooID.'&set=sold&userid='.$userid;
	} elseif ($avail == 'sold') { 
		$status = 'Sold';
		$status_class = 'redcell';
		$disabled = '#';
	} elseif ($avail == 'hold') { 
		$status = 'On Hold';
		$status_class = 'orangecell';
		$disabled = '#';
	}

	// before every fourth record end the current HTML row and start a new one
	if (($i % 3) == 1 && $i != 1) {
?>
		</tr>
		<tr>
<?php
	}

?> 
			<td align="center">
				<table width="150" border="0" cellspacing="0" cellpadding="0"> 
				<tr> 
					<td align="left" bgcolor="#666666"><font color="#FFFFFF"><?php echo $zooID ?></font></td> 
					<td align="right" bgcolor="#666666"><font color="#FFFFFF"><strong>$<?php echo $price ?></strong></font></td> 
				</tr> 
				<tr> 
					<td colspan="2">
						<a href="sale/<?php echo $zooID ?>big.jpg"><img src="sale/<?php echo $zooID ?>.jpg" alt="<?php echo $zooID ?>" border="0"></a>
					</td> 
				</tr> 
				<tr> 
					<td align="center" class="<?php echo $status_class ?>" colspan="2">
						<?php echo $status ?>
					</td>
				</tr> 
				</table>
			</td> 
<?php
	$i++; // increment the counter
} // end while

// Add empty cells if the number of records is not a multiple of 3
for ($i=0; $i < $empty_cells; $i++) {
	// it should say &nbsp; inside the cell (without the amp; bit)
	// but the forum isn't playing nicely
?>
	<td>&nbsp;</td>
<?php
} // end for
?> 
		</tr> 
		</table>
	</td>
</tr>
</table>
May not work exactly (haven't got your database to test it on) but should show the theory.

Mac