Help - Search - Members - Calendar
Full Version: how to modify this php for my random song lyrics
Movable Type Community Forum > Additional Resources > Tips and Tricks
brainchild2b
This php script:

CODE
<?php
 function randomline($filename) {
   $file = file($filename);
   srand((double)microtime()*1000000);
   $randomquote = $file[rand(0,count($file))];
   echo $randomquote;
 }
 randomline("/path/to/file/quotes.txt");
?>


randomly grabs a line from the text file and displays it. However I'm using this for my song lyrics and would prefer it to grab the text in between the "*" character. It's much easier having the lyrics span multiple lines and having the separated by a line with an "*" character on it. Please help! Thanks!
bmk
I'm not clear on the question.  But this is the way I used to do it:

The dump file.  Each "entry" has five lines, but you can have whatever number you want.  And each "entry" is separated by --------.

The script (from the forums here somewhere)
CODE
<?php

$filename = "/usr/local/psa/home/vhosts/mngeo.com/httpdocs/pix/thumbs.dump";                        
$posts = explode('--------', implode('', file($filename)));

srand((double) microtime() * 1000000);
list($num, $it) = each($posts);
$it = trim($it);
while (list($num, $line) = each($posts)) {
  $line = trim($line);
  if ($line != '')
      if (rand(0, $num+1) < 1)  
          $it = $line;
}

list($date, $title, $url, $excerpt, $cat) = explode("\n", $it, 5);          
echo "<a href=\"$url\"><img alt=\"PhotoLog\"class=\"pix\" border=\"1\" src=\"http://www.mngeo.com/pix/img/$excerpt-thumb.jpg\" /></a>";
?>


This part: list($date, $title, $url, $excerpt, $cat) is naming each line in the "entry", and the 5 is telling how many parts are to the "entry".

So you could use that to have one line as your title, one for the artist, one for the quote, etc.  It shouldn't count as the next part until you have a carriage return - \n between the sections.


The way I do it now is to put the data in a mysql table and then run a query with ORDER by rand() LIMIT 1 in it.  Works nice.

If you have mysql access you should consider setting up a table for your song lyrics.

Hope that helps, and yeah, I really didn't understand the question, sorry if I'm way off, just post back.  :)

Brenna
brainchild2b
can you post at least 2 sample entries from your database so I know exactly how to write mine? Thanks!
brainchild2b
also since most lyrics don't have all that information do i have to put them in a certain order, is it strick that each lyric must have a name, date, and other stuff? what if i don't have all that information for each lyric?
bmk
I was using that dump file that I linked above.  For each entry I have five things: date, title, url, excerpt, category. Each thing is separated with a carriage return and then each entry is separated from the next with the dashes.  (this was from my photoblog, using MTEntryExcerpt to enter the name of the image)

So you would just make a text file with your data in it that looks like the dump file.  I'm guessing it'll work out better to add in for the information you're missing, such as "Unknown Artist" so that everything stays lined up right.

I made that dump file by setting up an index template in my photo blog with this:
CODE
<MTEntries lastn="500"><$MTEntryDate format="%b %e, %Y"$>
<$MTEntryTitle$>
<$MTEntryLink$>
<$MTEntryExcerpt$>
<$MTEntryCategory$>
--------
</MTEntries>


Hope that helps, post back if you want to talk more.  :)

Brenna
hezair
hello there bmk,

i was fumbling around with yr script, and it even seemed to work!  but AH, i had to try and complicate things.

I wanted to be able to use the MTEntryBody (which would include an MT generated pop-up image tag) rather than the image tag you create with $excerpt in your example.

so my dump file looks like:

CODE
<MTEntries lastn="500">
<$MTEntryDate format="%b %e, %Y"$>
<$MTEntryBody$>
<$MTEntryTitle$>
<$MTEntryLink$>
<$MTEntryExcerpt$>
<$MTEntryCategory$>
--------
</MTEntries>


thumb dump: http://nearlythere.com/haha/thumbs.dump


and my php looks like this:
CODE
<?php

$filename = "/localpathto/nearlythere.com/haha/thumbs.dump";              
$posts = explode('--------', implode('', file($filename)));

srand((double) microtime() * 1000000);
list($num, $it) = each($posts);
$it = trim($it);
while (list($num, $line) = each($posts)) {
$line = trim($line);
if ($line != '')
  if (rand(0, $num+1) < 1)  
    $it = $line;
}

list($date, $body, $title, $url, $cat) = explode("\n", $it, 5);      

echo "$body";
echo "title: $title";
echo "$date";
echo "<hr />";
echo "url: $url";
echo "category: $cat";
?>


but of course, the EntryBody with all the " " makes the script freakout... (you can see the title is not appearing on the correct line... )
http://nearlythere.com/test_randomimg.php



I assume that the way you created your, you need to make all the thumbs and larger images yrself... and upload them via ftp?

OR - do you upload them within MT...?

hmmm... i think i am missing some big clue here...

Essentially, what I want is a random image, with link to pop-up window, and a title/date to appear on a page. And I don't have SQL access...

MAYBE just maybe it's bedtime...
bmk
Okay...

1. Turn off line breaks for that entry to get rid of the P tag around it.  That's on the edit entry screen, uncheck the box.

2. Take all the popup code out of the EntryBody area, and just put the unique part IMG_0413 (you would write the actual code for the popup in the echo part of the script, substituting $body for IMG_0413)

3. You have 6 things now instead of 5 so change this line to add $excerpt:
list($date, $body, $title, $url, $excerpt, $cat) = explode("\n", $it, 6);      

4. I think for the dump file to line up right you need to write the first two lines (start MTEntries and MTEntryDate) on the same line like I have further up this thread.

I did all mine in MT, using the thumbnail feature but you don't have to do it that way.

Hope this helps, please post back how it goes.

Brenna
bmk
Are all of your entries for this random thing going to follow the same format?  Because if some are going to be popup pictures and some are not, the method I described for just placing the unique part in the EntryBody won't work.

I'll wait to hear from you on that... you'll still need to do the few other things I mentioned.
bmk
Right, how about you keep the entry with the popup code in it, and use this in the dump template instead:
CODE
<$MTEntryBody encode_php="qq"$>

Does that take care of it?
hezair
Encode PHP?! That's brillig! Probably exactly what I need.

(In ans. to your Question : Not all the pix will be the same size)

In the meantime I came out with ye olde dog-simple idea. It is hybrid of your thumb dump and my... uh... bruteforce method: include the entries via php.

DOG SIMPLE VERSION:
Entries consist of a title, and an entry body which has the img src and pop-up tags. PHP is used to include the individual archives.

STEP 1 - Individual entry archives are set up like this:
CODE
<$MTEntryDate format="%B %d, %Y"$><br />
<$MTEntryBody$><br /> <!-- holds the MT generated img src tags -->
<$MTEntryTitle$><br />


STEP 2 - The so-called thumb-dump now only keeps the EntryID which I will use for the include URL:
CODE
<MTEntries lastn="500">
<$MTEntryID pad="1"$>
--------
</MTEntries>


STEP 3 - And the php simply rifles through the list and includes the simple html created in the individual archive.

CODE
<?php

$filename = "/localpathto/nearlythere.com/haha/thumbsID.dump";              
$posts = explode('--------', implode('', file($filename)));

srand((double) microtime() * 1000000);
list($num, $it) = each($posts);
$it = trim($it);
while (list($num, $line) = each($posts)) {
$line = trim($line);
if ($line != '')
  if (rand(0, $num+1) < 1)  
    $it = $line;
}

list($thumbID) = explode("\n", $it, 1);      
echo "$thumbID";
include "/localpathto/nearlythere.com/haha/archives/$thumbID.html";
?>


Vwa-Lah and they say.

But, now I need to go see how yr tip works!
hezair
hmm... not sure about that $MTEntryBody encode_php="qq"$. it does all kind of weird things to the img src because there are so many attributes in it... and also since there is javascript, there are also single quotes ( so $MTEntryBody encode_php="q"$ isn't handy either )

i wonder how that is useful. alas and alack, it does not appear to be so for me.

it might also be due to a certain je ne sais quoi i have about php. (meaning: i don't know what is happening.)

hmm...

thanks though!
bmk
Well, hmm, I thought that's what the php encode was for.  What exactly happens that it doesn't work?  Do you get parse errors, or do the lines in the dump file not line up right then?
hezair
Here's where I use the single 'q' version, if i don't the image doesn't show up at all. See, I think it's because I have both single and double quotes in the EntryBody (which is written by MT to make the pop-up link)

http://nearlythere.com/test_randomimg.php

this is what i get using the single 'q'

CODE
<p><a href="http://nearlythere.com/haha/archives/images/IMG_0413.html" onclick="window.open(\'http://nearlythere.com/haha/archives/images/IMG_0413.html\', \'popup\', \'width=381,height=432,scrollbars=no,resizable=no,toolbar=no,directories=no,lo
cation=no,menubar=no,status=no,left=0,top=0\'); return false"><img src="http://nearlythere.com/haha/archives/images/IMG_0413-thumb.jpg" width="200" height="226" border="0" /></a><br />title: </p>
Aug 28, 2002
<hr />url: we all scream for ice cream
category: http://nearlythere.com/haha/archives/000099.html
IMG_0413
kids


and this is what i get using the double 'qq'

CODE
<p><a href=\"http://nearlythere.com/haha/archives/images/IMG_0413.html\" onclick=\"window.open('http://nearlythere.com/haha/archives/images/IMG_0413.html', 'popup', 'width=381,height=432,scrollbars=no,resizable=no,toolbar=no,directories=no,lo
cation=no,menubar=no,status=no,left=0,top=0'); return false\"><img src=\"http://nearlythere.com/haha/archives/images/IMG_0413-thumb.jpg\" width=\"200\" height=\"226\" border=\"0\" /></a><br />\n</p>
title: we all scream for ice cream
Aug 28, 2002
<hr />url: http://nearlythere.com/haha/archives/000099.html
category: IMG_0413
kids


See the difference? It doesn't quite work. In the dump file it looks like:

CODE
Aug 28, 2002
<p><a href="http://nearlythere.com/haha/archives/images/IMG_0413.html" onclick="window.open(\'http://nearlythere.com/haha/archives/images/IMG_0413.html\', \'popup\', \'width=381,height=432,scrollbars=no,resizable=no,toolbar=no,directories=no,lo
cation=no,menubar=no,status=no,left=0,top=0\'); return false"><img src="http://nearlythere.com/haha/archives/images/IMG_0413-thumb.jpg" width="200" height="226" border="0" /></a><br />
</p>
we all scream for ice cream
http://nearlythere.com/haha/archives/000099.html
IMG_0413
kids
--------


Oh, anyway, I wanted to respond since you asked...

But I think I'll use my other work around until i can learn some more about the php encode...

Thanks anyway!
bmk
I was just playing with this and it works for me!  And you don't have to do any encoding at all.  

Don't forget to disable line breaks.  You can do it on the entry page, or in the the template:
CODE
<$MTEntryBody convert_breaks="0"$>


And then for the onclick you'll need that HEAD code somewhere on the page you're including the script.
brainchild2b
Don't mean to be a prick, but why can't somebody just write a simple php script like I posted about on my first post instead of all this messy complicated crap?
bmk
Um, it is.

I don't know why you can't get it to go, it works great.  From what I understood, this was what you were looking for.  I was just trying to help.   prick?  crap?  yeesh.  bye.
kadyellebee
That was rude.  

People on this board are always helpful, and just because you think it should be EASY doesn't necessarily mean it is.  If you want to be critical, try writing your own script.  

Kristine
brainchild2b
sigh i don't mean to be rude.. i'm harmless

when i said the last post i wasn't meaning that somebody should write a script for me anymore, i actually figured it out on my own. I was trying to understand why they way i thought of doing it wasn't used. The reason is it seems like people are overcomplicating their websites by using the other scripts.

While the other scripts work, it's not very portable was what i was trying to say. The format is somewhat difficult if someone wanted to adapt it elsewhere or for simply adding in quotes when you feel like it. Most of the quotes people add are spur of the moment, they see it then add it. They generally don't add 500 quotes in one sitting.
bmk
See, that's the beauty of setting these up like entries. You could just use the bookmarklet then to add quotes quickly and you don't have to mess with the code at all once it's set up.  The way I understood it, you used to edit the dump file by hand every time you added one?   You wouldn't do that any more.

The code provided here works *exactly* as posted.  That night after you couldn't get it, I set up a sample template, script, and your entry.   It works perfectly.  We kind of got off track with the encoding thing, but I see now that you just didn't have it set up right.

The last time I looked, you still have paragraph breaks on and I don't think that's going to work like that.

I mean, it's a simple thing.  There's a dump file and a script to split up the dump file and pull a random.  I don't know why you're saying it's so complicated.

If you want popups you have to add the onclick head code, that really doesn't have anything to do with the rest of this.

Just stick with your workaround then.  ???

You think you're frustrated?  Try posting on a two page thread on something you already know how to do, helping somebody out, only to have that somebody call it crap.

I don't use this method any more and I didn't write it.  I picked it up on the forums here and figured out how to make it work for me.
bmk
And it does.  :)

Sorry you had trouble with it.

I'll let you know, too, that we answer MANY repeat questions and are polite about it.  So please please please be nice.  People will bend over backwards here to help.  Serious.  :)
hezair
hey there... i was gonna sit this one out... but i thought i might put in my two cents on why (i think) we got beyond brainchild's orig. idea.

i was looking for a way to have random images (not song lyrics) and i wanted to use the built-in image upload. so i could have images pop-up or not pop up (without coding in the img tag like bmk did)

so while your simple php script might work for you (plain text only), it doesn't for me... and hey, it might not for someone else who comes across this thread searching for 'random images display'.  ... so they may be happy to find this kludgy bit of code that actually works.

oh, btw: brainchild says ' i actually figured it out on my own.'

how about posting the code you finally used? it might round this thread out a bit.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.