PDA

Click to See Complete Forum and Search --> : tables PHP - resolved


Chrisio
Mar 21st, 2005, 05:53 PM
Hi all,

in the below code i create a single column table on the fly using a returned record set, and this works great my question is how can i create a 2 column table as I just cannot work it out.


<?php
while($row = mysql_fetch_array( $result1 )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'] .'<br>';
echo '<a href="detail.php?item=' .$row['stockcode']. '&dept='.$_GET['dept'].'"> <img src="'.$row['img_location'].' " width="88" height="94"><br>';
echo $row['price']. '<br>';
echo $row['stockcode']. '<br>';
echo "</td>";
echo "</tr>";
}
?>



many thanks chris

The Hobo
Mar 21st, 2005, 11:36 PM
Creating a new column would require another set of <td></td> tags.

<?php
while($row = mysql_fetch_array( $result1 )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'] .'<br>';
echo '<a href="detail.php?item=' .$row['stockcode']. '&dept='.$_GET['dept'].'"> <img src="'.$row['img_location'].' " width="88" height="94"><br>';
echo $row['price']. '<br>';
echo $row['stockcode']. '<br>';
echo "</td><td>";
echo $row['another_field'] . '<br>';
echo "</td>";
echo "</tr>";
}
?>

Note you could also do this, which I find much cleaner:

<?php
while($row = mysql_fetch_array( $result1 )) {
// Print out the contents of each row into a table
echo '<tr>
<td>' . $row['name'] .'<br>
<a href="detail.php?item=' .$row['stockcode'] . '&dept=' . $_GET['dept'] . '">
<img src="' . $row['img_location'] . '" width="88" height="94">
<br>' . $row['price']. '<br>' . $row['stockcode']. '<br>
</td>
<td>' . $row['another_field'] . '<br></td>
</tr>';
}
?>

visualAd
Mar 22nd, 2005, 01:36 AM
Note you could also do this, which I find much cleaner:

<?php
while($row = mysql_fetch_array( $result1 )) {
// Print out the contents of each row into a table
echo '<tr>
<td>' . $row['name'] .'<br>
<a href="detail.php?item=' .$row['stockcode'] . '&dept=' . $_GET['dept'] . '">
<img src="' . $row['img_location'] . '" width="88" height="94">
<br>' . $row['price']. '<br>' . $row['stockcode']. '<br>
</td>
<td>' . $row['another_field'] . '<br></td>
</tr>';
}
?>
And to make it even cleaner:

<?php while($row = mysql_fetch_array( $result1 )):
// Print out the contents of each row into a table ?>
<tr>
<td><?php ecoh($row['name']) ?><br />
<a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
<img src="<?php echo($row['img_location']) ?>" width="88" height="94">
<br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
</a>
</td>
<td><?php echo($row['another_field']) ?><br></td>
</tr>
<?php endwhile; ?>

Chrisio
Mar 22nd, 2005, 01:46 AM
thanks for the replys, I havent made myself clear, at the moment the code that I have produces 1 long table 1 column wide that is populated

such as

item1
item2
item3

and so on


what i want is

item1 item2
item3 item4
item5 item6

and so on.

can you help.

chris
:thumb:

visualAd
Mar 22nd, 2005, 02:02 AM
/you just need to use the mod operator % to do this. @The mod operator returns the remainder after division, which can be used to determine whether or not to print a <tr> tag.

<?php $count = 0; while($row = mysql_fetch_array( $result1 )):
// Print out the contents of each row into a table ?>
<?php if($count % 2 == 0): ?><tr><?php endif; // start the row?>
<td><?php ecoh($row['name']) ?><br />
<a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
<img src="<?php echo($row['img_location']) ?>" width="88" height="94">
<br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
</a>
</td>
<td><?php echo($row['another_field']) ?><br></td>
<?php if($count % 2 == 1): ?></tr><?php endif; // end the row?>
<?php ++$count endwhile; ?>

Chrisio
Mar 22nd, 2005, 02:32 AM
thanks for replying so fast, when i use this code i get
Parse error: parse error in C:\Program Files\Apache Group\Apache2\htdocs\catalog.php on line 61

the problem being is there is no line 61 in the code. This is all the code from that page.

<?php
require('db_connect.php');

$dept = $_GET['pid'];
$isle = $_GET['item'];
//get the cats from the db//
$result = mysql_query("SELECT DISTINCT category FROM shop WHERE toplevel = '".$dept."'") or die(mysql_error());
$result1= mysql_query("SELECT stockcode, name, price, description, img_location FROM shop WHERE category = '".$isle."'") or die(mysql_error());
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Catalog</title>
</head>

<body>
<?php

echo '<div align="left">';
echo '<font face="Century Gothic">';
echo '<p align="center">&nbsp;</p>';
echo "<table border='0'>";
echo "<tr> <th>Category</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo '<a href="catalog.php?item=' . $row['category'] . '&dept='.$_GET['pid'].'">' . $row['category'] .'</a>' ;
echo "</td><td>";
echo "</td></tr>";
// Get all the data from the "example" table
echo '<div align="center"></div>';
echo '<font face="Century Gothic">';
echo '<p align="center">&nbsp;</p>';
echo "<table border='0'>";



$count = 0; while($row = mysql_fetch_array( $result1 )):
// Print out the contents of each row into a table ?>
<?php if($count % 2 == 0): ?><tr><?php endif; // start the row?>
<td><?php ecoh($row['name']) ?><br />
<a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
<img src="<?php echo($row['img_location']) ?>" width="88" height="94">
<br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
</a>
</td>
<td><?php echo($row['another_field']) ?><br></td>
<?php if($count % 2 == 1) ?></tr><?php endif; // end the row?>
<?php ++$count endwhile; ?>


<p>
</p>
</body>
</html>



thanks for the help again

chris

visualAd
Mar 22nd, 2005, 02:42 AM
The error was caused becuase you missed the closing curly bracket for the first while statement.

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo '<a href="catalog.php?item=' . $row['category'] . '&dept='.$_GET['pid'].'">' . $row['category'] .'</a>' ;
echo "</td><td>";
echo "</td></tr>";
// Get all the data from the "example" table
echo '<div align="center"></div>';
echo '<font face="Century Gothic">';
echo '<p align="center">&nbsp;</p>';
echo "<table border='0'>";
} /// &lt;---- you forgot this

Chrisio
Mar 22nd, 2005, 02:58 AM
I dont know what im doing wrong but now im getting parse error on line 51 which is
<?php if($count % 2 == 1) ?></tr><?php endif; // end the row?>

code now looks like


<?php
require('db_connect.php');

$dept = $_GET['pid'];
$isle = $_GET['item'];
//get the cats from the db//
$result = mysql_query("SELECT DISTINCT category FROM shop WHERE toplevel = '".$dept."'") or die(mysql_error());
$result1= mysql_query("SELECT stockcode, name, price, description, img_location FROM shop WHERE category = '".$isle."'") or die(mysql_error());
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Catalog</title>
</head>

<body>
<?php

echo '<div align="left">';
echo '<font face="Century Gothic">';
echo '<p align="center">&nbsp;</p>';
echo "<table border='0'>";
echo "<tr> <th>Category</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo '<a href="catalog.php?item=' . $row['category'] . '&dept='.$_GET['pid'].'">' . $row['category'] .'</a>' ;
echo "</td><td>";
echo "</td></tr>";
// Get all the data from the "example" table
echo '<div align="center"></div>';
echo '<font face="Century Gothic">';
echo '<p align="center">&nbsp;</p>';
echo "<table border='0'>";
}


$count = 0; while($row = mysql_fetch_array( $result1 )):
// Print out the contents of each row into a table ?>
<?php if($count % 2 == 0): ?><tr><?php endif; // start the row?>
<td><?php ecoh($row['name']) ?><br />
<a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
<img src="<?php echo($row['img_location']) ?>" width="88" height="94">
<br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
</a>
</td>
<td><?php echo($row['another_field']) ?><br></td>
<?php if($count % 2 == 1) ?></tr><?php endif; // end the row?>
<?php ++$count endwhile; ?>


<p>
</p>
</body>
</html>



thanks again

chrisio

visualAd
Mar 22nd, 2005, 05:51 AM
It should be:

<?php if($count % 2 == 1): ?></tr><?php endif; // end the row?>

Chrisio
Mar 22nd, 2005, 05:52 AM
It should be:

<?php if($count % 2 == 1): ?></tr><?php endif; // end the row?>



yeps thats it. thanks very much works a treat
chrisio

visualAd
Mar 22nd, 2005, 09:57 AM
If your problem has now been solved, don't forget to edit the first post and mark it as resolved. :wave: