Jump to content

Assign Number Automatically in Each Row (Please help)


apaunganhote

Recommended Posts

Hello all,

 

I am the new PHP beginner and I want to assign numbers automatically in each html row.

 

Like this,

 

I am testing the loop and i printed the result every time i click the submit button.

 

Whenever i type in the input box, it print at html row.

 

Let's say, I put "Name1", it print out

 

Name1 << in first row

 

then, i put Name 2, it print out Name2 below Name1 , like this

 

Name1

Name2

 

So  that, the one i want to do is that, assigning auto numbers to that print data. Example like

 

1. Name1

2. Name2

3. (whatever i type)

 

Sorry for my bad explanation also. Please help me and point me how can i do for that with example php coding. So that I can understand clearly. Thank you very much.

Link to comment
Share on other sites

Try this,

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>Test</TITLE>
</HEAD>
<BODY>
<?php 
$arr = $_POST['oldvalue'];
$arr[] = $_POST['newvalue'];
?>
<table>
<?php
for($i=0;$i<count($arr);$i++) {
?>
<tr><td><?php echo $arr[$i] ?></td></tr>
<?php	
}
?>
</table>
<form method="POST">
    <input type="text" name="newvalue">
<?php
for($i=0;$i<count($arr);$i++) {
?>
<input type="hidden" name="oldvalue[]" value="<?php echo $arr[$i] ?>">
<?php	
}
?>
<input type="submit" name="submit" value="Add Value">
</form>
</BODY>
</HTML>

Link to comment
Share on other sites

Hi Thanks,

 

It's very fast response and great help. Thank you for this. I want one thing in there which is about auto numbering,

 

Please kindly check my screenshot.

 

screenwr4.jpg

 

At there u will see as four texts are printed.

 

I want to get that things with assigning auto numbers before text

 

For example,

 

1. test

2. testing

3. testing3

4. test

 

Can you please kindly help for me. Thank you.

Link to comment
Share on other sites

Oh, well depending on how you want to organize things (if the number has to be unique to the record, or if it's just for displays sake)  You can do it one of two ways.

 

 

(If the number has to be unique to the record)

 

Set a field in the table to auto_increment.  (A lot of us call this field "ID")  It will start at 1, and each record that gets added will get a new ID.

 

 

(If it's for display purposes)

 

You're setting a counter with $i, and then incrementing it naturally in the loop with $i++  You can simply echo $i

 

 

echo $i.". ".$arr[$i];

 

Should display 1. (your value) and increment one each time the loop processes.

 

Let me know if this is what your'e looking for :)

Link to comment
Share on other sites

Hi nafetski,

 

Thanks for your great info. The one I want to get is like the one u said about only for the display purposes with looping process.

 

If you don't mind, can you give me the full php code for sample about that as I am so noob and as beginner.

 

echo $i.". ".$arr[$i]; 

 

So that if i can see the code, I hope, I will be alittle understand, how to do it. Please kindly provide code for me. Thank you.

 

With Regards,

 

 

 

Link to comment
Share on other sites

<?php

// This is whatever number you want to use for the starting part of your code.  If you want the first number to be 42, then set $i as that.
$i=1;


// A foreach loop just goes through each value in your array, you're setting each value as $data in the first part

foreach($array as $data){
     
     /* Here you are simply displaying the value for $i (The first one being 1) followed by a period, and a space.  In PHP we call this contencation (where you combine variables, strings, etc)  To do this, you use a period to join things together.  For example if you wanted to display 1test it would be echo $i.$data since you want 1. Test you want to combine a string that has a period and a space.  You need to use double quotes to achieve this. */
     
     echo $i.". ".$data; 

     // Since we're looping through data, and you want your counter to increment...you want this in the foreach loop as well
     $i++;
     //  That is just shorthand for $i + 1


}

?>

 

Let me know if that works for ya =)

 

Link to comment
Share on other sites

Hi Ken,

 

Thanks for your help. That one is really effective. But I want to do is with, row , so that, <li> doesn't really work on me. Can you tell me with looping method like nafestski suggestion. Thanks for your help

 

 

Hi nafetski,

 

I tried your one and I got this error, can you please take a look for me ? Thanks for your help. I will be waiting.

 

Warning: Invalid argument supplied for foreach()

Link to comment
Share on other sites

Putting it all simply:

$i = 0;

while ($i != 5){
echo $i.')';
$i++;
}

 

would display

1)

2)

3)

4)

5)

 

 

 

Thanks marklarah. I will try to do it.

 

another way is jusy to have an auto-incrementing value in your table, and just echo that.

 

Is that auto incrementing value mean, is that with database ? or Just dipslay only ? If that's display only, can you provide me that way also ? So that, I can learn it. Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.