Forum Moderators: coopster

Message Too Old, No Replies

Creating alphabetical list without using DB?

Want to have a list sorted from A-Z

         

I Will Make It

11:46 pm on Feb 26, 2006 (gmt 0)

10+ Year Member



... Don't want to use a database!

I have a list of widgets and I would like these widgets to be sorted on my page alphabetically and automatically.

What should I do? I really want to stay away from DBs ;)

Anyone?

Birdman

11:56 pm on Feb 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where will the list be stored? In a text file? If so, you do this:

<?php

$widgets = sort( file('/widgets.txt') );
foreach ($widgets as $widget) {
rtrim($widget);
print('some HTML code here' . $widget . 'more code' );
}
?>

I Will Make It

12:06 am on Feb 27, 2006 (gmt 0)

10+ Year Member



thank you for quick reply!
I'll try and se what happens :D I'll post here if I manage to get it right!

I Will Make It

12:15 am on Feb 27, 2006 (gmt 0)

10+ Year Member



hmm.. I get a
Warning: Invalid argument supplied for foreach()in C:\blablabla\myfilename.php on line 39.

Line 39 would be:
foreach ($widgets as $widget) {
in


$widgets = sort( file('widgets.txt') );
foreach ($widgets as $widget) {
rtrim($widget);
print('some HTML code here' . $widget . 'more code' );
}

I made a widgets.txt file containing nothing else but
widget
tegdiw
gdiwte
idgetw
dwtegi

and so on...

Sorry to be such an idiot.. :\ but I really have tried to find the answer on this forum, but without luck.
I'm also a newbie when it comes to php, although I've learned a LOT today!
So if you have the kindness, please help me one more time? :)

please?

Birdman

12:33 am on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm...looks like $widgets wasn't created.

Let's debug:

$widgets = sort( file('widgets.txt') );
print_r($widgets);
foreach ($widgets as $widget) {
rtrim($widget);
print('some HTML code here' . $widget . 'more code' );
}

Don't call yourself an idiot. You're just getting started and you will get better. Stick around here and it will happen faster :)

I Will Make It

12:45 am on Feb 27, 2006 (gmt 0)

10+ Year Member



thank you for helping me!
I will certainly hang around here more.. it's a great forum, with great people!

OK,

I tried this:
print_r($widgets);

I still get the error, but now it is a 1 above it..

like this:


1
Warning: Invalid argument supplied for foreach()

nothing wrong with the .txt file I made?

Is it supposed to look like this?
line1
line2
line3
line4
line5
and so on..

or this:

line1, line2, line3, line4?

Birdman

1:14 am on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you put the line before the foreach() like I did in my post?

Let's simplify it more:

<?php
error_reporting('E_ALL); //debug mode
$widgets = sort( file('widgets.txt') );
var_dump($widgets);
?>

You should either get an error or something like this:

array(10){
etc.

If you get array() then you should be able to run the original script. Go ahead and run this one and let us know the results.

[added]Yes, your text file is correct with each item on a line.[/added]

I Will Make It

1:18 am on Feb 27, 2006 (gmt 0)

10+ Year Member



Whooohoooo! I made it!
I changed the code to this:

<?php

$widgets = file('widgets.txt');
sort($widgets);
foreach ($widgets as $widget) {
rtrim($widget);
print('some HTML code here ' . $widget . ' more code<br>' );
}

?>

Now it's sorting like it has never done anything else before!

And the best part is, I understand the code too! LOL
Thank you so much Birdman for pushing me in the right direction!

Birdman

1:23 am on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's odd that it won't work with the sort() and file() combined into one statement. Oh well...

I'm glad you got it figured. My pleasure.

I Will Make It

1:24 am on Feb 27, 2006 (gmt 0)

10+ Year Member



Oh.. you answered me the same time I was writing ;)
It seems like I had to put the content of the file, and sort the content in two different operations?
Do you think it has something to do with the php version i'm running?
Anyway, thank you so much.. You don't even know how much I appreciated this one!
I'll still check out what happens with that error checking of yours :D
It's such fun when you finally get it right!