applying html formatting to different array elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tobias
Forum Newbie
Posts: 4
Joined: Mon Aug 26, 2002 11:40 am

applying html formatting to different array elements

Post by tobias »

I'm doing a class listing for a nonprofit community program

Thus far, I've been able to pull from a text file:

sample of text file:

Code: Select all

Introduction to Databases Using Microsoft Access|CEP #123-B|(10 weeks) 2.4 Continuing Education Units (CEU)|Maurice E. Kennedy, Jr.
The Harlem Renaissance Remembered|CEP #264|Tyrone Stevens Drummond
African-American Reparations: Here & Now|CEP #286 (6 weeks)|Mr. Robert Bell, Mr. Milton McGriff
African American Male/Female Relationships: Surviving the New Millennium|CEP #04-A (8 weeks)|Dara Baye
Hypnotherapy:The Power of the Subconscious Mind|CEP #259|Instructor: J. Joanne Yohannes
The Science of Political Organizing and Organizational Building|CEP #308 (5 weeks)|Instructor: Odinga Mukhtar
Basic Design/Sewing|CEP #213-A (8 weeks)|Instructor: Suzi White
and format it using this piece of code:

Code: Select all

<?
$file = "classes.txt";

$pull = file($file);

	foreach ($pull as $final)
	&#123;
		$final_val = explode("|",$final);

			foreach ($final_val as $output)
				&#123;
					print "<b>$output</b><br />";
				&#125;
			print "<br />";
&#125;
?>
Which give me the an out put like this:

Introduction to Databases Using Microsoft Access
CEP #123-B
(10 weeks) 2.4 Continuing Education Units (CEU)
Maurice E. Kennedy, Jr.

The Harlem Renaissance Remembered
CEP #264
Tyrone Stevens Drummond

...and so on and so forth

I have two questions, first can I get a pointer as how to have the class title bold, the class number italicized? In other words, how to apply different things to elements in my array

and second, is the above code effiecient?

Thank you much
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post by skehoe »

Hi,

If every record in the data file contains four fields (seperated by a "|"), you could do something like this after the explode instead of the foreach loop.

Code: Select all

echo "<b>".$final_val&#1111;0]."</b><br>"; // Course Title
echo "<i>".$final_val&#1111;1]."</i><br>"; // Class Number
echo $final_val&#1111;2]."<br>"; // Class Info
echo $final_val&#1111;3]."<br>"; // Instructor
Hope that helps,

~Scott
glo-wurm
Forum Newbie
Posts: 13
Joined: Mon Jan 13, 2003 2:07 pm
Location: Naples, FL

Post by glo-wurm »

may I suggest XML instead of using the text files like that? XML is fairly simple, you will just have to implement an XML parser, or use XML parsing module for PHP. It will be much more scalable, and much more clean to work with :D

otherwise, skehoe's method should work without issue :)
tobias
Forum Newbie
Posts: 4
Joined: Mon Aug 26, 2002 11:40 am

Post by tobias »

yes, I'm learning how to apply xml to everyday life now. Thanks for all yall's help.
Post Reply