Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display one field in multiple table columns

Status
Not open for further replies.

kawnz

Technical User
Jan 30, 2003
67
US
I have a text based dbase, so any MYSQL-specific code won't help me.

The database contains meeting information (venue, day, time, address, city, state, zip, etc. )

When I choose a state, I want it to display all the cities in that state.

Right now I have it at
[city1]
[city2]
[city3]
[city4]
[city5]
etc.

What I want to display:

[city1][city2][city3]
[city4][city5][city6]
etc.



Question One: how do I do that?

Question Two: how do I then complete the table, when the bottom row has, say only one [city]?
 
do this:

Code:
<table>
  <tr>
    <td>
    //output cities in first row
    </td>
  </tr>
  <tr>
    <td>
    //output cities in second row
    </td>
  </tr>
  //continue this until all cities output, then output
</table>

i have some code for a page im writing if you'd like to see it, its very similar to what your doing. it'd take me a minute to edit it for posting here so im not going to bother if you dont need it. what we see depends mainly on what we're looking for.
--unknown
 
i apologize kawnz, i didnt really think before posting. i think youll need to have a table for each row within each cell of the outer table in order to display in a row like that. so in the code above where it says to output cities, it should be in its own table. what we see depends mainly on what we're looking for.
--unknown
 
I know how tables work
But I want to know how to use those tables with calling up info from the dbase.

I am using the for statement to generate the cities, but I want to know how to then display those cities in a table form, and not just one under the other.

I am guessing I need to use another for statement, but not sure how to go about it or where to put it.

right now I pretty much have
<table>
for
<tr><td>$city</td></tr>
next
</table>

how do it get it to do
<tr><td>$city</td><td>$city</td><td>$city</td></tr>
 
hey now it's a php question, or at least a general program question

in psuedo-code
$counter = 0

for () {
if counter = 0 {
<tr>
}
<td>$city</td>
$counter++
if $counter=2 {
</tr>
$counter=0
}
}

if $counter != 2 {
</tr>
}
</table>

That's ugly but effective.

-Rob
 
sleipnir:
since he is loading dynamic info from the db, i assumed he needed help with the php code that generates the tables.

kawnz
assuming that is your dilemma, this problem isnt any harder than making a table in html. say you loop through some db results, output the cities. add <td> and </td> to the output to insert that city in a table, and add <tr> and </tr> at the beginning and end of the loop to make a row ie:

Code:
print &quot;<table><tr>&quot;; //starts first row
for(...)
{
  print &quot;<td>&quot;.$city.&quot;</td>&quot;;
  if($num_cities_in_row > $max_cities_in_row)
    print &quot;</tr><tr>&quot;;  //starts a new row
}
print &quot;</tr></table>&quot;; //end last row

and to second sleipnir's comment, if what your looking for ISNT php code or help, then pls post in the appropriate forum. what we see depends mainly on what we're looking for.
--unknown
 
bedrock

You forgot to increment your counter ($num_cities_in_row), and you leave the possibility of having an extra empty row at the end of your table if total number of cities mod $max_cities = 0.

Not that that's a big issue, but if your layout is very specific it might be frustrating.

-Rob
 
skiflier

very true. should have thought about that for a min more before posting. guess i was trying to give him the general idea and forgot about some details. sorry 'bout that. what we see depends mainly on what we're looking for.
--unknown
 
this is what I have so far


$my_isues = select_pb ($conn, &quot;state&quot;,&quot;x == 'PA'&quot;);
$amountofrows = count_pb ($my_isues);

$counter = 0;

echo &quot;<table>&quot;;
for ($i=0;$i< $amountofrows;$i++)
{
$id = $my_isues[&quot;id&quot;][$i];
$state = $my_isues[&quot;state&quot;][$i];
$county = $my_isues[&quot;county&quot;][$i];

for () {
if counter = 0 {
<tr>
}
<td>$county</td>
$counter++
if $counter=2 {
</tr>
$counter=0
}
}

if $counter != 2 {
</tr>
}
echo &quot;</table>&quot;



ir gives me this error
Parse error: parse error, unexpected ')', expecting ';'
 
your first reference to counter needs a $, and you probably want to turn those 2's to 3's.

your second for loop doesn't make sense and should just be removed.

And you need == for comparisons in PHP.

Not to mention your echo's... here, try this.

basically

Code:
$my_isues = select_pb ($conn, &quot;state&quot;,&quot;x == 'PA'&quot;);
$amountofrows = count_pb ($my_isues);

$counter = 0;

echo &quot;<table>&quot;;
for ($i=0;$i< $amountofrows;$i++)
{
  $id = $my_isues[&quot;id&quot;][$i];
  $state = $my_isues[&quot;state&quot;][$i];
  $county = $my_isues[&quot;county&quot;][$i];

  if ($counter == 0) {
     echo &quot;<tr>\n&quot;;
  }
  echo &quot;<td>$county</td>\n&quot;;
  $counter++;
  if ($counter==3) {
    echo &quot;</tr>\n&quot;;
    $counter=0;
  }
}

if ($counter != 2) {
  echo &quot;</tr>&quot;
}
echo &quot;</table>&quot;
 
skiflyer:
Writing someone else's code for him is considered contrary to the purposes of this site. Tek-Tips is not a free technical support system -- it is a site where technical professionals can help one another. The &quot;help&quot; part being specifically sharing understanding, not writing code.

The unfortunate thing is that I noticed some gaps in kawnz's basic understanding of the process. I doubt your writing his project for him helped in that understanding. Want the best answers? Ask the best questions: TANSTAAFL!
 
Thank you very much for your help. You just added 1 to the counter, then reset the counter to 0 once the counter reached 3. That makes a lot of sense. I was trying to do it with another for statement embedded in which was superfluous.

sleipnir:
I know this is not a &quot;we do it for you&quot; site, and didn't expect it to be handed to me, but I do thank skiflyer; seeing how it's supposed to be coded, does actually help to know what I am supposed to be doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top