sean04 Posted July 23, 2010 Share Posted July 23, 2010 Hey, So on my profile page I have a lot of info such as Name, Birthday, City, Country, Status etc... Its set up like this... Name: $loggedInfo[Name]<p/> Birthday: $loggedInfo[birthday]<p/> City: $loggedInfo[City]<p/> Country: $loggedInfo[Country]<p/> Status: $loggedInfo[status]<p/> The problem is is that if the user doesnt fill out this info then there is a blank space on their profile. How can I go about having it so if there is no data for say Country, on their profile there will be a link and it will say "Edit this!" and that link can just go to edit profile. Pretty much I dont want to do this for everything... if($loggedInfo[Name] != "") { echo"$loggedInfo[Name]"; }else{ echo"<a href='edit_profile.php'>edit this!</a>"; } Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/ Share on other sites More sharing options...
gwolgamott Posted July 23, 2010 Share Posted July 23, 2010 Where ever it is you are storing the data and retrieving it from, say you retrieve it all in an array then take that array and foreach through it, if one of the elements is empty fill it with with a string like: '<a href="myedit.html">Edit This</a>' then print all the variables as normal later. Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1090266 Share on other sites More sharing options...
sean04 Posted July 24, 2010 Author Share Posted July 24, 2010 Thanks gwolgamott. Im just getting all my information from a view as I posted some of my code in my previous post. Do you have any ideas of how I can do this with the way im doing things now? Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1090768 Share on other sites More sharing options...
gwolgamott Posted July 28, 2010 Share Posted July 28, 2010 Sorry so long for a reply been sick and sleeping for a number of days... not quite literal but close enough that I never even touch a computer for 4 days. You've already the array with the $loggedInfo //So just loop through that array foreach($loggedInfo as $key => $value) { if($value != "") { echo"$value"; }else{ echo"<a href='edit_profile.php'>edit this!</a>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092270 Share on other sites More sharing options...
sean04 Posted July 28, 2010 Author Share Posted July 28, 2010 Glad to see your better Thanks for the reply! Sorry but im not sure if I understand. Where would I put like name, birthday, city etc..? Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092274 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 Assuming that the $loggedInfo array only contains the fields that you want to display in this particular area you can do something like this: foreach($loggedInfo as $name => $val) { if(!empty($val)) { echo "$name: $val<p/>"; } else { echo "<a href='edit_profile.php'>edit this!</a>"; } } If you want you can also shorten it with the ternary operator: foreach($loggedInfo as $name => $val) { echo !empty($val) ? "$name: $val<p/>" : "<a href='edit_profile.php'>edit this!</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092276 Share on other sites More sharing options...
sean04 Posted July 28, 2010 Author Share Posted July 28, 2010 Ah so can I do this? foreach($loggedInfo as $name => $val) { if(!empty($val)) { echo "$name: $val<p/>"; echo "$birthday: $val<p/>"; echo "$location: $val<p/>"; } else { echo "<a href='edit_profile.php'>edit this!</a>"; } } Or like individually? foreach($loggedInfo as $name => $val) { if(!empty($val)) { echo "$name: $val<p/>"; } else { echo "<a href='edit_profile.php'>edit this!</a>"; } } foreach($loggedInfo as $name => $val) { if(!empty($val)) { echo "$birthday: $val<p/>"; } else { echo "<a href='edit_profile.php'>edit this!</a>"; } } Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092283 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 The code I provided will loop through all of the elements in the array, so it's not necessary to write repetitive code. Try it. If you have any problems post back. Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092284 Share on other sites More sharing options...
sean04 Posted July 28, 2010 Author Share Posted July 28, 2010 All loggedInfo is is a sql statement $loggedInfo = select * from table where userid = loggedInUser Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092287 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 Then how did you plan on doing what you did with it in the OP? Here's what you might do (I made some assumptions so you might have to change the column names and things like that): $sql = "select Name, Birthday, City, Country, Status from table where userid = loggedInUser"; // Did you mean $loggedInUser ? if($result = mysql_query($sql)) { $row = mysql_fetch_assoc($result); foreach($row as $name => $val) { if(!empty($val)) { echo "$name: $val<p/>"; } else { echo "<a href='edit_profile.php'>edit this!</a>"; } } } else { // error } Quote Link to comment https://forums.phpfreaks.com/topic/208618-if-there-is-no-data-write-edit/#findComment-1092292 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.