Is there any way to get organized in this

is there a way to get more organized in all these sqls and the if statements and for loops and so on:index.php (66.1 KB)

use OOP, separation of concerns, dependency injection; put templates, js and php into files and use autoload

1 Like

I have not had the time to study the whole thing, but skimming through, there are cases where you could take advantage of prepared statements with parameter binding, to both re-use queries and help guard against injection, since you don’t seem to be validating or sanitising your $_GET data.

Certainly a case for that.
A real mash-up of PHP, JS, HTML and CSS in there.

1 Like

how do you sanitize it. I had a hard time understanding what Isset is and if I would need to put !=0 , !==0, !=NULL in the if statements.

What I often do is use preg_replace directly on the $_GET to filter out unwanted characters.
For example, if I’m expecting an integer:-

$id = preg_replace('#[^0-9]#i', '', $_GET["id"]) ; // Get ID

That will remove any character which is not within the range of 0 to 9, so the result cannot be anything other than an integer.
Or you can use the filter functions to sanitize and validate different types of data.

But this is just one aspect of a very big script. It’s probably a bit too big to realistically expect someone to go right through and pick out every improvement to be made, there is a lot of scope for improvement in how it’s organised, in many ways.
Separation of concerns has been mentioned and is something you should be looking into to better organise your scripts.
That is, put simply, to keep PHP, JS, HTML and CSS apart in separate files, rather than jumbled together like this.

I kind of got burnt out from coding. I thought of changing my method. I would look in two texts a given set of keywords to determine how much common keywords are there between the texts.

I was thinking of changing my method and taking the 1st text, break down into a long string or array, sort out the little words like ā€œaā€, ā€œtheā€ā€¦or even better… make a checklist on the page of all the words displayed alphabetically (from the 1st text) and select/check what things to look for instead of adding the words manually in the url to begin with.

Nothing personal, but MHO you have been working with this long enough where you should have gained a basic understanding of what’s involved. That’s good.

Up to you of course how you proceed.
PHP can do a good job with Strings eg.
Take a mess of text, put the words into an array.
Using string comparison, regex, metaphone etc. compare those against another array of strings.

So if you want to further your knowledge and skill using that approach, no problem there.

- But - if you are starting to feel ā€œburnt outā€ that is a clue that you are pushing at the limits of what PHP can do and things are getting unwieldy (needing to set the time limit to over a half hour is another clue)

The Good news is others have also run into this problem and an alternative is available.

It might present a ā€œlearning curveā€ but once learned it will be a good addition to have in your toolbox.

MySQL has Natural Language Full-Text Searches

The database is quite efficient at doing a good job with much of what your PHP code is doing.
In fact, I would say it is more efficient.

1 Like

how do i strip off punctuation?

There are also dedicated search tools to consider like solr and sphinx. Those are increasingly more powerful, fast, and flexible than MySQL full text alone.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.