Help? Store a variable for later call?

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
iampariah
Forum Newbie
Posts: 3
Joined: Wed Aug 24, 2005 3:55 pm

Help? Store a variable for later call?

Post 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)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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].
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post 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
iampariah
Forum Newbie
Posts: 3
Joined: Wed Aug 24, 2005 3:55 pm

Post 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?
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post 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
:)
iampariah
Forum Newbie
Posts: 3
Joined: Wed Aug 24, 2005 3:55 pm

Post 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.
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post 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>
Post Reply