PHP is fun, and I need an assist [but I know little to nothing about it]

chabotc@uoguelph.ca's picture

My problem is that I have a text file that consists of lines that look like:

spn_001,T
spn_002,T

and I need to read each line in, then I break it up using explode() to get $parts[0], and $parts[1].
$parts[0] is the name of the item
$parts[1] is the visibility/printing boolean value (T means print, F means ignore)

This seems like a pretty straight forward thing to do.
It's not working out for some reason.
The code reading in and printing out is below.

<?
while (!feof($file_handle) ) {

	$line_of_text = fgets($file_handle);
	$parts = explode(',', $line_of_text);

	if ($parts[1] == "T") {
		++$itemCount;
		?>
		<td><img src="images/<? $parts[0] ?>.png"><br>
		This is an item called <? $parts[0] ?></td>
		<?
	}

}
?>

I know the line:

    <td><img src="images/<? $parts[0] ?>.png"><br>

is broken, but I can't figure out how to append the ".png" to the variable name to reference the correct image file.

Also, it's only the last item read in from the external file that ends up getting printed.

Lots of web searching. Anyone fancy giving some tips [or solving it in 5 minutes if you know anything about PHP]?

chabotc@uoguelph.ca's picture

PHP doesn't like me adding

PHP doesn't like me adding the ".png" on to the variable name, so I have to concat it on to make it work, or just have another column in the source file that says, "spn_001.png".

In fact, it has to say, "images/spn_001.png", else only the very last entry in the list works.

Web sleuthing has not resolved that issue yet. Maybe someone can chime in?

2nd 3rd year H.B.Comp
CPES SC VP Social Finance
Office hours W10
Tuesday 1500-1600
Thursday 0930-1030
SCIE 1505

chabotc@uoguelph.ca's picture

Solved

I just used some string variables like this:
img src= $directory.$part[0].$fileExtension

Thanks for you help, Chad!

2nd year H.B.Comp
CPES SC VP Social
Office hours W10
Tuesday 1500-1600
Thursday 0930-1030
SCIE 1505

jlapp's picture

WOW did you break the page

WOW did you break the page layout. Protip: the forums understand some small forms of HTML in the comments, so you'll need to escape the brackets < and > in anything you want to actually see.

The first try didn't work because you need to use <?= to echo text like that. You could also have concatenated the extension on like so:

<img src="images/$parts[0]" . ".png" />
aberry@uoguelph.ca's picture

Enable the HTML corrector

Enable the HTML corrector input filter to fix that; it will ensure that all tags close, preventing bad HTML from breaking the rest of the page.

http://drupal.org/project/geshifilter is awesome :D

--
Andrew

aberry@uoguelph.ca's picture

Unless this is a really

Unless this is a really small project, you should look at using a templating layer. This will allow you to separate your HTML from your PHP code, and will make your code far more maintainable.

Also, be careful if your text file is from an untrusted source. Otherwise, it could be used for XSS.

--
Andrew

chabotc@uoguelph.ca's picture

This "Templating Layer" you

This "Templating Layer" you speak of… perhaps you can enlighten me as to how to build such a thing?
A couple relevant URLs would be super rad.

The concept is, of course, not difficult to grok, but a hint/push in the right direction as to how to do such a thing [properly] would be great.

*Chad

2nd 3rd year H.B.Comp
CPES SC VP Social VP Finance
Office hours W10
Tuesday 1500-1600
Thursday 0930-1030
SCIE 1505

aberry@uoguelph.ca's picture

PHPTemplate and Smarty are

PHPTemplate and Smarty are both well known.

--
Andrew

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.