Jump to content

Recommended Posts

Hello guys,

i am having one table called country and it has to field state, city?

example
table name country

state          city
-----------------
ABC            aaa
ABC            bbb
ABC            ccc
ABC            ddd

BBB            eee
BBB            fff
BBB            ggg ......

like this i am having 100 records

i want to display this in four colomn where each colomn should have 20 rows.

that is

AAA |BBB |CCC |
---- |--- |---- |
aaa  |eee|hhh  |
bbb  |fff  |iiii    |
ccc  |ggg|jjjj  |
....

Please help me guys i need soon.
Link to comment
https://forums.phpfreaks.com/topic/23153-displaying-problemcolomn-wise/
Share on other sites

You need to get your information correct before we start.  For example, 4 rows of 20 is 80, so you don't want to display your remaining 20 records?

Also, can you not provide us with some actual data, rather than ABC BBB CCC etc, which incidently, it also looks like you got wrong.

Regards
Huggie
Table name is india

state                city
----------------------
tamilnadu      chennai
tamilnadu      tricy
tamilnadu      madurai
tamilnadu      combatore
tamilnadu      salam
etc.....
karala          eranagulam
karala          munaru
karala          choain
etc..

Oh I see, it can be done, but it's going to take some time to get right... I have a script that does something similar, I can post the code for you tomorrow when I get home.

See here: http://www.dizzie.co.uk/php/pages2.php

Regards
Huggie
Hi

I see HuggieBear is showing you a way to do it...

Below is a rough (untested - just type as i go) method that would pretty much do it in table format - with each state left to right as the table column headers, and then the cities listed below it...

you may find it useful

cheers,
tdw

[code]<?php

mysql_connect("localhost", $usr, $pass);
mysql_select_db($db);

echo "<table><tr>"; // start the html table;

// the states: you can select this from db using  group by, but if theres only 4 then just hardcode it
$states = array("tamilnadu, karala, 3rdstate, 4thstate");
$statesarrays = array();
foreach ($states as $state)
{      // init and set each value blank $nbsp; for html output when there isnt a field
        for ($i=0;$i<20;$i++) { $statesarrays[$state][$i] = "&nbsp;"; }

        // add the state name as a heading in the table (started on line 6)
        echo "<td>$state</td>";

        // select all the cities matching the state, loop through and overwrite &nbsp; with the city
        $result = mysql_query("SELECT city FROM india WHERE state = '$state';");
        while ($row = mysql_fetch_row($result))
        { $statesarrays[$state][$i] = $row[0]; }
}

echo "</tr>"; // end ther tables header row

for ($i=0;$i<20;$i++) // loop up to twenty (max cities)
{
        echo "<tr>"; // start a table row
        foreach ($states as $state)
        {
                echo "<td>". $statesarrays[$state][$i] . "</td>"; // echo the city name in a cell
        }
        echo "</tr>"; // end the table row
}
echo "</table>"; // end the table

?>
[/code]
I was going to post something like that, but the problem is that if tamilnadu has one city and karala has five, you end up with something like this...

[table]
[tr][td][b]tamilnadu[/b][/td][td][b]karala[/b][/td][/tr]
[tr][td]city1[/td][td]city1[/td][/tr]
[tr][td] [/td][td]city2[/td][/tr]
[tr][td] [/td][td]city3[/td][/tr]
[tr][td] [/td][td]city4[/td][/tr]
[tr][td] [/td][td]city5[/td][/tr]
[/table]

The example provided the following format:

[table]
[tr][td][b]tamilnadu[/b][/td][td]city3[/td][/tr]
[tr][td]city1[/td][td]city4[/td][/tr]
[tr][td] [/td][td]city5[/td][/tr]
[tr][td][b]Karala[/b][/td][td] [/td][/tr]
[tr][td]city1[/td][td][b]State3[/b][/td][/tr]
[tr][td]city2[/td][td]city1[/td][/tr]
[/table]

Regards
Huggie
here the problem is there may be 10 cities in one state or 15 or 5 etc.. we cannot guess it so

i want

tamilnadu    state3
--------      ------
city1          city1
city2          city2
.              state4
.              ------ 
.              city1
.              city2
city10      .
              ..
karala      .
------      ;
city1        city16
city2
.
.
.
city10

like this it should want to come
yip... that is the problem (if the numbers of cities are very uneven)...
i'm a man for funtionallity not looks (we'll just pretend that the code i wrote worked  :) )

What HuggieBear were saying earlier was cool - but i see he said he'll post it tomorrow :(

I'll have a play around just now and see if i can come up with a good way of doing it...

[quote author=thedarkwinter link=topic=110667.msg447926#msg447926 date=1160135399]
What HuggieBear were saying earlier was cool - but i see he said he'll post it tomorrow :(
[/quote]

Can't access my box at home from my desk at work :(

I'll try to post it early tomorrwo morning for you.

Huggie
i give the table below the table name is city1 not only this values it may come countless... records i want to display it only 20 rows and remaining thing should come in next colomn.


INSERT INTO city1 VALUES (1, 'tamilnadu', '35005');
INSERT INTO city1 VALUES (1, 'tamilnadu', '35007');
INSERT INTO city1 VALUES (1, 'tamilnadu', '35950');
INSERT INTO city1 VALUES (1, 'tamilnadu', '35010');
INSERT INTO city1 VALUES (1, 'tamilnadu', '35952');
INSERT INTO city1 VALUES (1, 'tamilnadu', '36420');
INSERT INTO city1 VALUES (1, 'tamilnadu', '36201');
INSERT INTO city1 VALUES (1, 'Karala', '35016');
INSERT INTO city1 VALUES (1, 'Karala', '35611');
INSERT INTO city1 VALUES (1, 'Karala', '36502');
INSERT INTO city1 VALUES (1, 'Karala', '35954');
INSERT INTO city1 VALUES (1, 'Karala', '36830');
INSERT INTO city1 VALUES (1, 'Karala', '36507');
INSERT INTO city1 VALUES (1, 'jammu', '35020');
INSERT INTO city1 VALUES (1, 'jammu', '35204');
INSERT INTO city1 VALUES (1, 'jammu', '35031');
INSERT INTO city1 VALUES (1, 'jammu', '35956');
INSERT INTO city1 VALUES (1, 'jammu', '36426');
INSERT INTO city1 VALUES (1, 'jammu', '35040');
INSERT INTO city1 VALUES (1, 'Centre', '\n35960');
INSERT INTO city1 VALUES (1, 'jammu', '35044');
INSERT INTO city1 VALUES (1, 'jammu', '35045');
INSERT INTO city1 VALUES (1, 'delhi', '35051');
INSERT INTO city1 VALUES (1, 'delhi', '35453');
INSERT INTO city1 VALUES (1, 'delhi', '35962');
INSERT INTO city1 VALUES (1, 'delhi', '35055');
INSERT INTO city1 VALUES (1, 'delhi', '36853');
INSERT INTO city1 VALUES (1, 'delhi', '36322');
INSERT INTO city1 VALUES (1, 'delhi', '36526');
OK, are you going to paginate the results?

So if you have 4 columns of 20 rows, that's a maximum of 80 table cells, but there's 85 rows in your database, what do you want to do with the other 5 results.

What are each of the columns in your table?  Can I also make modifications to the database table structure or not?

Huggie
okay i'm going to leave this to Huggie (i'm supposed to be working too)... but here is something you can look at if you like:

when it hits 80 though it will make a 5th column though so it will need work.



[code]<?php

mysql_connect("localhost", "root", "2cool4u");
mysql_select_db("dddom");

// the states: you can select this from db using  group by, but if theres only 4 then just hardcode it
$states = array("tamilnadu", "karala", "jammu", "centre", "delhi");
$statesarrays = array();
foreach ($states as $state)
{
        $statesarrays[$state] = array();

        // select all the cities(anumber?) matching the state
        $result = mysql_query("SELECT anumber FROM city1 WHERE state = '$state';");
        $i =0;
        while ($row = mysql_fetch_row($result))
        { $statesarrays[$state][$i++] = $row[0]; $total++; }
}

echo "<table border='1'><tr valign='top'>\n  <td>";

$i=0;
foreach ($states as $state)
{
        if ($i >= 19) { $i=0; echo "  </td><br>\n"; }
        echo "  <b>$state<br></b>\n";
        $i++;
        foreach ($statesarrays[$state] as $city)
        {
                echo "    $city<br>\n";
                $i++;
                if ($i == 20) { $i=0; echo "  </td><td>\n"; }
        }
        echo "<br>";

}
echo "  </td></tr>\n</table>\n";
?>
[/code]

outputs
[code]
<html>
<table border='1'><tr valign='top'>
  <td>  <b>tamilnadu<br></b>
    35005<br>
    35007<br>
    35950<br>
    35010<br>

    35952<br>
    36420<br>
    36201<br>
<br>  <b>karala<br></b>
    35016<br>
    35611<br>

    36502<br>
    35954<br>
    36830<br>
    36507<br>
<br>  <b>jammu<br></b>
    35020<br>

    35204<br>
    35031<br>
    35956<br>
  </td><td>
    36426<br>
    35040<br>

    35044<br>
    35045<br>
<br>  <b>centre<br></b>
    35960<br>
<br>  <b>delhi<br></b>
    35051<br>

    35453<br>
    35962<br>
    35055<br>
    36853<br>
    36322<br>
    36526<br>

<br>  </td></tr>
</table>
[/code]
We're using a similar method I think, except I'm getting the states from the database, and I'm putting each city in its own table cell as opposed to using [b]<[/b][b]br>[/b] tags.  But that's for formatting preference.

I'm not sure how I'm going to get them to paginate accurately, but I'm working on it.

Huggie
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.