Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP For - getting values
Old 06-29-2010, 02:07 PM PHP For - getting values
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hi,

I have this for loop:

Code:
for ($i = $postNumber; $i > 0; $i--){
  print "$i<br>";
}
and what it does it count backwards to 0 (to 1, you don't see a 0) from whatever $postNumber is. $postNumber is always a number.

So let's say $postNumber is 5. The loop would say:
5
4
3
2
1
with line breaks separating each number.

My question is, in the above scenario, how do I turn the first number echoed (the 5) into a variable? It's not an array, or a string... I don't know what it is!

Thanks,

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2010, 04:57 PM Re: PHP For - getting values
Skilled Talker

Posts: 64
Name: Ralph Freshour
Trades: 0
Not exactly sure if this is what you want:

PHP Code:
for ($i $postNumber$i 0$i--)
{
  print 
"$i<br>";
  if (
$i == $postNumber)
     {
       
$php_variable $i;
     }

Now $php_variable equals 5
__________________
RalphF
Business Text Messaging Services

Please login or register to view this content. Registration is FREE
rfresh is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 05:00 PM Re: PHP For - getting values
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
What do you mean by turn it into a variable?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-29-2010, 05:19 PM Re: PHP For - getting values
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Thanks rfresh,

And NullPointer, what I'm doing with this is very complicated, but I'll do my best D: .

Ok so I have a script that adds posts to a database. The posts go in most recent order. So let's say you make a 'post'. It will be there. You make another post, it will not be on top of the first one. Now the file looks something like this:

Second Post|First Post

Second post is before First post.

Now I want to number these posts. Second post will be #1, and First post will be #2 (Yes, that way. Don't ask why, or I'll have to explain that, too D: ). If you add a third post...

First post will be #3, second post will be #2, and third post will be #1.

I need to keep track of these, somehow.

This is what I want:
$postNumber: First post|$postNumber: Second post|$postNumber: Third post.........

You know. But $postNumber would have to be fixed, and I'm writing this to a file, so opening and closing it, then reopening and closing it many times just to add one post hogs bandwidth. I don't want to put an fwrite in a loop

How can I possibly do this? With some uber mega ultra super coding?

Thanks
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 06-29-2010 at 05:21 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 06-29-2010, 08:02 PM Re: PHP For - getting values
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I think I see what you are going for.

Can't you just count the number of posts and then display then just count down as you display each post:
PHP Code:
$posts//array of posts
$postCount count($posts);

foreach(
$posts as $p)
{
     echo 
$postCount ': ' $p '<br />';
     
$postCount--;

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 06-29-2010 at 08:07 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-29-2010, 08:57 PM Re: PHP For - getting values
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, that should do it! I can't test it right now, I'm not on Linux (where my file is), but it sure looks good! Thanks, man!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP For - getting values
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17377 seconds with 12 queries