Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
increment variables in a loop
Old 05-06-2005, 07:14 AM increment variables in a loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Hi again.

would anyone explain to me how/ if i can set a loop that will increment the variables in it by one with each revolution?

for example: id like the first loop to read from $_SESSION[apwidth1] but the 2nd loop to read from $_SESSION[apwidth2]


1st time:

for(1;$numbeds;$i++)
{
$_SESSION[apwidth1]
}

2nd time:

for(1;$numbeds;$i++)
{
$_SESSION[apwidth2]
}


Obviously my loop will be a little more sophisticated, its just an example.


To put you in context im designing a website for a sliding wardrobe door company. it will enable customers to enter an aperture width/height, and the php will decide how many doors of what size they require. then the customer will choose a colour and the php will look to mysql for the price of however many doors of what size in their chosen colour and output a total price. I finished the php/ mysql section yesterday and ive been informed today that most customers have more than 1 bedroom and therefore need more than 1 set of doors (up to 4), so without duplicating my php 4 times (which would be a very long haul) id like to set a loop that rotates for $numbeds(number of bedrooms) and increments the variablenames involved within it by 1 each time so when it comes to the end and the total price of each bedroom is calculated i will end up with bed1price +bed2price +bed3price, as opposed to bed1price + bed1price +bed1price.... Am i asking too much?????

Thanks guys
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-06-2005, 07:20 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
for($i = 1; $i < $numbeds; $i++)
{
$_SESSION["apwidth$i"];
}
This should do the trick.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-06-2005, 07:24 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
excellent... it's that simple. nice work
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-07-2005, 01:12 PM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
It was that simple until i wanted to make another loop to show the results, how can i make this loop work any ideas?

PHP Code:
for($i 0$i $_SESSION[numbed]; $i++)
{

echo
"   bedroom$i aperture is
 
$_POST[apwidth$i] mm wide x $_POST[apheight$i] mm high"
The error i get says expecting ] on the line which starts $_POST[apwidth$i]. Ive tried putting ' and " tags all over the place, basically what im asking is how can i make the php read $i if its enclosed in []?

above is the results page of the form shown below.

PHP Code:

<?php
session_start
();

//$_SESSION[numbed] refers to the previous page where the customer would've entered the number of bedrooms they want wardrobes for.

$_SESSION[numbed]="$_POST[numbed]"

{
 echo
    
" Enter your apperture size/s in mm<br>";
}
for(
$i 0$i $_SESSION[numbed]; $i++)

{
echo
"
      <form action='doorselector.php' 
            method='post'>\n
Bedroom 
$i
      <input type='text' name='apwidth
$i' size='4' maxlength='4'>\n
 wide x
      <input type='text' name='apheight
$i' size='4' maxlength='4'>\n
 high <br><br>\n"
;
}
echo
"
<input type='submit' value='send'>\n
      </form>\n"
;
?>
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-07-2005, 01:56 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
PHP will only expand variables out if they are in quotes. It might do this for the first one ($_POST) but then the inner variable confuses it.
Generally when doing this with arrays I play it safe and break the string up:

PHP Code:
echo"   bedroom$i aperture is ".$_POST["apwidth$i"]." mm wide x ".$_POST["apheight$i"]." mm high"
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 05-07-2005, 01:59 PM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
excellent. thanks very much oberon it works fine now
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to increment variables in a loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.99891 seconds with 12 queries