Jump to content

*New to PHP* Need to strip first character from a string


JasonTTex84

Recommended Posts

I have this command that basically populates my website with titles for pages based on the information that is placed within "info.txt" I have multiple titles being created so I'd like a way of putting them in order beside alphabetically.

 

My thought was that I could place numbers as the first character in the "info.txt" file and that would allow me to put the titles in an order I find acceptable. Now this causes the issue that I don't want those numbers displayed on my website. So the Echo needs to strip the number.... I'm lost, but my brain thinks it's a good idea.

 

Here's the code I'm working with:

 

		<tr>
		<td id="body">
			<table id="columns" cellspacing="0">
			<tr>
			<td>

				<h2>LSAV Latest Events</h2>
				<p>Select an event from below to view a gallery of images from that event.</p>
				<ul>
				<?php
				$dir = 'latest-events';
				if ($handle = opendir($dir)) {

				    while (false !== ($file = readdir($handle))) {
						if( $file != '.' && $file != '..') {
							$title = file_get_contents( "$dir/$file/info.txt" );
							echo "<li><a href=\"event-viewer.php?event=$file\">$title</a></li>";
						}
				    }
				    closedir($handle);

you could actually do it inline

...
echo "<li><a href=\"event-viewer.php?event=$file\">" . substr($title, 1) . "</a></li>";
...

 

This code worked great in the fact that it did indeed strip the first letter from my text file, however I can't figure out what is determining the order in which these Titles are displayed. I tried put 1-2-3-4 in front of each title string as well as a-b-c-d and neither worked for me.

 

Any suggestions??

You should use a specific separator character that will never exist in the data, such as a |, between the 'fields' in your data. You would then explode each line using that separator character.

 

What happens with the current code once you get more than 9 events saved in the file?

You should use a specific separator character that will never exist in the data, such as a |, between the 'fields' in your data. You would then explode each line using that separator character.

 

What happens with the current code once you get more than 9 events saved in the file?

 

I get what your saying... Perhaps use letters, a-z = 26... I'll never have more than say 10-15 events listed.

 

I guess my issue right now is that I don't understand what method the PHP is using to sort the items I have now, everything is in alphabetical order off of the first letter of the Title (of course ignoring the letter that I put in front of it i.e. a-z...) except for the last two which start with "Z" and "Q" and are listed in that order.

 

None the less I don't want alpha order I really want to determine the order myself.

 

What I have is this, I have a directory with folders, each folder has photos as well as thumbs of each photo as well as a file named "info.txt". Whatever I type in the "info.txt" file is what shows up on the page as the title of the page containing the photos in that directorty. So each "info.txt" file basically has a few words in it with a year and a couple "-". That's all.

 

When I started this thread all I wanted to accomplish was setting the order in which these titles were displayed, I thought I was on the right track but obviously not.

 

Thanks for all of your help thus far, you have given me exactly what I've asked for... Just seems that what I'm asking for isn't the solution....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.