Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2008-11-28 05:02:52

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Hi Roelof,

No worries about your PHP knowledge! I am game to help you out. :)


<div class="pro_linedrop">
	<ul class="select">
		<txp:php>
			$mah_lib_category_array_atts = array('type' => 'article', 'posted' => 'all', 'format' => 'name', 'entry_order' => 'true'); // Setup attributes.
			$category_list = mah_lib_category_array($mah_lib_category_array_atts); // Call method.
			# Loop through array:
			for($i = 0; $i < count($category_list); $i++) {
				$category = $category_list[$i];
				echo '<li class="line">' . "\n";
				echo '<a href="#nogo"><b>' . $category . '</b></a>' . "\n";
				echo '</li>' . "\n";
			}
		</txp:php>
	</ul> <!-- /.select -->
</div> <!-- /.pro_linedrop -->

I made a few changes:

  • Added semi-colons at end of echo statements. The semicolon signifies the end of a PHP statement.
  • In the second echo statement, the variable “category” was missing the $ sign.
  • In the for loop, I removed the echo statement of the UL… Not sure if you intended this, but I assume you do not want a UL for each outputted category name. I also added a closing LI.

I have not tested the above code, but it should get you further along… And, IIRC (it has been several months since I last used this plugin), it is for use outside of a form. :)

Also, here is a php tip:

If you want to quickly look up something via PHP, you can append it to the end of the php.net URL, like so:

  • php.net/echo
  • php.net/variables
  • php.net/for
  • php.net/array

I find that the comments (found on every PHP doc page) are also very helpful in learning PHP. Also, the “general” php.net mailing list is a very good resource… There is also a Google mailing list that is good also. ;)

I hope that helps! Feel free to post back with more questions.

Have a great night/day!

Cheers,
Micky

Last edited by mhulse (2008-11-28 05:20:55)

Offline

#14 2008-11-28 05:17:35

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Oh, I also forgot to mention:

You were not escaping your quotes in your strings (easy thing to overlook)…

For example:


$my_string = '<a href='http://site.com/'>FOO!</a>';

The above is not correct, in that the inner quotes need to be escaped, otherewise PHP thinks (same is true for most other languages), using above example, that the =' part is the end of the string, and that the rest of your statement on that line is malformed… Er, hard for me to describe, but here are three possible correct solutions:

  • $my_string = '<a href=\'http://site.com/\'>FOO!</a>';
  • $my_string = "<a href='http://site.com/'>FOO!</a>";
  • $my_string = '<a href="http://site.com/">FOO!</a>';

The first is using all single quotes… where I use the backslash, in front of the inner-single-quotes, to escape them in the string.

The second is using double quotes on the outside, and single quotes on the inside… This is correct because the double quotes were used on the outside.

The third is my prefered approach to quoting strings… Unless there is a need to use double quotes on the outside of the string, I prefer to use single quotes for my strings… This allows me to use double quotes for the “inner quotes” without breaking the string.

There some drawbacks, or features, to using single quoted strings… And vice versa…

For example:


Note:  Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. 

(Above quote is why I put the newline character in double quotes, and not within a single-quoted string: echo '<a href="#nogo"><b>' . $category . '</b></a>' . "\n";)

I am writing all of this fast, so sorry if I am not being clear. :(

Read more about strings and quotes here:

http://php.net/string

Oh, also, the period is a string concatenator

Last edited by mhulse (2008-11-28 05:29:25)

Offline

Board footer

Powered by FluxBB