unistake Posted October 16, 2010 Share Posted October 16, 2010 Hi all, I am trying to add the echo " AND "; below to the variable $model if the $_POST['purchasetype'] has a value. how can you add echos within IF statements to a variable, if at all possible? Many thanks, <?php if ($_POST[model] !="") { $model = "model='". $_POST['model'] ."'"; if ($_POST['purchasetype'] !="") { echo " AND "; } } else { $model = ""; } ?> Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 16, 2010 Share Posted October 16, 2010 you need to explain more. you aren't echoing anything besides "AND" Quote Link to comment Share on other sites More sharing options...
unistake Posted October 16, 2010 Author Share Posted October 16, 2010 I am just trying to add " AND "; to the variable $model ONLY if $_POST['purchasetype'] has a value. I would then use the $model further down the page. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 16, 2010 Share Posted October 16, 2010 What do you want the output to be? model=string"AND"purchasetype=string model=stringANDpurchasetype=string model=string AND purchasetype=string Regards, Paul. Quote Link to comment Share on other sites More sharing options...
unistake Posted October 16, 2010 Author Share Posted October 16, 2010 Ok, my script basically auto generates a variable depending on what details the user enters and could end up like this: $sql = "SELECT * FROM table WHERE email='$_POST' AND model='$_POST[model]' AND"; Of course for a $sql I can not finish the above line with a ...AND"; Is there a way of clipping the last ...AND" off the variable using rtrim for example? - This would solve the problem in a much easier way. Thanks Quote Link to comment Share on other sites More sharing options...
unistake Posted October 16, 2010 Author Share Posted October 16, 2010 anyone? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 16, 2010 Share Posted October 16, 2010 What happened when you tried using rtrim()? Did it not work? Quote Link to comment Share on other sites More sharing options...
unistake Posted October 16, 2010 Author Share Posted October 16, 2010 Just got it to work with rtrim()! What a long day lol $sql1 = "SELECT * FROM sales WHERE $category $manufacturer $model $purchasetype"; $sql = rtrim($sql1, ' AND '); and it looks so simples Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.