Jump to content

Question about functions


3raser

Recommended Posts

Can someone give me an actual time when functions would become handy?

 

Thanks!

Please, you can easily Google this for more reasons.  Calling functions helps you not repeat code.

Link to comment
Share on other sites

EDIT: I decided to post a question about functions instead.

 

Here is my function code:

 

<?php

function OverallGrabInfo($username)
{
$set_query = mysql_query("SELECT d.username, u.date, u.username FROM downloads d, users u WHERE d.username = '$username' AND u.username = '$username'") or die(mysql_error());

if(mysql_num_rows($set_query) == 0)

	$content_return = 'Sorry, no information was found';


else

	$grab = mysql_fetch_assoc($set_query);

	$content_return = $grab['date'];


return $content_return;
}

?>

 

Here is my page code:

 

<?php

if(!$_COOKIE['user'])
{
	$welcome = 'Welcome, guest! <a href="signup.php">Sign up now for uploading abilites!</a>';
}
else
{
	include_once('functions.php');
	$welcome = 'Welcome, '. $_COOKIE['user'] .'!'. $content_return;

}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style/style.css" />
<title><?php echo $title; ?></title>
</head>
<body>

<div class="logo"><a href="index.php"><img src="style/images/logo.png"></a></div>

<center>

<div class="background">

<div class="container">
Welcome.
</div>

</div>
</center>

</body>
</html>

 

How do I set $username to the one that will be processed in the function?

Link to comment
Share on other sites

So submitting a post into a database would require a function?

 

Why, though? I mean, more code to do what you could do on the actual posting page...

Yes.  You use functions all the time.  mysql_query(...) is a function.  Trust me, you wouldn't want to write that code every time.  Native functions are used to reduce code, so are custom ones.

 

EDIT: Because you can ask questions on here. Thus the point of this whole section?

Because, unless you have a very specific question, then there are literally thousands of results for "PHP, why use functions?", and therefore, wasting our time.

Link to comment
Share on other sites

you don't *need* functions, but they make life easier.  The point of functions is being able to reuse code.  If you just have a single page doing a single thing, then there is little point in making a function out of it.  But if you have many pages doing the same thing, it makes more sense to make it into a function and call the same function - have a single instance of the code instead of many instances of it.  Why? For one thing, having 100 instances of the same thing is a waste of space.  Also, what about having to make updates to it?  If you suddenly need to make a change to the code, you would be making a change to it in 100 places instead of one.

Link to comment
Share on other sites

 

EDIT: I needed to make changes to the original post, like the one below, but couldn't edit. So I had to re-post, sorry for this.

 

Here is my function code:

 

<?php

function OverallGrabInfo($username)
{
$set_query = mysql_query("SELECT d.username, u.date, u.username FROM downloads d, users u WHERE d.username = '$username' AND u.username = '$username'") or die(mysql_error());

if(mysql_num_rows($set_query) == 0)

	$content_return = 'Sorry, no information was found';


else

	$grab = mysql_fetch_assoc($set_query);

	$content_return = $grab['date'];


return $content_return;
}

?>

 

Here is my page code:

 

<?php

if(!$_COOKIE['user'])
{
	$welcome = 'Welcome, guest! <a href="signup.php">Sign up now for uploading abilites!</a>';
}
else
{
	include_once('functions.php');
	$welcome = 'Welcome, '. $_COOKIE['user'] .'!'. $content_return;

}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style/style.css" />
<title><?php echo $title; ?></title>
</head>
<body>

<div class="logo"><a href="index.php"><img src="style/images/logo.png"></a></div>

<center>

<div class="background">

<div class="container">
Welcome.
</div>

</div>
</center>

</body>
</html>

 

How do I set $username to the one that will be processed in the function?

Link to comment
Share on other sites

You would do something like this:

else
{
   include_once('functions.php');
   $content_return = OverallGrabInfo($_COOKIE['user']);
   $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return;
}

 

FYI, your IF/ELSE blocks in your function are missing braces.

Link to comment
Share on other sites

You would do something like this:

else
{
   include_once('functions.php');
   $content_return = OverallGrabInfo($_COOKIE['user']);
   $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return;
}

 

FYI, your IF/ELSE blocks in your function are missing braces.

 

I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O

Link to comment
Share on other sites

You would do something like this:

else
{
   include_once('functions.php');
   $content_return = OverallGrabInfo($_COOKIE['user']);
   $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return;
}

 

FYI, your IF/ELSE blocks in your function are missing braces.

 

I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O

That only works if you want the block to only include the first line directly below it.  It will throw a fatal error if you run it the way your blocks are set up.

Link to comment
Share on other sites

You would do something like this:

else
{
   include_once('functions.php');
   $content_return = OverallGrabInfo($_COOKIE['user']);
   $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return;
}

 

FYI, your IF/ELSE blocks in your function are missing braces.

 

I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O

That only works if you want the block to only include the first line directly below it.  It will throw a fatal error if you run it the way your blocks are set up.

 

Thanks :)

 

This is now solved.

Link to comment
Share on other sites

You would do something like this:

else
{
   include_once('functions.php');
   $content_return = OverallGrabInfo($_COOKIE['user']);
   $welcome = 'Welcome, '. $_COOKIE['user'] .'!' . $content_return;
}

 

FYI, your IF/ELSE blocks in your function are missing braces.

 

I was told that you can do if/else functions without braces, they said it could be an improvement for code. o.O

That only works if you want the block to only include the first line directly below it.  It will throw a fatal error if you run it the way your blocks are set up.

 

Thanks :)

 

This is now solved.

You could have just hit the Auto Solve button ;P

Link to comment
Share on other sites

Heh, I wish!

 

But yeah, one last question regarding functions (hopefully!). Here is my code below:

 

<?php

function OverallGrabInfo($username)
{
$set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error());

if(mysql_num_rows($set_query) == 0)
{

	$content_return = 'Sorry, no information was found';

}
else
{
	$grab = mysql_fetch_assoc($set_query);

	//login information
	if($grab['COUNT(d.username)'] > 0)
	{
		$welcome_return = "You have uploaded ". $grab['COUNT(d.username)'] ." files. You've registered on ". $grab['u.date'] ."!";
	}
	else
	{	
		$welcome_return = "You have uploaded 0 files. You've registered on ".$grab['date'] . "!";
	}

	//display downloads


}	

return $content_return;
}
?>

 

Is it possible to grab information from $set_query without having the WHERE? I used the WHERE code to grab specific information regarding the user, but is it possible to do another section of the query to display all rows in the table uploads without having the WHERE? Or will I just need to create another query?

Link to comment
Share on other sites

Now, when you call this function, unless you specify the $where parameter to be "FALSE", it will include the WHERE clause.

 

function OverallGrabInfo($username, $where=TRUE)
{
   if($where===TRUE)
   {
       $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error());
   }
   else
   {
      $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u") or die(mysql_error());
   }

Link to comment
Share on other sites

Now, when you call this function, unless you specify the $where parameter to be "FALSE", it will include the WHERE clause.

 

function OverallGrabInfo($username, $where=TRUE)
{
   if($where===TRUE)
   {
       $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u WHERE d.username = '$username' AND u.username = '$username' LIMIT 1") or die(mysql_error());
   }
   else
   {
      $set_query = mysql_query("SELECT COUNT(d.username), u.date, u.username FROM uploads d, users u") or die(mysql_error());
   }

 

Ah, nice!

 

Also, if I were to make a sign-up/registration page, would creating another function for specifically that cut down on the code processing time? It's a one time code thing, though. So I'm not sure if I should create a function for it in functions.php, or just put the code on the signup.php page.

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.