erikjan Posted July 4, 2006 Share Posted July 4, 2006 I made a database which is filled with a main table which contains names of artists, a work they made, details of that work etc.I retrieve these details the following way. Visitors can click any letter of the alphabet and then get a list of the artists whose name end with that specific letter. So for instance, if they click on the letter "A", they see the following:Alexander Adonixander (1900-1987) Apollo 1959 | Unsigned Oil on canvas 51 x 55 cm Alexander Adonixander (1900-1987) Daphne 1966 | Unsigned Oil on canvas 87 x 77 cm Alexander Adonixander (1900-1987) Pandemonium 1974 | Signed Oil on canvas 123 x 140 cm Antoine Adrax (1899-1956) Cleopatra 1921 | Unsigned Oil on canvas 12 x 34 cm (Of course the layout is different, but that is not important for my question).My question is: Is it possible to get a formatting like this (instead of the one above), so [u]without [/u] repeating the name of the artist?Alexander Adonixander (1900-1987) Apollo | 1959Daphne | 1966Pandemonium | 1974Antoine Adrax (1899-1956) Cleopatra | 1969I have tried several things, but I didn't succeed until now. I would be extremely greatful if someone could give me a clue. Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/13634-please-help-on-formatting-retrieved-data-with-phpfrom-mysql-database/ Share on other sites More sharing options...
shocker-z Posted July 4, 2006 Share Posted July 4, 2006 $artist='none';$query=mysql_query("select * from table WHERE LEFT(artist', 1) = '$startswith' ORDER BY artist ASC");while ($row=mysql_fetch_array($query)) { if ($artist !== $row['artist']); $artist=$row['artist']; echo('<Br>'$artist.' ('.$date.')<Br>'); }echo($var1.' - '.$var2.'<Br>');}I think that will do the job mate but i havn't tested it..Hope it point you in the right direction any way :)RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/13634-please-help-on-formatting-retrieved-data-with-phpfrom-mysql-database/#findComment-52875 Share on other sites More sharing options...
toplay Posted July 4, 2006 Share Posted July 4, 2006 Sort the query by name (i.e. SELECT * FROM table_name ORDER BY `name`).Then display the name whenever it changes. Something like this:[code]// First do open, select DB, query and place in $result$name = ''; // Initializewhile ($row = mysql_fetch_assoc($result)) { if ($row['name'] != $name) { // Has the name changed? $name = $row['name']; echo '<br/><br/>', $name, '<br/>'; // Display heading/name every time it changes } echo $row['work'], ' | ', $row['work_year'], '<br/>'; // Display other data}[/code]p.s. Posted too late. Quote Link to comment https://forums.phpfreaks.com/topic/13634-please-help-on-formatting-retrieved-data-with-phpfrom-mysql-database/#findComment-52878 Share on other sites More sharing options...
erikjan Posted July 4, 2006 Author Share Posted July 4, 2006 TOPLAY and SHOKER-Z, both thank you so much!!The script you sent me, TOPLAY, i understand and it works. Simple and elegant!Sorry SHOKER-Z, but i'm sure yours works as well, but i don't get itquite. WHERE LEFT(artist', 1) =, can you explain? if you have time of course . .and is it correct the third { should be a } , while i am missing a { after if ($artist !== $row['artist']);Yours both. Quote Link to comment https://forums.phpfreaks.com/topic/13634-please-help-on-formatting-retrieved-data-with-phpfrom-mysql-database/#findComment-52906 Share on other sites More sharing options...
shocker-z Posted July 4, 2006 Share Posted July 4, 2006 As i said i hadn't tested it :Pleft(artist,1) will display the first letter of each artist there for if the 1st letter = $startswith then it starts with that letter same thing as WHERE artist LIKE '$startswith%' like you may have usedyeah i forgot the {} i'm at work what else can i say lolCustomers are eating away at my head by the minute every time i answer the phone! :)I spose it would have been easier if i didnt write the query like toplayRegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/13634-please-help-on-formatting-retrieved-data-with-phpfrom-mysql-database/#findComment-52912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.