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>" Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/ 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? Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176380 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>"; Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176382 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. Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176385 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>" Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176387 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. Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176391 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>"; } Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176394 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. Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176397 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)). Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176398 Share on other sites More sharing options...
sayedsohail Posted February 3, 2007 Author Share Posted February 3, 2007 Thanks a million. Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176400 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>"; Link to comment https://forums.phpfreaks.com/topic/36961-solved-echo-ifrowname-rowname-error/#findComment-176524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.