johnber Posted September 30, 2017 Share Posted September 30, 2017 Hi Im quite new to php but have managed to create a page which reads records from a mysql database and present them in a table one of the fields in each record is an image filename and I can get it to display the image - not every record has an image and so currently it will just show a broken image so I assume and if then else statement will fix this as if there is no image i can say No Image Available my code looks like this echo "<tr> <td>{$row['event_id']}</td> <td>{$row['user_ID']}</td> <td>{$row['Lat']}</td> <td>{$row['long']}</td> <td>{$row['source']}</td> <td>{$row['Time']}</td> <td>{$row['Duration']}</td> <td>{$row['snratio']}</td> <td>{$row['adjfreq']}</td> <td>{$row['doppler_Hz']}</td> <td><a href='/mimages/{$row['image']}' target='_blank'><img src='/mimages/{$row['image']}' width='100' height='100'></a></td>"; echo "</tr>"; so its all around the last last line - pointers really appreciated John Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/ Share on other sites More sharing options...
requinix Posted September 30, 2017 Share Posted September 30, 2017 (edited) You can't put an if/else inside a string so that means you have to end the one you've built up early. echo "<tr> <td>{$row['event_id']}</td> <td>{$row['user_ID']}</td> <td>{$row['Lat']}</td> <td>{$row['long']}</td> <td>{$row['source']}</td> <td>{$row['Time']}</td> <td>{$row['Duration']}</td> <td>{$row['snratio']}</td> <td>{$row['adjfreq']}</td> <td>{$row['doppler_Hz']}</td> <td>";Then add in an if/else to output the image if one is present. Tip: you don't need an else. Then finish the row. echo "</td></tr>"; Edited September 30, 2017 by requinix oh wait, you want an alternate message Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/#findComment-1552160 Share on other sites More sharing options...
johnber Posted September 30, 2017 Author Share Posted September 30, 2017 im close after getting a few syntax issues I now have this <td>{$row['doppler_Hz']}</td>"; echo "<td>"; if (is_null.$row['image']) echo "No data!"; else echo "<td>{$row['image']}</td>"; echo "</td></tr>"; However it shows No data even if there is data, so this statement if (is_null.$row['image']) is wrong and its probably my understanding of null \ empty ? John B Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/#findComment-1552161 Share on other sites More sharing options...
requinix Posted September 30, 2017 Share Posted September 30, 2017 is_null is a function like any other so you need to use it as such. And apparently you don't have your environment set up properly for development. Find your php.ini, find and change two settings display_errors = on error_reporting = -1then restart Apache/nginx. Try your code as it is now without the is_null fix, see what PHP says, then make the change. Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/#findComment-1552162 Share on other sites More sharing options...
Solution johnber Posted September 30, 2017 Author Solution Share Posted September 30, 2017 (edited) learning a lot so error reporting now on and after a few reported syntax errors which I fixed I have echo "<td>"; if ($row['image']) echo "{$row['image']}</td>"; else echo "No data!"; echo "</td></tr>"; which works a treat - thanks for all the help. John Edited October 3, 2017 by cyberRobot Please enclose code with [code][/code] tags Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/#findComment-1552163 Share on other sites More sharing options...
ginerjm Posted September 30, 2017 Share Posted September 30, 2017 I know people love to use the nuances of PHP's true/false capabilities. But - especially since you are new - try and write Meaningful Code instead of these things you have to think about when seeing them. if ($row['image'] == '') is much more direct than the line you copied from somewhere. Or even use the is_null function that you mis-used earlier. Lines like that are so much clearer when reviewing lots of code and not having to stop and think about them. Quote Link to comment https://forums.phpfreaks.com/topic/305163-if-then-else-help-please/#findComment-1552164 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.