Jump to content

If there is no data write "Edit"


sean04

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
}

}


Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

:shrug: 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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.