Jump to content

[SOLVED] Help with displaying result from mysql fetch


Cappen

Recommended Posts

I have a shopping cart that is displaying my items and I want to change the way they are displayed. The way the result is displayed now is like this:

 

result1

result2

result3

result4

 

But I want to change it to:

 

result1result2

result3result4

 

Instead of having a table row with only one table data, I want to have a table row with two table data.

 

The problem with the below code is that it runs the echo for each row and I don't know how to make it run for two rows at a time.

 

I should say I don't know anything about php programming but I have so far managed to change a lot of code just by looking at it and learn but with this code I guess I have to use some other type of function to get the job done.

 

I would appreciate any help please.

 

 

<?php

// database connection
mysql_connect("$host","$user","$pass");

// database selection
mysql_select_db("$database");

// database query to select the categories
$result = mysql_query("select distinct * from products WHERE maingroup = '$maingroup' AND secondgroup = '$secondgroup' ORDER BY item") ;

// counter for how many iterations have been done
$i=0;

while($row = mysql_fetch_row($result)) {
$color = ($coloralternator++ %2 ? "D9D9D9" : "E9E9E9");
echo "<TR BGCOLOR=\"#$color\"><TD><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\"><IMG SRC=\"images/$row[6]\" BORDER=\"0\" WIDTH=\"$thumbs_width\" HEIGHT=\"$thumbs_height\"></A></TD>";
echo "<TD><font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">$row[2]</FONT></TD>";
echo "<TD><font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">$row[3]</FONT></TD>";
echo "<TD ALIGN=\"RIGHT\"></a><font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\">$row[5]</font></TD>";
echo "<TD><FORM ACTION=\"item_show.php?code_no=$row[2]\" method=POST><INPUT TYPE=submit  NAME=Submit VALUE=\"$txt_buy_now\" class=\"button\"></TD></FORM></TR>"; 

}

?>

Basically in HTML, the <tr> tag designates a new row. Therefore, the logic behind generating two columns would simply be to create a simple counter. Increment the counter every time you look through your array. Then it would just be a matter of dividing the counter by the number of columns that you want (hint:modulus).

 

If the number is divisible (returns a remained of zero) then you can close the <tr> tag and open a new one to designate the next row.

 

If you need further help, I can always put up some code, but I thought it would be best to have an understanding of how it works.

Perfect, thank you very much GingerRobot!

 

The code you pointed me to worked great. I didn't use all of it, just took some parts and put in my code. All I needed was:

 

$max_columns = 2;

which I put above the "while" loop.

 

if($i == 0)

which I put above the "echo" code.

 

	if(++$i == $max_columns) {
echo "</TR>";
$i=0;
}  // end if	

which I put below the "echo" code.

 

I also had to move this code:

 

$color = ($coloralternator++ %2 ? "D9D9D9" : "E9E9E9");

 

above the if statement because it didn't want to work otherwise. I also wanted to change the display of the table and use two <tr></tr> but I couldn't make it work so I had to use only one.

 

Below is the complete working code if someone else is interested in it:

 

<table cellpadding="3" cellspacing="3" border="1">
<?php

// database connection
mysql_connect("$host","$user","$pass");

// database selection
mysql_select_db("$database");

// database query to select the categories
$result = mysql_query("select distinct * from products WHERE maingroup = '$maingroup' AND secondgroup = '$secondgroup' ORDER BY item") ;

// counter for how many iterations have been done
$i=0;
$max_columns = 2;

while($row = mysql_fetch_row($result)) {

$color = ($coloralternator++ %2 ? "F0F0F0" : "D9D9D9");
if($i == 0)

echo "<TR BGCOLOR=\"#$color\">";

echo "<TD width='100' height='125' align='left' valign='top'>";
echo "<a href=\"item_show.php?code_no=$row[2]\" target=\"main\"><img src=\"images/$row[6]\" border=\"0\" width=\"$thumbs_width\" height=\"$thumbs_height\"></a><br>";
echo "<font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\"><b><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">$row[3]</a></b></font><br>";
echo "</TD>";

echo "<TD width='100' height='125' align='right' valign='top'>";
echo "<font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\">Artnr: $row[2]</font><br>";
echo "<font size=\"2\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\">Pris: $row[5]</font><br>";
echo "<form action=\"item_show.php?code_no=$row[2]\" method=POST><input type=submit  name=Submit VALUE=\"$txt_buy_now\" class=\"button\"></form>"; 
echo "</TD>";

// increment counter - if counter = max columns, reset counter and close row
if(++$i == $max_columns) {
echo "</TR>";
$i=0;
}  // end if	
}

?>
</table>

I did manage to solve the multiple <tr> code finally :) I had to put a <table> start both outside the php code and inside. Oh and btw, in the example code the fetch data said "mysql_fetch_array" but mine has "mysql_fetch_row". Don't know the difference but I left my as is and it worked just the same as the array did.

 

If you want to see the difference on my site you can check it out here:

 

Old page: http://www.birkaimport.se/item_list_old.php?maingroup=Minicross&secondgroup=Bromsar

 

New page: http://www.birkaimport.se/item_list.php?maingroup=Minicross&secondgroup=Bromsar

 

 

Well here's the final complete working code...

 

<table cellpadding="3" cellspacing="0" border="0">
<?php

// database connection
mysql_connect("$host","$user","$pass");

// database selection
mysql_select_db("$database");

// database query to select the categories
$result = mysql_query("select distinct * from products WHERE maingroup = '$maingroup' AND secondgroup = '$secondgroup' ORDER BY item") ;

// counter for how many iterations have been done
$i=0;
$max_columns = 4;

while($row = mysql_fetch_row($result)) {

$color = ($coloralternator++ %2 ? "D9D9D9" : "E9E9E9");
if($i == 0)

echo" <TR>";
echo "<TD>";

echo "<table cellpadding=\"3\" cellspacing=\"5\" border=\"0\" bgcolor=\"#$color\">";

echo "<TR>";
echo "<TD width=\"125\" height=\"50\" align=\"left\" valign=\"top\" bgcolor=\"#444444\">";
echo "<font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\"><b><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">$row[3]</a></b></font><br>";
echo "</TD>";
echo "</TR>";

echo" <TR>";
echo "<TD width=\"$125\" height=\"100\" align=\"left\" valign=\"top\">";
echo "<a href=\"item_show.php?code_no=$row[2]\" target=\"main\"><img src=\"images/$row[6]\" border=\"0\" width=\"$thumbs_width\" height=\"$thumbs_height\"></a><br>";
echo "<font size=\"1\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\">Artnr: $row[2]</font><br>";
echo "</TD>";
echo "</TR>";

echo" <TR>";
echo "<TD width=\"$125\" height=\"50\" align=\"left\" valign=\"bottom\"><form action=\"item_show.php?code_no=$row[2]\" method=POST>";
echo "<font size=\"2\" face=\"Trebuchet MS, Verdana, Arial, Helvetica, sans-serif\">Pris: $row[5] kr</font><br>";
echo "<input type=\"image\" src=\"/images/button_info.jpg\" name=Submit></form>"; 
echo "</TD>";
echo "</TR>";

echo "</table>";

// increment counter - if counter = max columns, reset counter and close row
if(++$i == $max_columns) {
echo "</TD></TR>";
$i=0;
}  // end if	
}

?>

</tr>
</table>

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.