Jump to content

Display data from MySQL


microbert

Recommended Posts

I am managing a shop website which is using php and mysql for data.

 

The website has a section that will display the related products with the one that you are watching. Now my problem is that if there are more than 5 items it will continue to display all the items in one row with the consequent that the page will not display well.

 

I need to find a way to begin a new row after the 5th item so it will display 5 items in each row.

 

this is my current code that is responsible for showing the related items. There is also a picture attached with the problem.

 

while ($row = mysql_fetch_array($retd))

{

$code = $row["code"];

$name = $row["name"];

 

echo("<td width=150 align=center>");

echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");

echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");

echo("</td>");

}

 

 

 

Does anyone know how can I do this?

post-98530-13482403367627_thumb.png

Link to comment
Share on other sites

Basically, you'll need to use a little logic to end rows and create new ones. One way to do that is to increment a counter and then just test the counter's value.

 

My code is a little different than yours, just for simplicity but it's the same concept.

// create an array to loop through
$array = range(1,20);

// set number of columns per row
$cols = 5;

// start our counter
$i = 0;

echo '<table>';
foreach($array as $arr)
{
// check the modulus of our counter
// if it is 0 we have to start a new row
if ($i % $cols == 0) {
	if ($i > 0) {
		// end the current row if it isnt the first row
		echo '</tr>';
	}

	echo '<tr>';	
}

echo '<td>' . $arr . '</td>';

// increment the counter
// this is important, dont forget this
$i++;
}
echo '</tr>';
echo '</table>';

Link to comment
Share on other sites

Do the same thing only in the while loop.

// define the number of columns per row
$cols = 5;

// start the counter
$i = 0;

while ($row = mysql_fetch_array($retd))
{
$code = $row["code"];
$name = $row["name"];

if ($i % $cols == 0) {
	if ($i > 0) {
		echo '</tr>';
	}
	echo '<tr>';
}

echo("<td width=150 align=center>");
echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");
echo("</td>");

// increment the counter
$i++;
}

Link to comment
Share on other sites

I have managed to solve the problem that was showing only two items,

 

now I'd like to make it to show 5 items on 1 line and then continue on another line.

 

this the code:

 

 

// define the number of columns per row
$cols = 5;
// start the counter
$count = 0;										
while ($row = mysql_fetch_array($retd)) 
{ 
$code = $row["code"];
$name = $row["name"];

if ($count % $cols == 0)
 {
	if ($c > 0) 
	{
		echo '</tr>';
	}
	echo '<tr>';
}
echo("<td width=150 align=center>");
	echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
	echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");
echo("</td>");

//increment the counter
$count++;
}

 

Link to comment
Share on other sites

Hi, does anyone have an idea of what should I try now?

 

this is my final updated code:

// define the number of columns per row
$cols = 5;
// start the counter
$counter = 0;										
while ($row = mysql_fetch_array($retd)) 
{ 
$code = $row["code"];
$name = $row["name"];

if ($counter % $cols == 0)
{
	if ($counter > 0) 
	{
		echo '</tr>';
	}
	echo '<tr>';
}
echo("<td width=150 align=center>");
	echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
	echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");
echo("</td>");

//increment the counter
$counter++;

Link to comment
Share on other sites

Hi,

 

I am still trying to solve this problem, so if there are someone out there that have an idea of how can I solve it, please let me know i really need this.

 

this is my latest version of the code:

// define the number of columns per row
$cols = 5;
// start the counter
$counter = 0;										
while ($row = mysql_fetch_array($retd)) 
{ 
$code = $row["code"];
$name = $row["name"];

if ($counter % $cols == 0)
{
	if ($counter > 0) 
	{
		echo '</tr>';
	}
	echo '<tr>';
}
echo("<td width=150 align=center>");
	echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
	echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");
echo("</td>");

//increment the counter
$counter++;
}

 

This is a link to the problem that I have:

The related items need to be 5 in every line.

 

thanks in advance for your help.

Link to comment
Share on other sites

I find it hard to believe that that is the code on your link; because there's no error with that snippet. It should be creating a new row every 5th iteration but your live example creates one on every iteration, which is not possible given the above snippet.

 

I've tested it again and it works without issue for me. You need to give us the exact code for the link you have provided.

Link to comment
Share on other sites

That code is a nightmare.

 

I suspect your problem is here:

$SQL = " SELECT * FROM list ";
$SQL = $SQL . " WHERE code = '$pieces[$i]' ";
$retd = mysql_db_query($db, $SQL, $cid);

if (!$retd) 
{ 
echo( mysql_error()); 
}
else 
{
// define the number of columns per row
$cols = 5;
// start the counter
$counter = 0;										
while ($row = mysql_fetch_array($retd)) 
{ 
	$code = $row["code"];
	$name = $row["name"];

	/*if ($counter % $cols == 0)
	{
		if ($counter > 0) 
		{
			echo '</tr>';
		}
		echo '<tr>';
	}*/
	if ($counter % $cols == 0)
	{
		if ($counter > 0) 
		{
			echo '</tr>';
		}
		echo '<tr>';
	}
	echo("<td width=150 align=center>");
		echo ("<a href=../products/info.php?scode=$code><img src=pictures/$code.gif border=0 alt=Item $name</a>");
		echo ("<br><a href=../products/info.php?scode=$code><span class=fs13>$name</span></a>");
	echo("</td>");

	//increment the counter
	$counter++;
}
}

 

I didn't dig too deeply but I would venture a guess that you are only returning one row at a time.

 

You need to clean up this code a lot. Running queries in nested loops like that is a bad, bad thing. You can most likely gather the same data in a single query.

Link to comment
Share on other sites

I thought so that the problem is somewhere there but could not find the problem.

 

About the code I know that it is a nightmare but it was not written by me and I am not that good in coding php, so it is taking me a lot of time to figure what to change and how to change it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.