For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > May 2005 > variable variables problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author variable variables problem
Rob O'Brien

2005-05-05, 3:58 am

I am struggling with the usage of variable variables.

www.bobobrien.co.uk/ebiz/screen1.php

I am developing a simple system that enables a user to:
1. select a car based on type (saloon/estate) and fuel type (diesel/petrol)
2. then display the appropriate cars from the database and their trade price
3. allow u to select a car and view its details and discounted price
4. Allow you to click a link to add extra items to your purchase
5. Allow you to select any number of extra items and recalculate total cost
6. Finally the recalculated price is shown on a final screen, with a
discount also applied to the extra parts trade prices.

As you can see from my example, I get as far as choosing extra parts, and it
is carrying the carId over so you know which car theyve requested. But I
cant master variable variables to get the final screen.

I can post my code if required.

I've been given this code as a hint but cant seem to adapt it, and not sure
which php file i should put it in:


Rob O'Brien

2005-05-05, 3:58 am

that sample code:

Passing variables onto other web pages.

If we want to pass variables onto other web pages that are not input by a
user then the easiest way to do this is by using the hidden HTML input type
variable.

<input type="hidden" name = "cost" value = <?php echo($price) ?> >


Using Variables Variables

Often when testing for true and false variables passed from another program
we may not know how many variables to test for in a loop. We can get over
this problem by creating variables dynamically, only assigning the variables
we need.



// Test each radio selection for accessories using variable variables
(dynamic variables)

while($count<=4){
$var = "radio" . $count;
if(empty($$var)){
$$var=0;
}else{
$select = $$var;
$result = mysql_query("SELECT * FROM accessories WHERE ID = $select");
$row = mysql_fetch_array($result);
$accost = $row["PRICE"] ;
$totalcost = $totalcost + $accost;
}

$count++;
}

Here we assign a variable name to $var. Each time we go round this loop $var
is assigned radio1, radio2, radio3, radio4. $$var means assign to me the
value of $var e.g whatever radio1, radio2, radio3 etc contains. If radio1,
radio2 etc have been sent by a previous program then this code will test all
the radio buttons that have been selected i.e. true. Any false ones, not
selected, will not be sent so we need to trap these or we will get an error
message

if(empty($$var)){
$$var=0;
}

This piece of code tests the value of what $$var contains and sets it to 0
if it does not exist i.e it is empty.


Rob O'Brien

2005-05-05, 3:58 am

My code for screen4.php (where the parts are listed):

I suspect I've got to not produce so mant diff radio button variables, but
am not sure how to integrate the idea of variable variables

echo "Car ID: $carId";

//Make the query
$query = "SELECT partName, partMRP, partId FROM mgPARTS";

$result = @mysql_query ($query); //run the query
if ($result) { //if it ran ok, display the records

----------------

Then I echo form action="screen5.php" and setup a table with column headings

--------------

//Fetch and print all records
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$partId=$row[2];
$partName=$row[0];
$partMRP=$row[1];

$part="part".$partId;

-------

Then echo the rest of table with extra part list

$partName , £$partMRP then a radio button: input name=$part value=$partId
type="radio
}

-----------
Then finish form:

input type="hidden" name="carId" value=$carId>
input name="submit" value="Submit" type="submit">

input name="reset" value="Reset" type="reset">";


Rob O'Brien

2005-05-05, 3:58 am

somebody's going to tell me a simple way to write code in a thread now arent
they?!? lol

anyway, screen5.php is simply reporting back values. it only reports if
something has been selected, which is good. but im still lame at getting my
head round this...

echo $part1;
echo $part2;
echo $part3;
echo $carId;

etc

ANY help at all would be magic!!

Thanks so much for putting up with me,

Bob


Oli Filth

2005-05-05, 8:55 am

Rob O'Brien wrote:
> somebody's going to tell me a simple way to write code in a thread now arent
> they?!? lol


Yes! Why have you posted 4 posts?!?!


> anyway, screen5.php is simply reporting back values. it only reports if
> something has been selected, which is good. but im still lame at getting my
> head round this...
>
> echo $part1;
> echo $part2;
> echo $part3;
> echo $carId;
>
> etc
>
> ANY help at all would be magic!!
>


I'm not sure what this has to do with variable variables...

If you want to save variables between multiple PHP scripts, it's far
simpler to use sessions than creating loads of hidden form variables.

http://www.php.net/manual/ref.session.php


P.S. Use radio buttons when you only want the user to select exactly
*one* option (no less, no more). That's what radio buttons mean. Using
lots of separate radio buttons with different name attributes is not an
appropriate use.

If you want the user to be able to select multiple options, use check
boxes (with the name attribute all set to the same thing) or a <SELECT>
drop-down box with the "multiple" attribute set.

--
Oli
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com