Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Number Display Help


PHP Number Display Help

Reply
  #1 (permalink)  
Old 12-17-03, 04:41 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Number Display Help

Hi again,

I need a code to display numbers in a row, each time the same echo code is displayed. For example, the first time it is displayed i'd want it to display 1, then the second it would display 2, and so on. I'm trying to do something like the following code at the moment, but the problem is a can't get it to display anything other than 1 each time the echo code is posted

PHP Code:

<?php

while ( $num <= ) {
    
$num += 1;
}
?>

<?php echo $num?>
Perhaps if someone could show me a correct code? Also, the echo code has to be the same each time, so things the following code won't work, because i will be deleting certain parts of the code at certain times, and i will need it to, say, if i delete part number two, for it to display '2' on number three, if you follow what i mean...

PHP Code:

<?

$numbers
=array("1","2","3");
sort($numbers);
?>

<?php echo $numbers[0]; ?>

<?php echo $numbers[1]; ?>

<?php echo $numbers[2]; ?>
Thanks to everyone who helps, if you can understand this, and also thanks to blaw, i got my code working last time
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-17-03, 05:31 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
loops are something useful in PHP
try to use FOR .. that's how is can be used :
PHP Code:

$names=array('NeverMind','tsb','Ryan');

$num=count($names);

for ( 
$i=1$i<$num$i++ ) {
  
$counter=$i-1
  
echo"$i$names[$counter] .";

and if you want to use it when you fetch data from databases like MySQL e.g. you need to make sure that mysql_fetch_array() is inside the loop not outside it.. like this :
PHP Code:

$fetch=mysql_query("SELECT * FROM table")or

die(
'couldn\'t excute query: '.mysql_error());

$num=mysql_num_rows($fetch);

for ( 
$i=1$i<=$num$i++ ) {
  
$data=mysql_fetch_array($fetch);
  
//we put it inside loop not outside it,
  //otherwise it will print the same data through the loop
  
  
echo $data['ID'], $data['name']; 

BTW if you want increment of a variable by 1 , you could use $var++ ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 12-17-03 at 05:38 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-17-03, 06:39 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Is that the whole code, because when i use it, it says:

Code:
Parse error: parse error, unexpected T_ECHO in /blah/blah/blah/numbers.php on line 7
All i basically want is something like:

PHP Code:

<? 

$numbers
=array("1","2","3"); 
?> 

<?php echo $numbers?> 

<?php echo $numbers?> 

<?php echo $numbers?>
Obviously that doesn't work, but i want it to display 1, 2, and 3 in each of the echos, in that order, with the echo codes being identical to each other...

Last edited by tsb; 12-17-03 at 07:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-17-03, 07:38 AM
steve steve is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php 

$num 
0;
while ( 
$num => ) { 
    
$num ++; 
    echo 
$num

?>
Will produce an endless loop of numbers
__________________
GamesValley
Your Home For Games
http://www.gamesvalley.net/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-17-03, 09:33 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
I used:

PHP Code:

<?php

$num 
0;
while ( 
$num >= ) {
    
$num ++;
    echo 
$num;
}
?>
...but how can i get it to display one number at a time, and in places i want it by using:

PHP Code:

<?php echo $num?>

So for example the echo in the second code would display '1', and then if i used that code again underneath it would display '2', and then if i used it again it would display '3', and so on?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 12-17-03, 09:38 AM
steve steve is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
hmm you could create a function

ie
PHP Code:

<?

function add1($num){
$num++;
return 
$num;
}
$numb 1;
?>
Then whenever you want to call another number you

PHP Code:

<?

$numb 
add1($numb);
echo 
$numb;
 
?>
You can make that a bit shorter but it not that long anyway
__________________
GamesValley
Your Home For Games
http://www.gamesvalley.net/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 12-17-03, 11:03 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, that's perfect =D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
PHP Dudes - Hi All stuart New Members & Introductions 4 05-03-04 02:22 PM
Realtime PHP Code Obfuscation barrywien General Advertisements 1 11-16-03 07:50 PM
Ad management and optimisation using php surebetdmg PHP 0 08-28-03 07:45 PM
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 03:08 PM


All times are GMT -5. The time now is 10:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.