Displaying results horizontally from mysql database

Heya, i’ve been reading it over internet but lack of my knowledge prevents me from editing this script to echo those products in same row. I read that i could do some sort of loop to make the result appear horizontally instead of vertically but i’d appreciate it, if someone could help me a little :slight_smile:
Lets say i want 2-3 products per row and then it starts a new row. I tried a few things around the internet but i wasn’t able to get the result i wanted. Thanks in advance for anyone, who can help me with this script! ^^

[php] <?php
mysql_connect(“localhost”, “root”, “pword”) or die(mysql_error());
mysql_select_db(“database”) or die(mysql_error());

$result = mysql_query("SELECT id , Code , Product , Quantity , Price , Image FROM products WHERE id >=1 AND id <=14");
while (list($id , $Product , $Code , $Quantity , $Price , $Image ) = mysql_fetch_row($result))

{
echo
"



<p class=“suositut”>
<img src="$Image" border=“0” alt="" align=“left” >
$Code
Code:
$Product

Quantity: $Quantity

Price:<span style=“color:blue”><font size=“h4”>$Price € (alv 0%)
";

}

?>[/php]

It looks like you got the basics down, you just need to adjust your logic a little. I didn’t test the code below, so there can be syntax errors, but basically, what I’m saying is to display 2 per row. You can adjust the math and make it do how many items per row as you like.

[php] <?php
mysql_connect(“localhost”, “root”, “pword”) or die(mysql_error());
mysql_select_db(“database”) or die(mysql_error());

$icount = 1;

$result = mysql_query("SELECT id , Code , Product , Quantity , Price , Image FROM products WHERE id >=1 AND id <=14");

echo “


while (list($id , $Product , $Code , $Quantity , $Price , $Image ) = mysql_fetch_row($result))

{

echo "


<p class=“suositut”>
<img src="$Image" border=“0” alt="" align=“left” >
$Code
Code:
$Product

Quantity: $Quantity

Price:<span style=“color:blue”><font size=“h4”>$Price € (alv 0%)
";

if (($icount % 2) == 0)
{
echo “

”;
};
++$icount;
}
echo “”
?>[/php]

Hey, there was this “T_while” parse error and i added “;” to the end of the first echo “

” and it started working (i got no clue what else to do). Anyway, instead of showing the rows horizontally or vertically, it shows them like :

Product 1
Product 2
Product 3

Any idea of what i’m doing wrong?

Hoodie,

Good Catch on the missing Semi-Colon, try this code instead, I forgot to add the <tr> to open the new row, after closing it.  Based on this example you should get the jist of what I'm trying to do and come up with the logic that will work for your project. 

[php] <?php
mysql_connect(“localhost”, “root”, “pword”) or die(mysql_error());
mysql_select_db(“database”) or die(mysql_error());

$icount = 1;
$ilastloop = 0;

$result = mysql_query(“SELECT id , Code , Product , Quantity , Price , Image FROM products WHERE id >=1 AND id <=14”);

echo “

”;
while (list($id , $Product , $Code , $Quantity , $Price , $Image ) = mysql_fetch_row($result))

{

$ilastloop = 0;

echo "


<p class=“suositut”>
<img src="$Image" border=“0” alt="" align=“left” >
$Code
Code:
$Product

Quantity: $Quantity

Price:<span style=“color:blue”><font size=“h4”>$Price € (alv 0%)
";

if (($icount % 2) == 0)
{
echo “

”;
};
++$icount;

$ilastloop = 1;

}

if ($ilastloop == 1) {
echo “

”;

}

?>[/php]

Hello, i really appreciate the effort you’ve put on the code! ^^ unfortunately, i’m ending up with same results on both codes. Products still appear in the same way i described earlier. :slight_smile: Tho, i’ve found few working solutions around the internet (for horizontal display) but i haven’t been able to mix those codes to my code. They have pretty much the same idea but i’m missing a working result. :confused: anyway, thanks alot for being helpfull! :slight_smile:

Now I’m curious can you right click and view source and send me the results in Private Message. I’m wondering where I went wrong.

Thanks for sharing the source code with me you’re missing the table tags. Use the code below.

[php] <?php
mysql_connect(“localhost”, “root”, “pword”) or die(mysql_error());
mysql_select_db(“database”) or die(mysql_error());

$icount = 1;
$ilastloop = 0;

$result = mysql_query(“SELECT id , Code , Product , Quantity , Price , Image FROM products WHERE id >=1 AND id <=14”);

echo “

”;
while (list($id , $Product , $Code , $Quantity , $Price , $Image ) = mysql_fetch_row($result))

{

$ilastloop = 0;

echo "

";

if (($icount % 2) == 0)
{
echo “

”;
};
++$icount;

$ilastloop = 1;

}

if ($ilastloop == 1) {
echo “

”;

}

echo “


<p class=“suositut”>
<img src="$Image" border=“0” alt="" align=“left” >
$Code
Code:
$Product

Quantity: $Quantity

Price:<span style=“color:blue”><font size=“h4”>$Price € (alv 0%)
”;
?>[/php]

It’s working like a charm! ^^ i’m really grateful to you! I’ve tried to learn enough php to make it work but so far i haven’t been able to. Now i can finally finish what i’ve started and only because of you! :slight_smile: Thank you alot for all the help ^^!

Sponsor our Newsletter | Privacy Policy | Terms of Service