sayedsohail Posted February 3, 2007 Share Posted February 3, 2007 hi, Any idea why i am getting error? here is my code: echo "<td>if($row[name] == "","-",$row[name])</td>" Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 3, 2007 Share Posted February 3, 2007 1. You can't put an if statement within an echo statement. 2. You can't put an if statement in double quotes unless you wish to echo the if statement itself. 3. Your if statement is very malformed, what are you trying to do with it? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 3, 2007 Share Posted February 3, 2007 You are putting PHP coed within the string you are trying to echo. You can do that with variables, butnot whole code. Plus your code is not formatted correctly. echo "<td>" . (($row[name]=="")?"XXX":$row[name]) . "</td>"; Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 3, 2007 Author Share Posted February 3, 2007 Basically i want to add '-' if the field is empty, in the table column. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 3, 2007 Share Posted February 3, 2007 if(!$row[name]){ $foo = '-'; }else{ $foo = $row[name]; } echo "<td>$foo</td>" Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 3, 2007 Author Share Posted February 3, 2007 thanks, but how could i check all my fields in while loop to print '-' if any of the fields are empty. since the earlier method is quite heavy for the processing time if i place if condition on every field. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 3, 2007 Share Posted February 3, 2007 Make an array of your fieldnames and loop. $foos = array('name', 'address', 'phone'); foreach($foos AS $bar){ if(!$row[$bar]){ $foo = '-'; }else{ $foo = $row[$bar]; } print "<td>$foo</td>"; } Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 3, 2007 Author Share Posted February 3, 2007 if empty($name){print"<td>-</td>";}else{print"<td>$name</td>";} I don't know what's wrong with this statement. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 3, 2007 Share Posted February 3, 2007 You need to do if(empty($name)). Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 3, 2007 Author Share Posted February 3, 2007 Thanks a million. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 4, 2007 Share Posted February 4, 2007 You can do it with this one liner: echo "<td>" . (empty($name)?"XXX":$row[name]) . "</td>"; 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.