Jump to content


Photo

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


  • Please log in to reply
14 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 20 September 2006 - 10:19 AM

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?

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 20 September 2006 - 11:00 AM

$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>";
 }
}


i beleave that should* work but hasn't been tested
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 11:10 AM

$query = mysql_query("SLEECT LEFT(field, 1)  as 'first', field FROM table ORDER by first ASC");


Typo in SELECT...

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 20 September 2006 - 11:18 AM

cheers huggie changed it :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 20 September 2006 - 02:07 PM

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

A

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

B

b
b, ba, bb, bc




#6 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 21 September 2006 - 06:21 AM

Hi, I was wondering if anyone knew if this is posisble at all?

#7 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 28 September 2006 - 08:14 AM

Hi, I was wondering if there was anyone at all out there that can possibly help with this?

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 September 2006 - 08:58 AM

Do you mean
<?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>'; 
}

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 28 September 2006 - 09:25 AM

Hi, That is looking more like I want, but it needs a few adjustments, view http://www.links-bid.com/1.php to 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

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 September 2006 - 09:37 AM

<?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++;
}

?>

EDIT: In your reply #4 you said you wanted letter on row

i wanted the letter above it and then in the same row,


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 28 September 2006 - 09:43 AM

awesome... thanks very much....

#12 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 28 September 2006 - 12:40 PM

    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?

#13 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 28 September 2006 - 12:46 PM

Can you provide all of the code for this section, including the while loop...

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#14 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 28 September 2006 - 12:54 PM

its ok i have worked it out..... i just put a table around the while loop.. thanks anyway

#15 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 September 2006 - 03:06 PM

or use css

<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++;
}

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users