Jump to content

Recommended Posts

I have created a function to generate some content using a parameter which is defined using the $_GET method.

 

The function checks to see if the $_GET variable is an integer before generating the content. If it is not I want to run the function again with a definite integer value. However I keep getting an internal server error.

 

function generatecontent($var) {
     if(is_int($var)) {
          echo 'content generated';
     } else {
          generatecontent(1);
     }
}

generatecontent($_GET['id]);

 

Any Idea where im going wrong :S

Link to comment
https://forums.phpfreaks.com/topic/146027-solved-calling-a-function-within-itself/
Share on other sites

"1" is not considered an int.

 

is_int.

 

Thus you are sent into an infinite loop. is_numeric would be a better choice, but would allow for doubles etc. You can always make the get variable an int statically.

 

function generatecontent($var) {
     if(is_numeric($var)) {
          echo 'content generated';
          $var = (int) $var; // convert it to int.
     } else {
          generatecontent(1);
     }
}

generatecontent($_GET['id']);

 

The above may work for you, or may not. But yea. At least you now know why it was not working. (sort of).

 

But that would also defeat the purposes of checking if it is an int, as will be converted to an int.

Thanks the is_numeric worked as I had required! I had seen it before but cant remember why I didnt try it?

 

I need to be able to call the function within itself as there is a mysql Select carried out during the content generation... if the $_GET variable is not a valid user profile id then the function defaults to the users own id

 

function viewprofile($user) {
if(!empty($user) && is_numeric($user)) {
	$query = "SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id='$user' AND user_profiles.user_id = users.id";
	$result = mysql_query($query);
	$num = mysql_num_rows($result);
	$row = mysql_fetch_assoc($result);

	if($num == 1) {
		echo '
			<div id="profile_container">
			<div id="profile_left">
				<div id="profile_info">
					<h2>'.$row['username'].'</h2>
					<img src=""/>
					<h3>Communication</h3>
					<ul>
						<li><a href="">Message</a></li>
						<li><a href="">Add Friend</a></li>
					</ul>
				</div>
				<div id="status">
					<h3>Status</h3>
					<ul>
						<li><p><span>Page Hits:</span>'.$row['views'].'</p></li>
						<li><p><span>Member Since:</span>'.$row['created'].'</p></li>
						<li><p><span>Last Active:</span>'.$row['lastlogin'].'</p></li>
						<li><p><span>Creatures Caught:</span>--</p></li>
					</ul>
				</div>
				<div id="personal_stats">
					<h3>Personal Status</h3>
					<ul>
						<li><p><span>Makes Me Happy:</span>'.$row['happy'].'</p></li>
						<li><p><span>Makes Me Sad:</span>'.$row['sad'].'</p></li>
						<li><p><span>Perfect Partner:</span>'.$row['partner'].'</p></li>
						<li><p><span>Bad Habits:</span>'.$row['habits'].'</p></li>
						<li><p><span>Cigarettes</span>'.$row['cigarettes'].'</p></li>
						<li><p><span>Alcohol:</span>'.$row['alcohol'].'</p></li>
						<li><p><span>Race:</span>'.$row['race'].'</p></li>
						<li><p><span>Hair:</span>'.$row['hair'].'</p></li>
						<li><p><span>Body Type:</span>'.$row['body'].'</p></li>
					</ul>
				</div>
				<div id="favourite_things">
					<h3>Favourite Things</h3>
					<ul>
						<li><p><span>Food:</span>'.$row['food'].'</p></li>
						<li><p><span>Music:</span>'.$row['music'].'</p></li>
						<li><p><span>TV Show:</span>'.$row['tv'].'</p></li>
						<li><p><span>Author:</span>'.$row['author'].'</p></li>
						<li><p><span>Movie:</span>'.$row['movie'].'</p></li>
						<li><p><span>Nightclub:</span>'.$row['club'].'</p></li>
						<li><p><span>Person:</span>'.$row['person'].'</p></li>
						<li><p><span>Place:</span>'.$row['place'].'</p></li>
					</ul>
				</div>
			</div>
			<div id="profile_right">
				<div id="identification">
					<h3>Identification</h3>
					<ul>
						<li><p><span>Name:</span>'.$row['firstname'].' '.$row['surname'].'</p></li>
						<li><p><span>Gender:</span>'.$row['age'].' years old ('.$row['star'].'), '.$row['gender'].', '.$row['relationship'].'</p></li>
						<li><p><span>Location:</span>'.$row['city'].', '.$row['country'].'</p></li>
						<li><p><span>Profile Link:</span><a href="">http://www.capture.me/'.$row['username'].'</a></p></li>
					</ul>
				</div>
				<div id="more_information">
					<h3>Blurb</h3>
					<ul>
						<li><p><span>About Me:</span>'.$row['about_me'].'</p></li>
						<li><p><span>Extra:</span>'.$row['extra'].'</p></li>
					</ul>
				</div>
				<div id="my_adventures">
					<h3>My Adventures</h3>
					<ul class="image_list">
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
					</ul>
				</div>
				<div id="my_friends">
					<h3>My Friends</h3>
					<ul class="image_list">
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
						<li><img src=""/><p>Name</p></li>
					</ul>
				</div>
			</div>
		</div>';
	} else {
		viewprofile($_SESSION['uid']);
	}
} else {
	viewprofile($_SESSION['uid']);
}
}

viewprofile($_GET['uid']);

 

Im totally new to PHP and this is the first attempt I have made at writing a function so I know its probably not correct (but it works :P) haha

 

PS - Does anyone have any tips on how to include the html code in my function, it just looks soo messy :(

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.