Jump to content

Weird variable declaration. will this work?


pakenney38

Recommended Posts

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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

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]<?php
if(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]
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.