Page 1 of 1

Help? Store a variable for later call?

Posted: Wed Aug 24, 2005 4:08 pm
by iampariah
I hope this is the right forum. Other help requests prompted me to post here.

I'm very, very new to PHP. I'm learning more by deconstructing and editing code--working up to writing it myself.

I'm trying to get a particular PHP script on my WordPress blog running, but I'm having no luck with my modifications. The script works to spit out text, but I'm trying to get it to simply create a variable (in the page header area) that I can later call from the body.

Here's the original script:

Code: Select all

<!-- Actual CATEGORY SNIFFER Begin -->
<?php $psb_category_id = get_category($cat); ?>
<!-- If category is parent, list it -->
<?php if ($psb_category_id->category_parent == 0) { ?>

<p><?php echo get_category_link($cat); ?></p>

<?php $psb_category_id->category_parent = $cat; ?>
<?php } else { ?> 
<!-- If category is not parent, list parent category -->
<?php $parent_category = get_category($psb_category_id->category_parent); ?>

<p><?php echo get_category_link($parent_category->cat_ID); ?></p>

<?php } ?>
<!-- Actual CATEGORY SNIFFER End -->
As is, the evaluations result in printing out the category_parent, but by two different means. What I need is for the evaluations to result in printing nothing, but creating a single variable ($psb_category_id) in either case (is parent or get parent). Later, down in the body of the page, I'll make calls to $psb_category_id.

I realize this is a very nOOb process, but I'm a nOOb. Would someone give me the rewrite to get the script I need (and maybe an explanation of that rewrite)?

Posted: Wed Aug 24, 2005 4:14 pm
by feyd
Welcome to the forums. If you haven't read the forum rules, please do so. We are glad you used the

Code: Select all

tags on your first post, hoorah!

Unfortunately, the general discussion board is not where code questions are to be posted (as said [url=http://forums.devnetwork.net/viewtopic.php?t=29088]here[/url])


Moved to [b]PHP - Code[/b].

Posted: Wed Aug 24, 2005 4:22 pm
by Sander
Just so you know, PHP also has comments: // You use them like this:

Code: Select all

// blaat
You also don't have to use the <php tag around every single line. It would make your code much easier to read:

Code: Select all

<?
// Actual CATEGORY SNIFFER Begin
$psb_category_id = get_category($cat);

// If category is parent, list it
if ($psb_category_id->category_parent == 0) {
  echo '</p>' . get_category_link($cat) . '</p>';
  $psb_category_id->category_parent = $cat;
} else {
  // If category is not parent, list parent category
  $parent_category = get_category($psb_category_id->category_parent);
  echo '<p>' . get_category_link($parent_category->cat_ID) . '</p>';
}
// Actual CATEGORY SNIFFER End
feyd wrote:Welcome to the forums. If you haven't read the forum rules, please do so. We are glad you used the

Code: Select all

tags on your first post, hoorah!

Unfortunately, the general discussion board is not where code questions are to be posted (as said [url=http://forums.devnetwork.net/viewtopic.php?t=29088]here[/url])


Moved to [b]PHP - Code[/b].[/quote]
I also used the

Code: Select all

tag in my first post, but there was no hooorah for me

Posted: Wed Aug 24, 2005 4:31 pm
by iampariah
Hoorah! :D
Sander wrote:Just so you know, PHP also has comments: // You use them like this:

Code: Select all

// blaat
You also don't have to use the <php tag around every single line. It would make your code much easier to read:
Thank you. I actually knew that--and cleaned up my code during my (faulty) modifications. I took the code back to the very beginning, though, to try to find where I'd gone wrong.

Do you know how to modify that code to give me the result I noted in my first post?

Posted: Wed Aug 24, 2005 5:36 pm
by Stewsburntmonkey
If you just want to store what the original code would have printed then this should do it:

Code: Select all

<?
// Actual CATEGORY SNIFFER Begin
$psb_category_id = get_category($cat);

// If category is parent, list it
if ($psb_category_id->category_parent == 0) {
  $category = get_category_link($cat);  // store what would be printed in the $category variable
  $psb_category_id->category_parent = $cat;
} else {
  // If category is not parent, list parent category
  $parent_category = get_category($psb_category_id->category_parent);
  $category = get_category_link($parent_category->cat_ID);
}
// $category now holds whatever would have been printed

// Actual CATEGORY SNIFFER End
:)

Posted: Wed Aug 24, 2005 6:09 pm
by iampariah
Stewsburntmonkey wrote:If you just want to store what the original code would have printed then this should do it:
Thank you. I'm still having trouble with it, though. With your code in my header, I get a fatal error "Call to undefined function: category() " when I call it with:

Code: Select all

<p><font size="4" color="#FF0000"><?php category(); ?></font></p>
(I had closed your script with ?> of course.

Posted: Wed Aug 24, 2005 6:26 pm
by Stewsburntmonkey
In my code $category is a variable not a function. If you wanted to display the contents of $category then your code should be:

Code: Select all

<p><font size="4" color="#FF0000"><?php print($category); ?></font></p>