Jump to content

Archived

This topic is now archived and is closed to further replies.

brown2005

help with layout of multiple results in table... *SOLVED*

Recommended Posts

hi....

i want to take some results from the database say

apples
attack
bob
brick
boys

etc, etc and display them like


A

apples, attack

B

bob, boys, brick

and so on based on the first letter and then in order...

any ideas please?

Share this post


Link to post
Share on other sites
[code]$query = mysql_query("SELECT LEFT(field, 1)  as 'first', field FROM table ORDER by first ASC");
$letter='';
while ($row=mysql_fetch_array($query)) {
if ($row['first'] !== $letter) {
  $letter=$row['first'];
  echo "<B>$first</b><br>";
}
if ($row['first'] == $letter) {
  echo "$row[field]<br>";
}
}[/code]


i beleave that should* work but hasn't been tested

Share this post


Link to post
Share on other sites
[quote]
[code]$query = mysql_query("SLEECT LEFT(field, 1)  as 'first', field FROM table ORDER by first ASC");[/code]
[/quote]

Typo in SELECT...

Regards
Huggie

Share this post


Link to post
Share on other sites
$query = mysql_query("SELECT LEFT(topic_a, 1)  as 'first', topic_a FROM A ORDER by first ASC");

$letter='';

while ($row=mysql_fetch_array($query))
{

if ($row['first'] !== $letter)
{
 
  $letter=$row['first'];
 
echo "<B>$first</b><br>";

}

if ($row['first'] == $letter)
{
 
  echo "$row[topic_a]<br>";
 
}

}

i have used the above and get http://www.fantasy-comps.com/a.php

but hasnt actually done what I wanted... i wanted the letter above it and then in the same row, and if fills up the row the next row....

i.e.

[b]A[/b]

a, ab, ac, ad, ae, af
ag, ah, ai, aj

[b]B[/b]

b
b, ba, bb, bc


Share this post


Link to post
Share on other sites
Do you mean
[code]
<?php

$query = mysql_query("SELECT LEFT(topic_a, 1)  as 'first', topic_a FROM A ORDER by topic_a");

$letter='';

while (list($first,$topic)=mysql_fetch_row($query))
{

  if ($first !== $letter)
  {

    $letter = $first;
    $count = 0;
    echo "<h3>" . strtoupper($first)  . "</h3>";
   
  }

  if (!$count) echo "$letter,";
  echo " $topic";
  $count++;
  if ($count%5 == 4) echo '<br>';
}

?>[/code]

Share this post


Link to post
Share on other sites
Hi, That is looking more like I want, but it needs a few adjustments, view [url=http://www.links-bid.com/1.php to]http://www.links-bid.com/1.php to[/url] and you will see.

I want the letter moved from the start, not the bold one the one say like a, and then it should have a , after every topic.....

Thanks for all your help

Share this post


Link to post
Share on other sites
[code]
<?php

$query = mysql_query("SELECT LEFT(topic_a, 1)  as 'first', topic_a FROM A ORDER by topic_a");

$letter='';

while (list($first,$topic)=mysql_fetch_row($query))
{

  if ($first !== $letter)
  {

    $letter = $first;
    $count = 0;
    echo "<h3>" . strtoupper($first)  . "</h3>";
   
  }

  echo ($count) ? "," : "";
  if ($count && $count%5 == 0) echo '<br>';
  echo ($count) ? " $topic" : "$topic";
  $count++;
}

?>[/code]

EDIT: In your reply #4 you said you wanted letter on row
[quote]i wanted the letter above it and then in the same row, [/quote]

Share this post


Link to post
Share on other sites
    echo"<table width='500' border='1' cellpadding='0' cellspacing='0'>";
    echo" <tr>
          <td>";
     
  echo ($count) ? "," : "";
  if ($count && $count%10 == 0) echo '<br>';
    echo ($count) ? " $topics_topic" : "$topics_topic";

    $count++;

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

Hi i have tried putting the results in a table so they can be easily aligned and stuff, but it comes out all wrong....

check www.allinthissite.co.uk for what happens...

is this the best way to do it in a table or would i use a div or something else?

Share this post


Link to post
Share on other sites
Can you provide all of the code for this section, including the while loop...

Also use code tags as it's easier to understand... [b][[/b][b]code][/b]My code goes here[b][[/b][b]/code][/b]

Regards
Huggie

Share this post


Link to post
Share on other sites
or use css

[code]<STYLE content="text/css">
DIV.item {
    width: 100px;
    height: 20px;
    float: left;
    padding: 3px;
    text-align: left;
    font-size: 0.8em;
    font-weight: 300;
    border:  1px solid #EEE;
}
DIV.head {
    width: 500px;
    height: 40px;
    padding: 13px 3px ;
    text-align: left;
    font-size: 0.9em;
    font-weight: 600;
    margin-top: 15px;
}
BR {
    clear: both;
}
</STYLE>
<?php
include 'db2.php';
define ("COLS", 5);

$query = mysql_query("SELECT LEFT(topic_a, 1)  as 'first', topic_a FROM A ORDER by topic_a");

$letter='';

while (list($first,$topic)=mysql_fetch_row($query))
{

  if ($first !== $letter)
  {

        $letter = $first;
        $count = 0;
        echo "<br/><DIV class='head'>" . strtoupper($first)  . "</DIV>";
   
  }

  if ($count && $count % COLS == 0) echo '<br>';
  echo  "<DIV class='item'>$topic</DIV>";
  $count++;
}

?>[/code]

Share this post


Link to post
Share on other sites

×

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.