Jump to content

Recommended Posts

I'm testing something and using the code provided by cooldude832 (modified), but I just can't make it work for me.

 

Basically what I'm trying to do is pull rows from a db (sorted), and place them in columns like:

 

1  6  11

2  7  12

3  8  13

4  9  14

5  10  15

 

How do I do that? Please help this is important (and frustrating  ;D)

 

Thanks,

Sam.

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449263
Share on other sites

<?php
$cat = $_GET['cat'];

include('../connectdb.php');

$sql = mysql_query("SELECT * FROM ffcats WHERE pid='$cat' ORDER BY id ASC");


if ($cat == "")
{
include('nonechosen.php');
}
else
{
echo "<table><tr><td>";
while($row = mysql_fetch_array($sql))
{
$no = 
$name = $row['name'];
$a = $row['id'];

echo "<a href=?cat=" . $a . ">" . $name . "</a><br>";
}
echo "</td></tr></table><br />";
}

?>

 

At the moment I get:

1

2

3

4

5

6

7

8

etc.

 

How can I get what I previously stated?

 

Thanks heaps!

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449270
Share on other sites

Ok,

 

Database db1

Table structure for table ffcats

Field	Type	Null	Default
id	int(4)	Yes	NULL
pid	int(4)	Yes	
name	varchar(25)	Yes

Dumping data for table ffcats

92	69	Papakura District
91	69	North Shore City
90	69	Manukau City
89	69	Franklin
88	69	Auckland City
213	83	Pakaraka
212	83	Paihia
85	68	Whangarei
84	68	Kaipara
83	68	Far North
82	0	Southland

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449274
Share on other sites

Try changing

while($row = mysql_fetch_array($sql))
{
$no = 
$name = $row['name'];
$a = $row['id'];

echo "<a href=?cat=" . $a . ">" . $name . "</a><br>";
}
echo "</td></tr></table><br />";
}

to this:

$count = 0;
while($row = mysql_fetch_array($sql))
{
$name[$count] = $row['name'];
$a[$count] = $row['id'];
$count++;
}
for($num==0;$num<$count;$num++) {
$count2 = 0;
while(($count2<3)and($a[$count]!=null)) {
echo "<a href=?cat=" . $a[$count] . ">" . $name[$count] . "</a> ";
$count++;
$count2++;
}
echo "<br>";
}
echo "</td></tr></table><br />";
}

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449284
Share on other sites

Haha, sorry, my mistake

$count = 0;
while($row = mysql_fetch_array($sql))
{
$name[$count] = $row['name'];
$a[$count] = $row['id'];
$count++;
}
for($num==0;$num<$count;$num++) {
$count2 = 0;
while(($count2<3)and($a[$num]!=null)) {
echo "<a href=?cat=" . $a[$num] . ">" . $name[$num] . "</a> ";
$num++;
$count2++;
}
echo "<br>";
}
echo "</td></tr></table><br />";
}

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449289
Share on other sites

Nice try... still doesn't work though.

 

Now, I get:

 

K W

 

Instead of:

 

Far North

Kaipara

Whangarei

 

(so the first one is missing and they aren't going in columns.

 

Should be:

 

1  6

2  7

3  etc

4

5

 

not:

 

1  2  3

4  5  6

etc

 

Any ideas?

 

Thanks heaps! Big help!

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449294
Share on other sites

A little manipulation should fix it I think O.o

 

$count = 0;
while($row = mysql_fetch_array($sql))
{
$namex[$count] = $row['name'];
$a[$count] = $row['id'];
$count++;
}
for($num==0;$num<$count;$num++) {
$count2 = 0;
while(($count2<3)and($a[$num]!=null)) {
echo "<a href=?cat=" . $a[$num+$count2] . ">" . $namex[$num+$count2] . "</a> ";
$count2++;
}
echo "<br>";
}
echo "</td></tr></table><br />";
}

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449298
Share on other sites

You're going to need to read all your db rows into an array before you start trying to draw your table because of the way you're displaying your data.

 

This example is pretty basic and inflexible but hopefully it will point you in the right direction.

<?php

$rows[] = "row 1";
$rows[] = "row 2";
$rows[] = "row 3";
$rows[] = "row 4";
$rows[] = "row 5";
$rows[] = "row 6";
$rows[] = "row 7";
$rows[] = "row 8";
$rows[] = "row 9";
$rows[] = "row 10";
$rows[] = "row 11";
$rows[] = "row 12";
$rows[] = "row 13";
$rows[] = "row 14";
$rows[] = "row 15";

$numColumns = 3;
$rowCount = count($rows);

$numRows = (int) $rowCount /$numColumns;

?>
<table>
    
    <tr>
        <th>col 1</th>
        <th>col 2</th>
        <th>col 3</th>
    </tr>
    
    
    <?php
        $j = 0;
        for ($i=0; $i<$numRows ; $i++) :
    ?>
    
    <tr>
        <td> <?php echo $rows[$j] ?> </td>
        <td> <?php echo $rows[$j+5] ?> </td>
        <td> <?php echo $rows[$j+10] ?> </td>
    </tr>
    <?php
        $j++ ;
        endfor;
    ?>
   
</table>
    

Link to comment
https://forums.phpfreaks.com/topic/87823-1-issue/#findComment-449301
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.