I have a database with several tables designating different product categories. Within each table is many rows representing different products. I want to be able to choose product category table dynamically in a conditional statement based on the value of a session variable. I am using php in dreamweaver 8.
Choosing a database table dynamically by the value of a session variable
Collapse
X
-
I also do not really understand the problem, but I don't know Dreamweaver. In pure PHP it would be something like:
[php]
$tabname =$_SESSION['category'];
$sql_stmt="SELE CT * FROM $tabname WHERE etc.......";
[/php]
Or don't I fully understand your problem?
RonaldComment
-
Sorry ... I am fairly new at writing code outside of Dreamweavers built in server behaviors ...
I took your advice and
[code=php]
if ($_SESSION['skey'] = 1)
THEN ($tblname = ('tbl_1'));
elseif ($_SESSION['skey'] = 2)
THEN ($tblname = ('tbl_2'));
else ($tblname = ('tbl_3'));
end if;
[/code]
then do the sql insert into $tblname where key =; etc ...
However, I am getting this error:
Parse error: parse error, unexpected T_ENDIF in /home/content/w/o/o/woodwebsite/html/DeptSpecials/InsertData2.php on line 85
Do I have a syntax problem?
Thanks everyone for your help ... I am getting better!Comment
-
The problem is in your if syntax.
You can read about the correct usage here, at php.net.
Here is a simple example of the correct usage:
[code=php]
<?php
$number = 2;
if($number < 5) {
echo "The number is less than five!";
}
else if($number >5) {
echo "The number is more than five!";
}
else {
echo "The number IS five!!";
}
?>
[/code]Comment
-
Hi,
The IF structure is great when you want to branch the code in two. However, what you are doing is branching the code in many ways - there are more than two tables that could be selected. You would be better off using the SWITCH structure:
[php]
<?php
swith ($_SESSION['skey'])
{
case 1:
$tblname = 'tbl_1' ;
break ;
case 2:
$tblname = 'tbl_2' ;
break ;
case 3:
$tblname = 'tbl_3' ;
break ;
case 4:
$tblname = 'tbl_4' ;
break ;
default:
$tblname = 'tbl_5' ;
break ;
}
?>
[/php]
This would set the variable for you ready for what comes next. It is, as you can see, easier to understand and therefore maintain than a long if elseif... style structure.
Cheers
nathjComment
-
Hey ...
I used the case statement and it worked. I did have to put it right at the top under the first
<?php require_once('. ./Connections/conn_specials.p hp'); ?>
<?php
It worked perfectly, and will provide the key to this whole application ... that's the second time I have gotten a solution from this forum. I am very grateful.
Thank you to Atli, Ronverdonk, and Nathj ...
RobertComment
-
Originally posted by rlm51...... Thank you to Atli, Ronverdonk, and Nathj ...
Robert
RonaldComment
Comment