for each syntax [message #169597] |
Sat, 18 September 2010 20:02  |
GarryJones
Messages: 21 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
I need to automatically write a few lines of code with php.
This is the output i need
if ($_POST['resmalval109'] == 109) {
$resmalval=109;
} elseif ($_POST['resmalval110'] == 110) {
$resmalval=110;
} elseif ($_POST['resmalval111'] == 111) {
$resmalval=111;
} elseif ($_POST['resmalval112'] == 112) {
$resmalval=112;
} elseif ($_POST['resmalval113'] == 113) {
$resmalval=113;
} elseif ($_POST['resmalval114'] == 114) {
$resmalval=114;
} elseif ($_POST['resmalval115'] == 115) {
$resmalval=115;
} else {
// something else
}
The required numbers just happen to be sequential at the moment, but
over time these values will change, Instead of manually writing the
above code every time I want to create it with php.
I have the required numbers in an array.
$arr_single = array(109,110,111,112,113,114,115);
So my guess is using "for each" I can write in each number where it is
needed until the array is empty. I can't use the $var++ trick as these
numbers can (for example) change to
$arr_single = array(109,111,112,113,115);
Any help in right direction greatly appreciated.,,
|
|
|
Re: for each syntax [message #169602 is a reply to message #169597] |
Sat, 18 September 2010 20:19   |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 18-09-10 22:02, GarryJones wrote:
> I need to automatically write a few lines of code with php.
>
> This is the output i need
> if ($_POST['resmalval109'] == 109) {
> $resmalval=109;
> } elseif ($_POST['resmalval110'] == 110) {
> $resmalval=110;
> } elseif ($_POST['resmalval111'] == 111) {
> $resmalval=111;
> } elseif ($_POST['resmalval112'] == 112) {
> $resmalval=112;
> } elseif ($_POST['resmalval113'] == 113) {
> $resmalval=113;
> } elseif ($_POST['resmalval114'] == 114) {
> $resmalval=114;
> } elseif ($_POST['resmalval115'] == 115) {
> $resmalval=115;
> } else {
> // something else
> }
>
> The required numbers just happen to be sequential at the moment, but
> over time these values will change, Instead of manually writing the
> above code every time I want to create it with php.
>
> I have the required numbers in an array.
> $arr_single = array(109,110,111,112,113,114,115);
>
> So my guess is using "for each" I can write in each number where it is
> needed until the array is empty. I can't use the $var++ trick as these
> numbers can (for example) change to
>
> $arr_single = array(109,111,112,113,115);
>
> Any help in right direction greatly appreciated.,,
>
<?php
$arr_single = array(109,110,111,112,113,114,115);
for ($x=0; $x<sizeof($arr_single); $x++) {
echo "x:$x e:".$arr_single[$x]."<br>";
}
?>
--
Luuk
|
|
|
Re: for each syntax [message #169604 is a reply to message #169597] |
Sat, 18 September 2010 20:21   |
spambait
Messages: 35 Registered: September 2010
Karma: 0
|
Member |
|
|
In article <6a86e2b1-a632-4415-9d1b-608bf205ffd9(at)k30g2000vbn(dot)googlegroups(dot)com>, GarryJones <morack(at)algonet(dot)se> wrote:
> I need to automatically write a few lines of code with php.
>
> This is the output i need
> if ($_POST['resmalval109'] == 109) {
> $resmalval=109;
> } elseif ($_POST['resmalval110'] == 110) {
> $resmalval=110;
> } elseif ($_POST['resmalval111'] == 111) {
> $resmalval=111;
> } elseif ($_POST['resmalval112'] == 112) {
> $resmalval=112;
> } elseif ($_POST['resmalval113'] == 113) {
> $resmalval=113;
> } elseif ($_POST['resmalval114'] == 114) {
> $resmalval=114;
> } elseif ($_POST['resmalval115'] == 115) {
> $resmalval=115;
> } else {
> // something else
> }
>
> The required numbers just happen to be sequential at the moment, but
> over time these values will change, Instead of manually writing the
> above code every time I want to create it with php.
>
> I have the required numbers in an array.
> $arr_single = array(109,110,111,112,113,114,115);
>
> So my guess is using "for each" I can write in each number where it is
> needed until the array is empty. I can't use the $var++ trick as these
> numbers can (for example) change to
>
> $arr_single = array(109,111,112,113,115);
>
> Any help in right direction greatly appreciated.,,
>
Perhaps if you'd describe what it is you're actually trying to -- not the
method you've chosen to try to do it -- we could be of more help. Please give
a general description of the task. There may be other, much simpler, methods
of getting it done.
|
|
|
Re: for each syntax [message #169605 is a reply to message #169597] |
Sat, 18 September 2010 20:28   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/18/2010 4:02 PM, GarryJones wrote:
> I need to automatically write a few lines of code with php.
>
> This is the output i need
> if ($_POST['resmalval109'] == 109) {
> $resmalval=109;
> } elseif ($_POST['resmalval110'] == 110) {
> $resmalval=110;
> } elseif ($_POST['resmalval111'] == 111) {
> $resmalval=111;
> } elseif ($_POST['resmalval112'] == 112) {
> $resmalval=112;
> } elseif ($_POST['resmalval113'] == 113) {
> $resmalval=113;
> } elseif ($_POST['resmalval114'] == 114) {
> $resmalval=114;
> } elseif ($_POST['resmalval115'] == 115) {
> $resmalval=115;
> } else {
> // something else
> }
>
> The required numbers just happen to be sequential at the moment, but
> over time these values will change, Instead of manually writing the
> above code every time I want to create it with php.
>
> I have the required numbers in an array.
> $arr_single = array(109,110,111,112,113,114,115);
>
> So my guess is using "for each" I can write in each number where it is
> needed until the array is empty. I can't use the $var++ trick as these
> numbers can (for example) change to
>
> $arr_single = array(109,111,112,113,115);
>
> Any help in right direction greatly appreciated.,,
>
I agree with Doug. Any time I see something like this, I know someone
is trying to force something which is easy.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: for each syntax [message #169610 is a reply to message #169605] |
Sat, 18 September 2010 23:17   |
spambait
Messages: 35 Registered: September 2010
Karma: 0
|
Member |
|
|
In article <i737cr$a2i$1(at)news(dot)eternal-september(dot)org>, Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
> On 9/18/2010 4:02 PM, GarryJones wrote:
>> I need to automatically write a few lines of code with php.
>>
>> This is the output i need
>> if ($_POST['resmalval109'] == 109) {
>> $resmalval=109;
>> } elseif ($_POST['resmalval110'] == 110) {
>> $resmalval=110;
>> } elseif ($_POST['resmalval111'] == 111) {
>> $resmalval=111;
>> } elseif ($_POST['resmalval112'] == 112) {
>> $resmalval=112;
>> } elseif ($_POST['resmalval113'] == 113) {
>> $resmalval=113;
>> } elseif ($_POST['resmalval114'] == 114) {
>> $resmalval=114;
>> } elseif ($_POST['resmalval115'] == 115) {
>> $resmalval=115;
>> } else {
>> // something else
>> }
>>
>> The required numbers just happen to be sequential at the moment, but
>> over time these values will change, Instead of manually writing the
>> above code every time I want to create it with php.
>>
>> I have the required numbers in an array.
>> $arr_single = array(109,110,111,112,113,114,115);
>>
>> So my guess is using "for each" I can write in each number where it is
>> needed until the array is empty. I can't use the $var++ trick as these
>> numbers can (for example) change to
>>
>> $arr_single = array(109,111,112,113,115);
>>
>> Any help in right direction greatly appreciated.,,
>>
>
> I agree with Doug. Any time I see something like this, I know someone
> is trying to force something which is easy.
>
I don't see it quite that way, Jerry -- more that he's latched on to one
particular method of solving a problem, and is determined to solve the problem
using that method no matter what. Sometimes one needs to step back and think
"What am I really trying to accomplish here?". Without doing so, it's not
possible to consider alternative methods of solving the problem.
|
|
|
Re: for each syntax [message #169611 is a reply to message #169610] |
Sat, 18 September 2010 23:55   |
GarryJones
Messages: 21 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
On Sep 19, 1:17 am, spamb...@milmac.com (Doug Miller) wrote:
> Sometimes one needs to step back and think
> "What am I really trying to accomplish here?". Without doing so, it's not
> possible to consider alternative methods of solving the problem.
I have a booking site where users can click on a trip.
On the booking choice page there is a <tr><td> for each trip with a
description pulled in from info on a mysql server dynamicaly
To create the correct <tr><td>'s all I have to do is to add or remove
an id number from the array. If the number is there it will create the
<td> with the correct info.
ie if the array is 111,112,113,114 then four <tr><td> are
I place an ok box at the end of each <tr>
To do this I make each <tr><td> its own unique form.
Each ok box submits the form.
To cut a corner I make each ok box redirect to the same page.
The example code for 1 of these <tr> is
<tr>
<td width="650" valign="top" class="resmallefty">Chase The
Sun<br>2010-10-02 -> 2010-10-12<br>9 400:-</td>
<td width="150" valign="middle" class="barstyle">
<FORM action="boksteg2.php" method="post" name="steg1109"
id="steg1109" ><input name="resmalval109" type="hidden" value="109">
<input type="Submit" value = "Boka NU">
</FORM>
</td>
</tr>
Every <tr> has a form with a unique name.
To create the above code I use this code for each value in the array
(varid can be 109,110,111, etc as explained above
---------------------------------------------------------------------
<?
print '<tr><td >' . $var1 . '<br>';
print $var2 . " -> " . $var3 . "<br>" . $var3 . ":-";
echo '</td>';
?>
<td>
<FORM action="boksteg2.php" method="post" name="steg1<?= $varid ?>"
id="steg1<?= $varid?>" ><input name="resmalval<?= $varid?>"
type="hidden" value="<?= $varid?>">
<input type="Submit" value = "Boka NU">
</FORM>
</td>
</tr>
----------------------------------------------------------------------
As you can see the name of the form I am sending to is identical for
all OK buttons
On that page I need to set a value according to which ok button the
user clicked on.
if ($_POST['resmalval109'] == 109) {
$resmalval=109;
} elseif ($_POST['resmalval110'] == 110) {
$resmalval=110;
etc until all value found
So if the user has clicked on the ok button for the form with the
input name "resmalval109" the value I need to read in is 109
if ($_POST['resmalval109'] == 109) {
Gives yes, the input name resmalval109 is set to 109 because 109 is
read in by the post. It then does not need to check for 110, 111 etc,
it bypasses the rest of the if and I can now use my value to do what I
need to do.
When these id values change it would be nice if I don't have to edit
the long-winded code, so I thought I could use "for each" and write
the else /elseifs for each value in the array
I may well be going about this the wrong way, but dont have more
knowledge than that I pocess today.
So any help appreciated
Garry Jones
Sweden
|
|
|
Re: for each syntax [message #169612 is a reply to message #169610] |
Sun, 19 September 2010 00:33   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/18/2010 7:17 PM, Doug Miller wrote:
> In article<i737cr$a2i$1(at)news(dot)eternal-september(dot)org>, Jerry Stuckle<jstucklex(at)attglobal(dot)net> wrote:
>> On 9/18/2010 4:02 PM, GarryJones wrote:
>>> I need to automatically write a few lines of code with php.
>>>
>>> This is the output i need
>>> if ($_POST['resmalval109'] == 109) {
>>> $resmalval=109;
>>> } elseif ($_POST['resmalval110'] == 110) {
>>> $resmalval=110;
>>> } elseif ($_POST['resmalval111'] == 111) {
>>> $resmalval=111;
>>> } elseif ($_POST['resmalval112'] == 112) {
>>> $resmalval=112;
>>> } elseif ($_POST['resmalval113'] == 113) {
>>> $resmalval=113;
>>> } elseif ($_POST['resmalval114'] == 114) {
>>> $resmalval=114;
>>> } elseif ($_POST['resmalval115'] == 115) {
>>> $resmalval=115;
>>> } else {
>>> // something else
>>> }
>>>
>>> The required numbers just happen to be sequential at the moment, but
>>> over time these values will change, Instead of manually writing the
>>> above code every time I want to create it with php.
>>>
>>> I have the required numbers in an array.
>>> $arr_single = array(109,110,111,112,113,114,115);
>>>
>>> So my guess is using "for each" I can write in each number where it is
>>> needed until the array is empty. I can't use the $var++ trick as these
>>> numbers can (for example) change to
>>>
>>> $arr_single = array(109,111,112,113,115);
>>>
>>> Any help in right direction greatly appreciated.,,
>>>
>>
>> I agree with Doug. Any time I see something like this, I know someone
>> is trying to force something which is easy.
>>
> I don't see it quite that way, Jerry -- more that he's latched on to one
> particular method of solving a problem, and is determined to solve the problem
> using that method no matter what. Sometimes one needs to step back and think
> "What am I really trying to accomplish here?". Without doing so, it's not
> possible to consider alternative methods of solving the problem.
Which is the same as what I was saying - just different words.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: for each syntax [message #169613 is a reply to message #169611] |
Sun, 19 September 2010 00:35   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/18/2010 7:55 PM, GarryJones wrote:
> On Sep 19, 1:17 am, spamb...@milmac.com (Doug Miller) wrote:
>> Sometimes one needs to step back and think
>> "What am I really trying to accomplish here?". Without doing so, it's not
>> possible to consider alternative methods of solving the problem.
>
> I have a booking site where users can click on a trip.
>
> On the booking choice page there is a<tr><td> for each trip with a
> description pulled in from info on a mysql server dynamicaly
> To create the correct<tr><td>'s all I have to do is to add or remove
> an id number from the array. If the number is there it will create the
> <td> with the correct info.
>
> ie if the array is 111,112,113,114 then four<tr><td> are
> I place an ok box at the end of each<tr>
>
> To do this I make each<tr><td> its own unique form.
> Each ok box submits the form.
> To cut a corner I make each ok box redirect to the same page.
>
> The example code for 1 of these<tr> is
>
> <tr>
> <td width="650" valign="top" class="resmallefty">Chase The
> Sun<br>2010-10-02 -> 2010-10-12<br>9 400:-</td>
> <td width="150" valign="middle" class="barstyle">
> <FORM action="boksteg2.php" method="post" name="steg1109"
> id="steg1109"><input name="resmalval109" type="hidden" value="109">
> <input type="Submit" value = "Boka NU">
> </FORM>
> </td>
> </tr>
>
> Every<tr> has a form with a unique name.
>
> To create the above code I use this code for each value in the array
> (varid can be 109,110,111, etc as explained above
> ---------------------------------------------------------------------
> <?
> print '<tr><td>' . $var1 .'<br>';
> print $var2 . " -> " . $var3 ."<br>" . $var3 . ":-";
> echo '</td>';
> ?>
>
> <td>
> <FORM action="boksteg2.php" method="post" name="steg1<?= $varid ?>"
> id="steg1<?= $varid?>"><input name="resmalval<?= $varid?>"
> type="hidden" value="<?= $varid?>">
> <input type="Submit" value = "Boka NU">
> </FORM>
> </td>
> </tr>
> ----------------------------------------------------------------------
>
> As you can see the name of the form I am sending to is identical for
> all OK buttons
>
> On that page I need to set a value according to which ok button the
> user clicked on.
>
> if ($_POST['resmalval109'] == 109) {
> $resmalval=109;
> } elseif ($_POST['resmalval110'] == 110) {
> $resmalval=110;
> etc until all value found
>
> So if the user has clicked on the ok button for the form with the
> input name "resmalval109" the value I need to read in is 109
>
> if ($_POST['resmalval109'] == 109) {
>
> Gives yes, the input name resmalval109 is set to 109 because 109 is
> read in by the post. It then does not need to check for 110, 111 etc,
> it bypasses the rest of the if and I can now use my value to do what I
> need to do.
>
> When these id values change it would be nice if I don't have to edit
> the long-winded code, so I thought I could use "for each" and write
> the else /elseifs for each value in the array
>
> I may well be going about this the wrong way, but dont have more
> knowledge than that I pocess today.
>
>
> So any help appreciated
>
> Garry Jones
> Sweden
Why are you using different id's for values in every form? The browser
will only submit one form at a time.
I suggest you start with alt.html and get an understanding on how HTML
works. Your PHP will then be much easier.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
|
|
|
|
|
|
Re: for each syntax [message #169629 is a reply to message #169616] |
Mon, 20 September 2010 09:49  |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/19/2010 4:52 AM, Michael Fesser wrote:
> .oO(GarryJones)
>
<snip>
>> <tr><td>Trip 1 name and details</td>
>> <td width="150" valign="middle" class="barstyle">
>> <FORM action="boksteg2.php" method="post" name="steg110" id="steg110"
>>> <input name="resmalval111" type="hidden" value="110">
>> <input type="Submit" value = "Boka NU">
>> </FORM>
>> </td></tr>
>
> BTW: Forms don't have names. You can remove it from the 'form' tag.
Forms can have names in HTML4 specification.
(But ID's are preferred.)
http://www.w3.org/TR/html401/interact/forms.html#h-17.3
name = cdata [CI]
This attribute names the element so that it may be referred to from
style sheets or scripts. Note. This attribute has been included for
backwards compatibility. Applications should use the id attribute to
identify elements.
(Appart from that I totally agree to your suggested approach.)
Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
|
|
|