Toon posts:

[MySQL/PHP] Sorteren op veldlengte

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik ben in een while($row = mysql_fetch_array($result)) lus aan het str_replace'en waarbij ik dus alle condities uit een MySQL table gehaald heb.

Ik loop dus 1 voor 1 de te testen strings af en doe iedere keer een str_replace.

Het vervelende is dat ik een volgorde moet aanhouden want anders gaat hij dingen 'te vroeg' replacen.

Bijvoorbeeld:

'test1' moet 'xxxxx' worden
'test12' moet 'yyyyy' worden

Ik ben dus verplicht om eerst te testen op main12 want anders krijg ik op de plaats waar 'main12' staat 'xxxxx2' i.p.v. 'yyyyy'.

Ik zou dus het liefst de results in m'n SELECT gelijkt gesorteerd op veld/string lengte terugkrijgen.

Weet iemand of dit kan en/of hier een andere truuk voor is?

  • chem
  • Registratie: Oktober 2000
  • Laatst online: 27-08 13:53

chem

Reist de wereld rond

wat dacht je van "order by strlen(column) asc"

('t is geloof ik geen strlen, check de manual: string functions)

Klaar voor een nieuwe uitdaging.


  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

code:
1
2
3
4
SELECT     meuk 
FROM     tabellen 

ORDER BY   LENGTH(veldnaam)

da's toch redelijk basic dacht ik zo :?

sowieso twijfel ik aan je script zoals je het vertelt, maar dat ff daar gelaten

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Grum
  • Registratie: Juni 2001
  • Niet online

mysql> create table test (id int not null auto_increment primary key, txt varchar(255) not null default '');
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test (txt) values ('test1'),('test12'),('est1');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select id,txt,LENGTH(txt) as len from test order by len,txt;
+----+--------+-----+
| id | txt | len |
+----+--------+-----+
| 3 | est1 | 4 |
| 1 | test1 | 5 |
| 2 | test12 | 6 |
+----+--------+-----+
3 rows in set (0.02 sec)

mysql>


:P

Verwijderd

Topicstarter
duh... |:(
En ik maar denken dat het moeilijk was :)

Dank!