Jump to content

relatively simple question


mindapolis

Recommended Posts

Hi, I'm working on a product page and I got it to list all the products in one column of a table, but when I add a second column it just duplicates the first column.  What do I need to change? 

 

<?php
require_once("functions.php"); 
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
td {
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #30C;
border-right-color: #30C;
border-bottom-color: #30C;
border-left-color: #30C;
}
</style>
</head>

<body>
<form action="" method="post" name="catalog">
<?php 

DatabaseConnection();  

  $query = "SELECT * FROM treats";
        $result_set = mysql_query($query) or die(mysql_error());

        $output = "<table>";
        while ($row = mysql_fetch_array($result_set))
        {
            $output .= (" 
			<tr>
				<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>  
				<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>  					
			</tr>
	");
        }
        $output .= "</table>";
        echo $output;
?>   
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/248703-relatively-simple-question/
Share on other sites

You need to call mysql_fetch_array a second time to get the next set of information for that column.

 

$row is only updated with the next bit of information once you assign it a value (i.e. $row = mysql_fetch_array ( $result_set ); )

 

EDIT: Can this topic be moved to PHP help because this has nothing to do with MySQL.

 

~juddster

so would put that line before the second <td>  block? 

 

Sorry I posted it in the wrong place. 

You need to call mysql_fetch_array a second time to get the next set of information for that column.

 

$row is only updated with the next bit of information once you assign it a value (i.e. $row = mysql_fetch_array ( $result_set ); )

 

EDIT: Can this topic be moved to PHP help because this has nothing to do with MySQL.

 

~juddster

<?php
require_once("functions.php"); 
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
td {
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #30C;
border-right-color: #30C;
border-bottom-color: #30C;
border-left-color: #30C;
}
</style>
</head>

<body>
<form action="" method="post" name="catalog">
<?php 

DatabaseConnection();  

  $query = "SELECT * FROM treats";
        $result_set = mysql_query($query) or die(mysql_error());
$i = 0;
        $output = "<table>";
        while ($row = mysql_fetch_array($result_set))
        {
/* Do we need a new row? */
if ( $i % 2 == 0 )
{
	/* Yes, so output the table row stuff */
	$output .= ( $i != 0 ? '</tr>' : '' ) . '<tr>';
}

$output .= "<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>";

++$i;
        }
        $output .= "</tr></table>";
        echo $output;
?>   
</form>
</body>
</html>

 

~juddster

didn't help. . . 

 

<?php
require_once("functions.php"); 
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
td {
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #30C;
border-right-color: #30C;
border-bottom-color: #30C;
border-left-color: #30C;
}
</style>
</head>

<body>
<?php
navBar();
echo "<form action=\"\" method=\"post\" name=\"catalog\">";
  
DatabaseConnection();  

  $query = "SELECT * FROM treats"; 
        $result_set = mysql_query($query) or die(mysql_error());
$i = 0;
        $output = "<table>";
        while ($row = mysql_fetch_array($result_set))
        {
/* Do we need a new row? */
if ( $i % 2 == 0 )

{
/* Yes, so output the table row stuff */
$output .= ( $i != 0 ? '</tr>' : '' ) . '<tr>';
}
            $output .= (" 
			<tr>
				<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>  
				<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>  					
			</tr>
	");
        }
        $output .= "</table>";
        echo $output;
?>   
</form>
</body>
</html>

Not 100% I got this right without testing it.  BTW, you can use this for any number of columns.

<?php 

DatabaseConnection();  
$cells_per_row = 2; 
$headcells=$cells_per_row;
  $query = "SELECT * FROM treats";
        $result_set = mysql_query($query) or die(mysql_error());
$counter=0;
        $output = "<table>";
        while ($row = mysql_fetch_array($result_set)){ 
	$counter++;
    if(($counter % $cells_per_row) == 1)  {$output .= "<tr>"; }
            $output .= "<td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td>"; 
if(($counter %  $cells_per_row) == 0) { $output .= "</tr>\r"; }
// just in case we haven't closed the last row
// this would happen if our result set isn't divisible by $cells_per_row
if(($counter % $cells_per_row) != 0) { $output .= "</tr>\r"; }

        }
        $output .= "</table>";
        echo $output;
?>  

IF you are just  displaying your "treats" results over two columns then you can do as I posted below.  It's my version from above with the closing bracket moved and not a "copied/edited version of yours".

<?php
$cells_per_row = 2; 
  $query = "SELECT * FROM treats";
        $result_set = mysql_query($query) or die(mysql_error());
        $output ="<table>"; 
	$counter=0;
        while ($row = mysql_fetch_array($result_set)){ 
	$counter++;	
	if(($counter % $cells_per_row) == 1){ $output .="<tr>"; }
            $output .="<td style=\"width:400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br />  Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /></td>"; 
if(($counter %  $cells_per_row) == 0) { $output .="</tr>\r"; }
}//while ($row = mysql_fetch_array
// just in case we haven't closed the last row
// this would happen if our result set isn't divisible by $cells_per_row 
if(($counter % $cells_per_row) != 0) { $output .="</tr>\r"; }
       $output .="</table>";
        echo $output;
?>

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.