PDA

View Full Version : Help! pulling my hair out ...


Sean S
02-27-2006, 04:21 AM
Today I found out that I'm still a noob when it comes down to php :ermm: and would like to ask for help here once again, and I hope someone will be able to do so.

Goal:
what I'm trying to do is to display some data from mysql in a php page in a table containing 3 columns.

Problem:
I can't get the code to work and display the data in columns. I already know how to display the data in rows using the loop, but what I don't know is how I would go ahead and display the data in columns.

So instead of having something like this,

data 1
data 2
data 3

I want to have something like this

data 1 data 2 data 3

The code that I'm working with is the following, and it just doesn't work for me,


<?php
$username="xxxxx";
$password="xxxxx";
$database="xxxxx";
$table="testing";
$number="20";

//number of columns of the table generated
$column = "3";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$i = 1;
$e = 2;
$col_value = $column;

$getbikes = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $number "); //Retrieves all of the Data from table
while($row = mysql_fetch_assoc($getbikes))
{
echo "<table><tr>";

echo "<td><a href='http://baldjf.com' class=''>$row[bikename]</a>";

echo "<a href='http://www.static-design.com'>";

echo "<img border='0' height='65'width='75' src='$filepath'>";

echo "<span>";

echo "<br>something here<br>";

echo "</a><br></font><br>";

echo "</span>";

if ($i == '$pp_column') {

echo "</td></tr><tr>";

$column = $col_value*$e;
$e++;
}

else {
echo "</td>";
}

$i++;
}

echo "</tr></table>";
?>


thanks in advance :nervous: ,
Sean

SuperFly
02-27-2006, 06:45 AM
while($row = mysql_fetch_assoc($getbikes))
{
echo "<table><tr>";
Every time the loop runs, a new table and a new row are created. Try changing this:
$getbikes = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $number "); //Retrieves all of the Data from table
while($row = mysql_fetch_assoc($getbikes))
{
echo "<table><tr>";

echo "<td><a href='http://baldjf.com' class=''>$row[bikename]</a>";

echo "<a href='http://www.static-design.com'>";

echo "<img border='0' height='65'width='75' src='$filepath'>";

echo "<span>";

echo "<br>something here<br>";

echo "</a><br></font><br>";

echo "</span>";

if ($i == '$pp_column') {

echo "</td></tr><tr>";

$column = $col_value*$e;
$e++;
}

else {
echo "</td>";
}

$i++;
}
to this:
$getbikes = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $number "); //Retrieves all of the Data from table
echo "<table><tr>";
while($row = mysql_fetch_assoc($getbikes))
{
echo "<td><a href='http://baldjf.com' class=''>$row[bikename]</a>";

echo "<a href='http://www.static-design.com'>";

echo "<img border='0' height='65'width='75' src='$filepath'>";

echo "<span>";

echo "<br>something here<br>";

echo "</a><br></font><br>";

echo "</span>";

echo "</td>";

$i++;
}
I removed the $i == '$pp_column' conditional, cause I don't know what you want to do with it, or what $pp_column is.
Also, some HTML, you close a font tag, but you don't open it, and your <span> and <a> tags go like this: <a><span></a></span> (a.k.a., they aren't nested properly), which may screw them up. You might want to fix these.
I hope Brandon doesn't mind me posting from his account -___-

Princeton
02-27-2006, 12:08 PM
if all you want is a 3 column (will never change)

echo <table>
while (loop)
{
echo <tr><td>col 1</td><td>col 2</td><td>col 3</td></tr>
}
echo </table>

Sean S
02-27-2006, 06:24 PM
if all you want is a 3 column (will never change)

echo <table>
while (loop)
{
echo <tr><td>col 1</td><td>col 2</td><td>col 3</td></tr>
}
echo </table>

wow thanks, those help me get a bit closer :). Princeton the problem is that I might change the value sometimes, and I want to control the amount of columns by just changing a value of a variable, in this case the $column.

Using your code superfly also did help, I changed it around a bit and got this. However the problem with this one is that the number of columns only applies to the first row,

so I end up having data like this,

data 1 data 2 data 3
data 4 data 5 data 6 dtat 7 data 8 ....

I just can't understand why the value 3 for columns only applies to the first row now :(, you guys know why this happens? this is the final code that I have,


<?php
$username="xxxxx";
$password="xxxxx";
$database="xxxxx";
$table="testing";
$number="8";

$i = "1";
$e = "2";
$col_value = "$column";

//number of columns of the table generated
$column = 3;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$getbikes = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $number"); //Retrieves all of the Data from table
echo "<table><tr>";
while($row = mysql_fetch_assoc($getbikes))
{
echo "<td><a href='http://baldjf.com' class=''>$row[bikename]</a>";

echo "<a href='http://www.static-design.com'>";

echo "<img border='0' height='65'width='75' src='$filepath'>";

echo "<span>";

echo "<br>something here<br>";

echo "</a><br></font><br>";

echo "</span>";

if ($i == "$column") {

echo "</td></tr><tr>";

$column = $col_value*$e;

$e++;

}

else {

echo "</td>";

}

$i++;

}

echo "</table>";

?>

Aesma Deva
02-28-2006, 11:19 AM
I'm the same guy who posted as Superfly before. Anyway...
$col_value = "$column";

//number of columns of the table generated
$column = 3;
The first line assigns the value "$column" to $col_value, not what's inside of $column. Also, you don't need a $col_value variable, since it's value doesn't change from 3, unless if you plan changing it later. Soooo, this should work as you want it:

<?php
$username="xxxxx";
$password="xxxxx";
$database="xxxxx";
$table="testing";
$number="8";

$i = 1;
$e = 2;

//number of columns of the table generated
$column = 3;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$getbikes = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $number"); //Retrieves all of the Data from table
echo "<table><tr>";
while($row = mysql_fetch_assoc($getbikes))
{
echo "<td><a href='http://baldjf.com' class=''>$row[bikename]</a>";

echo "<a href='http://www.static-design.com'>";

echo "<img border='0' height='65'width='75' src='$filepath'>";

echo "<span>";

echo "<br>something here<br>";

echo "</a><br></font><br>";

echo "</span>";
echo "</td>";

if ($i == $column) {

echo "</tr><tr>";

$column = 3*$e;

$e++;

}

$i++;

}

echo "</table>";

?>

I also removed some more stuff that I don't think you'll need.

Sean S
03-01-2006, 04:38 AM
wow thank you so much Aesma, it worked great! :D