Echo Variable before given value, need help

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
aefeskemen
Forum Newbie
Posts: 3
Joined: Fri Sep 12, 2008 6:34 pm

Echo Variable before given value, need help

Post by aefeskemen »

Hello everyone,

I need help with an item counter on my site that will tell the user how many items are in a single category. Items will be added frequently, so there needs to be a variable representing the # of items. So I have made $counter. When each item is added, I simply do $counter++ to increment the counter by 1 item. I then print out the $counter variable with echo and it is okay. However, the problem is, I want to echo the $counter ABOVE the increments $counter++. The problem is that it does not include these increments because they come after the echo. So the echo doesn't update with the counters. If echo comes after the $counter++'s, it works, but I can't have it like that.

Here is what works, but echo is on the bottom, I need it on the top:

Code: Select all

<?php $counter=0; ?>
 
<?php $counter++; ?>
<?php $counter++; ?>
<?php $counter++; ?>
 
<?php echo $counter; ?>
This is what I need, but clearly doesn't work because counter is zero, not three:

Code: Select all

<?php 
 
$counter=0; 
 
echo $counter; ?>
 
<?php $counter++; ?>
<?php $counter++; ?>
<?php $counter++; ?>
 
ssssss
Forum Newbie
Posts: 17
Joined: Fri Aug 29, 2008 8:34 am

Re: Echo Variable before given value, need help

Post by ssssss »

As you are aware, you cannot echo a variable before it is set. Your code cannot predict the future.

If you tell us more about the rest of your code, I'm sure there's an easy way to echo the correct number in the correct place.

Are you looping through an array of items that you need to count? If so, look at the count() function. You will not need to count the items yourself, you can simply:
<?php echo count($array_name); ?>
aefeskemen
Forum Newbie
Posts: 3
Joined: Fri Sep 12, 2008 6:34 pm

Re: Echo Variable before given value, need help

Post by aefeskemen »

Hello,

Thank you for your response. I am unfortunately not using arrays. I am making a system where I can upload items to a category. This category includes a file 'include.php' that I write to when I want to make a new item in a category. Each item is added at the bottom and makes a block of code. I figured when I write this block of code to the include.php, I could just $counter++ and it would add up the counters and this would be the item numbers. But I must add at the bottom as you can see. So any way to get around this? I was thinking a while loop, but couldn't figure that out. I basically need to to counter up all the "counters" at the way beginning of the program (before the echo).

Thanks!
ssssss
Forum Newbie
Posts: 17
Joined: Fri Aug 29, 2008 8:34 am

Re: Echo Variable before given value, need help

Post by ssssss »

So your application accepts some input and then writes php code or html to various include files? Is that right?

I lied, then. It's not going to be easy.

It would be better to save the information in some way other than as formatted html or php code. Some sort of raw data format like xml, csv or something. Much, much better. And not only because it would make the current problem trivial.
aefeskemen
Forum Newbie
Posts: 3
Joined: Fri Sep 12, 2008 6:34 pm

Re: Echo Variable before given value, need help

Post by aefeskemen »

Well, the program is practically already finished, so I think I will just skip the counter. It is not necessary, just interesting. I can also just put it on the bottom. Thanks for your help anyway.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Echo Variable before given value, need help

Post by Stryks »

Finished is a floating state. I myself have several projects I work on that go through almost daily states of being 'finished'.

But this storage method is going to come back and bite you, and I'd really recommend switching to an alternate method. Not now perhaps, but at some point in later development. Database storage being the top of my list for alternate methods. Even if you don't have access to mysql, something like sqlite provides a nice flat file database with all the whiz-bangery of an ACID compliant database.

It just makes life a lot easier, especially if you ever have to alter your application. I mean, what if you wanted to change the display order. Or even edit or delete rows of data from the middle of the list. Sure you can do it with your method, but it's a whole lot easier using practically any other method.

Just food for thought anyhow.

As far as displaying the counter, if you're really wanting to get it done, you could do something like ...

Code: Select all

<?php
   // start buffering the output
   ob_start();
 
   // Your usual HTML output generation goes here
   // except where you want the counter, you'd put something like ...
   echo 'Items In List: <<counter>>';
 
   // capture the buffered page
   $output = ob_get_clean();
   
   // insert the final counter into the buffered output
   $output = str_replace('<<counter>>', $counter, $output);
 
   // Output the revised HTML
   echo $output;
 
?>
 
That might work well enough to suit your needs in the short term anyhow.

Cheers.
Post Reply