pakenney38 Posted November 16, 2006 Share Posted November 16, 2006 Can anyone tell me if the following would work?What I want to do is create x $date variables ($date1, $date2, $date3...) where x is equal $rowcount. Assume the value of $rowcount has already been declared.[code]for ($x = 1; $x <= $rowcount; $x++){$date$x = $_POST['date$x'];}[/code]The part I am mostly concerned with is the $date$x part. I've tried this in the past with mixed results and can't remember how I got it to work, or if I even did. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 16, 2006 Share Posted November 16, 2006 This will be much more easier to manipulate and nicer using an array:[code]<?php$date = array();for ($x = 1; $x <= $rowcount; $x++){$date[] = $_POST['date'.$x];}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted November 16, 2006 Author Share Posted November 16, 2006 So the output of this would be like $date[1], $date[2], $date[3], etc? Kind of goes against the field numbering in the related form (date1, date2, date3), but I guess it will work. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 16, 2006 Share Posted November 16, 2006 You can do what you want with:[code]<?phpfor($i = 1; $i <= $rowcount; $i++){ ${'date' . $i} = $_POST['date' . $i];}?>[/code]However, I [b]highly[/b] recommend not doing it this way and using an array as Orio suggested. Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted November 16, 2006 Author Share Posted November 16, 2006 I will use the array. What is it about the array that makes it better? I would like to know just so I can avoid poor programming in the future. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 16, 2006 Share Posted November 16, 2006 [quote author=pakenney38 link=topic=115231.msg469112#msg469112 date=1163706367]So the output of this would be like $date[1], $date[2], $date[3], etc? Kind of goes against the field numbering in the related form (date1, date2, date3), but I guess it will work.[/quote]The first key in $date will be 0. So you'll have $date[0], $date[1], $date[2], etc'.[quote author=pakenney38 link=topic=115231.msg469125#msg469125 date=1163706901]I will use the array. What is it about the array that makes it better? I would like to know just so I can avoid poor programming in the future.[/quote]First, arrays are much easier to manipulate. You can use the count() function, the sorting functions etc'. When you use variables, you dont know how many you are going to have, so when you want to print/manipulate the dates, you will have a hard time doing that.Second, it'll be much easier to understand the script and modify it in the future when everything is nicer and clearer- and arrays are more easy to understand compared to varibles variables.Orio. Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted November 16, 2006 Author Share Posted November 16, 2006 [quote author=Orio link=topic=115231.msg469146#msg469146 date=1163707424]The first key in $date will be 0. So you'll have $date[0], $date[1], $date[2], etc'.[/quote]Could I use:[code]$date[x] = $_POST['date'.$x];[/code]So that it starts with $date[1]?Then when I come along and say:[code]<input name='date1' type='text' id='date1' value='$date[1]' />[/code]The numbering matches with the field name? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 16, 2006 Share Posted November 16, 2006 You have to use $date[[b]$[/b]x] = ... Notice the $ sign you had left out.Here is why the arrays are easier, you could output all of your inputs like so:[code]<?phpif(count($date)){ // $date array has members in it foreach($date as $key => $val){ echo '<input name="date' . $key . '" type="text" id="date' . $key . '" value="' . $val . '" />'; }}?>[/code][size=10pt][/size] 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.