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 = ""; } ?> Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/ 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" Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122765 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. Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122767 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. Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122780 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 Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122782 Share on other sites More sharing options...
unistake Posted October 16, 2010 Author Share Posted October 16, 2010 anyone? Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122804 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? Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122806 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 Link to comment https://forums.phpfreaks.com/topic/216019-adding-an-echo-to-a-variable/#findComment-1122813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.