Forum Moderators: coopster

Message Too Old, No Replies

PHP - dynamic 3 column issue

         

tecun

4:38 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Hello everyone, I'm a fresh newbie with database background and long forgotten c programming skills, discovering php, however I'm not able to display three columns on my dynamic script can someone help me out. Here is my code...
<?php require("includes/connection2.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PARTNERS PAGE</title>
</head>

<body>
<?php
$query = "SELECT link FROM testlogo";
$results = mysql_query($query)
or die(mysql_error());

echo '<table>';
$cols=3;
$numtd = 3;

while ($row = mysql_fetch_row($results)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>$value</td>";
echo "</tr>";
}
}
echo "</tr>";
echo "</table>\n";
?>
</body>
</html>

Knucklehead00

4:51 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Where is your database connection string? Do you have that in there and just left it out for security purposes?

Also if you are trying to create 3 columns and not 3 rows, your code should look like this:

while ($row = mysql_fetch_row($results)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>\n";

tecun

4:57 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



my database connection string is in another php script <?php require("includes/connection2.php"); ?>
thank you knucklehead I will try that

Knucklehead00

5:02 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Not a problem.

Best of luck mate.

tecun

5:04 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Hi Knucle I tried that but I'm still getting only one column

Knucklehead00

5:34 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Are you looking for 3 rows or 3 columns?

Columns go up/down, rows go left/right.

If you are looking for 3 columns, then it is possible that your query is not returning the proper data.

Lets do some troubleshooting here. Insert the following code after "$numtd = 3;":

print_r(mysql_fetch_row($results));

Let me know what you get back from the print_r. Just paste the results into here.

tecun

5:47 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



I'm actually looking for my code to return only 3 columns
after executing the code you provided me this is the output

Array ( [0] => www.mysite.com ) www.canada.com
www.hotmail.com
www.yahoo.com

shouldn't I use the $col=3; variable somewhere in my code after?

tecun

7:27 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



I figured it out, even thou the code had to change a little bit, for those fresh newbies like myself that are interested on the code here it is

<?php
$sql="select link from testlogo";
if(mysql_query($sql))
{
$check=mysql_query($sql);
$no=mysql_num_rows($check);
}

echo"<table>";
echo "<table border=\"1\">\n";
for($i=0; $i<$no; $i++){
if($i%3==0){echo"<tr><td>";}
else{echo "<td>";
}
print mysql_result($check,0+$i,"link");
if($i%3==0){
echo "</td>";}
else
{
echo "</td>";}
}
echo "</tr>";
echo "</table>\n";
?>
</body>
</html>

mooger35

12:07 am on Oct 24, 2008 (gmt 0)

10+ Year Member



Just as an add on here... what I like to do is put the contents in an array first. Then count the results and add the necessary &nbsp;'s.

a simple example using 3 columns:

$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
$cols = 3;
$count = count($array);

if($count%$cols > 0){
for($i=0;$i<($cols-$count%$cols);$i++){
$array[] = '&nbsp;';
}
}
echo "<table border=\"1\">\r\n";

foreach($array as $key => $td){
if($key%$cols == 0) echo "<tr>\r\n";
echo "<td>$td</td>\r\n";
if($key%$cols == ($cols - 1)) echo "</tr>\r\n";
}
echo "</table>";

will return:

<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

tecun

4:45 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



I don't know if is just me, but I tried your code mooger and I could not get it to execute, but anyway your code looks more appropiate for this type of task "makes more sense". thank you

mooger35

5:23 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



What error did it kick out? My guess is that the problem comes from you putting the data into the array.

tecun

5:43 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



you might be right as I'm not using the same method that you presented, I did it this way.
$sql="select link from testlogo";
if(mysql_query($sql))
{
$check=mysql_query($sql);
$no=mysql_num_rows($check);
}

$array = array($no);
$cols = 3;
$count = count($array);

you see i want the page to be dynamic and the array is static, however I'm not 100% sure if you can actually call a variable from within an array, so far what I have been getting is a frame with 3 boxes and one with the number of records to be display.

8

mooger35

6:09 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



I see...

try this:

$sql="select link from testlogo";
$array = array(); // initialize array
if(mysql_query($sql))
{
$check=mysql_query($sql);
while($row=mysql_fetch_row($check)){
$array[] = $row[0];
}
}
$cols = 3;
$count = count($array);
if($count%$cols > 0){
for($i=0;$i<($cols-$count%$cols);$i++){
$array[] = '&nbsp;';
}
}
echo "<table border=\"1\">\r\n";

foreach($array as $key => $td){
if($key%$cols == 0) echo "<tr>\r\n";
echo "<td>$td</td>\r\n";
if($key%$cols == ($cols - 1)) echo "</tr>\r\n";
}
echo "</table>";

tecun

7:29 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



thanks, greatly appreciated is working like some sort of white magic... hehe

like my great, great grandfather from Japan would say... YOU GOOD