apaunganhote Posted March 21, 2008 Share Posted March 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
samshel Posted March 21, 2008 Share Posted March 21, 2008 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> Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 21, 2008 Author Share Posted March 21, 2008 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. 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. Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 21, 2008 Author Share Posted March 21, 2008 any one help please ? ??? Quote Link to comment Share on other sites More sharing options...
nafetski Posted March 21, 2008 Share Posted March 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 21, 2008 Author Share Posted March 21, 2008 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, Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 21, 2008 Share Posted March 21, 2008 You can use an HTML ordered list: <?php $arr = array('one','two','three','four'); echo "<ol>\n"; foreach ($arr as $v) echo "<li>$v</li>\n"; echo "</ol>\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
nafetski Posted March 21, 2008 Share Posted March 21, 2008 <?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 =) Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 22, 2008 Author Share Posted March 22, 2008 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() Quote Link to comment Share on other sites More sharing options...
marklarah Posted March 22, 2008 Share Posted March 22, 2008 Putting it all simply: $i = 0; while ($i != 5){ echo $i.')'; $i++; } would display 1) 2) 3) 4) 5) Quote Link to comment Share on other sites More sharing options...
marklarah Posted March 22, 2008 Share Posted March 22, 2008 another way is jusy to have an auto-incrementing value in your table, and just echo that. Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 22, 2008 Author Share Posted March 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
marklarah Posted March 23, 2008 Share Posted March 23, 2008 Well im assumming that you would want to loop (other than for learning) for databases. In each table, you would most likely have an ID, so you can just echo the ID. The only problem being, If you delete a row, it gets messy. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2008 Share Posted March 23, 2008 And if the query uses something like "WHERE fname = 'fred'" then the IDs could be 23, 56, 1258, 29555 Quote Link to comment Share on other sites More sharing options...
marklarah Posted March 23, 2008 Share Posted March 23, 2008 Yeah. But also, if you delete a row, then its messy to replace it where it should be. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2008 Share Posted March 23, 2008 another way is jusy to have an auto-incrementing value in your table, and just echo that. So I guess we are agreed that this isn't a viable option Quote Link to comment Share on other sites More sharing options...
apaunganhote Posted March 23, 2008 Author Share Posted March 23, 2008 Thanks for all of your help and great instructions to me. I really got some knowledges for you all. Thanks for everything. With Regards, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.