Jump to content

Recommended Posts

Hi, I am having some problems with includes /functions

 

The code is complicated and long so I'll just explain it and give you some simplified code. 

What the code does:  It takes a form entry. It collects all the variables using $_REQUEST.  It does some checks and then puts the form data into a db on the site.  Here is where it gets more complicated... There are check boxes on the form that relate to other websites.  The code asks if each checkbox is checked. It also checks the previous state of each checkbox (in case it has changed). For each checkbox that is ticked it sends the form data via xml to the other site referred to by the checkbox.

 

There are four possibilities for each checkbox:-

checked was checked before -          Means it's a data update on other site

checked wasn't checked before -      Means it's a fresh post to that site but not to source site

not checked was checked before -    Means it's a data delete on other site

not checked wasn't checked before. - No action

 

for each of those possibilities the code for sending is slightly different

 

There are 10 checkboxes.

Rather than write out nearly the same code 40 times I wanted to pull it into my code from an external page. That way I end up with

//lots of variables from form go here
//
//update
if($checkboxA=="on" && $checkboxAprevious =="on"){
     $remote_website = "www.siteA.com" ;
     //include code for updating here
}//end update
//fresh post
if($checkboxA=="on" && $checkboxAprevious !="on"){
     $remote_website = "www.siteA.com" ;
     //include code for posting here
}//freshpost
//Delete
if($checkboxA !="on" && $checkboxAprevious =="on"){
     $remote_website = "www.siteA.com" ;
     //include code for deleting here
}//delete
//do nothing
if($checkboxA !="on" && $checkboxAprevious !="on"){
}//do nothing

That snippet has to be repeated 10 times (once for each checkbox).  I can live with that

The code to be included is long.  It grabs all the form variables and writes them to an xml file (xml file is different depending on action to be taken (eg post/delete/update)).  It then opens a port to the remote site and sends the xml and prints a response if all has gone well.

 

Finally I get to the problem:  If I make "code for posting" a function() and call it then the form $variables and the $remote_website will not pass into the function.

 

If I make "code for posting" an include() then the form $variables and the $remote_website will not pass into the included code either.

 

All I want is to be able to write the code once and call it to a place in my code where it should run like it was part of the code to start with and not called in.

 

Where am I going wrong? ???

 

P.s sorry for such a long post. ::)  Hope it makes sense to you. :-\

Link to comment
https://forums.phpfreaks.com/topic/150306-solved-help-with-cleaner-code/
Share on other sites

No. I'm using Simple_xml which is working fine.  There is nothing wrong with the code for posting to remote sites.  It works perfectly if I write it where it is supposed to go.  The problems are coming from trying to have the code elsewhere and call it to where it is supposed to go.  The object is to make the page lighter.  I want to only call the code if the if statements are met rather than have them all on every time the page loads.  That would be 40 segments of code where I may not need any (if no action on checkboxes).

 

I have 10 sites I am sending to now but that is set to triple soon (120 code segments if I don't get this problem sorted).  I will also have multiple users posting.  The overhead is too high and I need to find ways of keeping it down.

Why not use an array? Then you can check to see if a check box has been set with in the array and you can do this via a loop, if yes the array can be 2d and have the url included then you know exactly which check box and which url, you could continue this system (3d) to even make the array contain the include code so then you simply echo it? Thinking in arrays is difficult, but they make code lighter and more robust.

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.