Trying to display mysql data into table (3 wide) in php

Hey,

I am trying to display my mysql data into a table that is 3 wide in php.
here is the php I have been using:
[php]<?php

// set database server access variables:
$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “product_index”;

// open connection
$connection = mysql_connect($host, $user, $pass) or die (“Sorry error!: (unable to connect)”);

// select database
mysql_select_db($db) or die (“Sorry error!: (unable to select database)”);

// create query
$query = “SELECT * FROM products WHERE type = ‘bracelets’”;

// execute query
$result = mysql_query($query) or die ("Sorry error!: in query: $query. ".mysql_error());

define(‘COLS’, 3); // number of columns
$col = 0; // number of the last column filled

echo ‘

’;
echo ‘’; // start first row

while ($rows = mysql_fetch_array($result))
{ $col++;

echo ‘

’;

if ($col == COLS) // last time filled the last column
{ echo ‘

’; // start new row – “linebreak” sequence
$col = 0; // so now we’re filling first column
} else $col++; // increment otherwise

}

echo ‘

’; // end last row
echo “
’;
echo ‘
’, $rows[2], ‘

’, $rows[0],’
’, $rows[4],‘
More info
’, ‘
”;

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>[/php]
The issue is: it looks like this:


| item1 | item2 |


| item3 | item4 |


| item5 | item6 |


Why isn’t it 3 wide?

Thanks in advance.

Looks like there is logical mistake - you’re incrementing your $col variable twice in one loop cycle:
[php]
while ($rows = mysql_fetch_array($result))
{ $col++;

// …

if ($col == COLS) // last time filled the last column
{ echo ‘

’; // start new row – “linebreak” sequence
$col = 0; // so now we’re filling first column
} else $col++; // increment otherwise

}
[/php]

Try to initialize $col = 1; (outside the loop, instead of zero what you have now). And then remove top instance of $col++. Also, don’t forget to change this line within your loop (replace 0 with 1):
[php]$col = 1; // so now we’re filling first column[/php]

Hey, Thanks so much for the reply phphelp.
This is the code I am now using:
[php]<?php

// set database server access variables:
$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “product_index”;

// open connection
$connection = mysql_connect($host, $user, $pass) or die (“Sorry error!: (unable to connect)”);

// select database
mysql_select_db($db) or die (“Sorry error!: (unable to select database)”);

// create query
$query = “SELECT * FROM products WHERE type = ‘bracelets’”;

// execute query
$result = mysql_query($query) or die ("Sorry error!: in query: $query. ".mysql_error());

define(‘COLS’, 3); // number of columns
$col = 1; // number of the last column filled

echo ‘

’;
echo ‘’; // start first row

while ($rows = mysql_fetch_array($result))
{

echo ‘

’;

if ($col == COLS) // last time filled the last column
{ echo ‘

’; // start new row – “linebreak” sequence
$col = 1; // so now we’re filling first column
} else $col++; // increment otherwise

}

echo ‘

’; // end last row
echo “
’;
echo ‘
’, $rows[2], ‘

’, $rows[0],’
’, $rows[4],‘
More info
’, ‘
”;

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>[/php]
This now has the right amount per row (3).
The only issue is now: there is now a extra [php]

[/php] at the end.
What did I do wrong?

Once again, Thanks a lot!

Sponsor our Newsletter | Privacy Policy | Terms of Service