• 0

using functions in this situation?


Question

I'm creating a mini CMS for a client so he can easily update parts of his website.

At the moment i have lots of php files or switches to execute queries to update the content.

Is it possible to contain all this information in one file called functions.php, require it into the document and just call the function when it needs to be executed?

I've not really dabbled in functions before.

An example,

some of the tasks the client can do:

-update ticker on home page (updateTicker.php)

-update profiles (addProfile.php)

and so on...

can i add them into functions in 1 file?

function updateTicker() {

}

function addProfile() {

}

function updateProfile() {

}

Would this be the right way to go about it? and how can i call the function when submitting the form so it can update the database?

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

I'm looking on google and as far as i can see i can't call a function from a form?

The way i want it to work is.

I have a admin page, which calls different forms, depending on the switch, such as:

?do=ticker

- this executes the ticker form

?do=addRelease

- this executes the add release form

Now, the old way of doing things i would create dbTicker.php and have the UPDATE query in here..

The i'd have another addRelease.php and have the INSERT query in here.

What i want to do is eliminate the need for lots of php files containing only a query.

My idea was to put it all in functions and have all the queries in one PHP file? How would i execute a certain function when the form is submitted?

If that is messy, what is the correct to do this?

Link to comment
Share on other sites

  • 0

In one line object oriented programming. Its best because its very scalable and a breeze to maintain.

never heard of it, i'll look it up. thanks

Link to comment
Share on other sites

  • 0

I'm looking on google and as far as i can see i can't call a function from a form?

The way i want it to work is.

I have a admin page, which calls different forms, depending on the switch, such as:

?do=ticker

- this executes the ticker form

?do=addRelease

- this executes the add release form

Now, the old way of doing things i would create dbTicker.php and have the UPDATE query in here..

The i'd have another addRelease.php and have the INSERT query in here.

What i want to do is eliminate the need for lots of php files containing only a query.

My idea was to put it all in functions and have all the queries in one PHP file? How would i execute a certain function when the form is submitted?

If that is messy, what is the correct to do this?

switch (strtolower($_GET['do'])) {
    case "ticker":
        ticker();
        break;
    case "addrelease":
        addRelease();
        break;
    default:
       default();
}

etc.

The rest is really how you want to organise it, in one of my scripts I have 2 files that do different but related things, so all the common code is stuffed in an external php file I require() in each script.

Link to comment
Share on other sites

  • 0

I'm have little PHP experience but why don't you install WordPress on your local server. I use WordPress for my sites and had to make a few changes to the templates. Adding and taking stuff out. I saw a "functions.php", which is contains functions like adding the "read more" tag after an intro title. Look at the codes on the functions.php to see how it is set out there and go look at the index.php and 404.php pages to see how functions has been incorporated in the normal php sites.

The best part... you might learn something new.

Link to comment
Share on other sites

  • 0

Wordpress is often overkill for what the OP is looking for. I know it's often overkill for me. If you're looking for a basic CMS, what do you need all of Wordpress's features (and vulnerabilities)?

Link to comment
Share on other sites

  • 0

http://php.net/oop - the PHP manual is a great place to start, I've found.

I'm not expert so i could be way off but that looks quite bloated and complicating for submitting short lines of code to a database, it seems it will be almost easier to do it my old way.

switch (strtolower($_GET['do'])) {    case "ticker":        ticker();        break;    case "addrelease":        addRelease();        break;    default:   	default();}

etc.

The rest is really how you want to organise it, in one of my scripts I have 2 files that do different but related things, so all the common code is stuffed in an external php file I require() in each script.

In this case would you have the form inside the function.

such as:

function addProfile($name,$age..) {

if (isset($_POST['submit'])) {
$result = mysql_query("INSERT....");
} else {
echo '<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
...
</form>';
}

sorry, as above, i'm no real expert and your example isn't really clear how i would go from there?

Thanks :)

I do use WordPress on some sites, it would be too bloated for this site though.

I already know PHP basics and have this operating, i'm just looking for a different method of coding these forms though, as mentioned i'm pretty sure i don't know a new .php file for each query.

Link to comment
Share on other sites

  • 0

You don't need to use WordPress for the site. WP has a functions.php page where, as it seems to me, all the functions and actions is listed. Looking at the codes, you might find out how you can combine all those actions

At the moment i have lots of php files or switches to execute queries to update the content.
into one file called functions.php. After that you can look at the other pages to see how those functions are taken out of the functions.php page and put onto eg. a index.php page.

You don't have to use WP, but learn from it.

Link to comment
Share on other sites

  • 0

You don't need to use WordPress for the site. WP has a functions.php page where, as it seems to me, all the functions and actions is listed. Looking at the codes, you might find out how you can combine all those actions into one file called functions.php. After that you can look at the other pages to see how those functions are taken out of the functions.php page and put onto eg. a index.php page.

You don't have to use WP, but learn from it.

good point, thanks :)

Link to comment
Share on other sites

  • 0

...

In this case would you have the form inside the function.

such as:

function addProfile($name,$age..) {

if (isset($_POST['submit'])) {
$result = mysql_query("INSERT....");
} else {
echo '<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
...
</form>';
}

sorry, as above, i'm no real expert and your example isn't really clear how i would go from there?

Thanks :)

...

Ehh, no.

In the page for the user you'd (well, I made this code up) have something like...

<form action="main.php?action=add" method="post">
    <label for="name">User Name:</label> <input type="text" name="username" id="name">
</form>

Then in main.php you'd have this bit of code.

switch (strtolower($_GET['action'])) {
    case "add":
        addUser($_POST['username']);
        break;
    default:
       default();
}

Then the addUser function runs the queries, returns a result (say, the next page for the user to navigate to) then the script redirects the user there.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.