Php noob help

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236

I like that! I like that a lot. Thanks Kabal

OOP is something I really need to start doing, I am just struggling to think of things in a Object manner and before I know it I have a begin middle end approach.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,922
no problem

a simple way is to try internalise the Single Responsibility Principal, and always ask yourself things like "Does this really belong here? What is this doing, is this a good name? etc"
 

rward

Senior Member
Joined
Oct 26, 2007
Messages
865
Extra S in the second one:

private static GET_ITEMS_SQL = "SELECT * FROM items";
private static GET_ITEMS_SQL = "SELECT * FROM items WHERE id = ?";

should be:

private static GET_ITEMS_SQL = "SELECT * FROM items";
private static GET_ITEM_SQL = "SELECT * FROM items WHERE id = ?";
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
no problem

a simple way is to try internalise the Single Responsibility Principal, and always ask yourself things like "Does this really belong here? What is this doing, is this a good name? etc"

I do do that and then I simply add it in a other file and use require or include :p

Right now I am trying to get comfortable with OOP and achieve "least privileged" since I can see from your example some variables and functions should not be accessible to anything other than the function that uses it.
 

f0Iygu7gXHVM

Well-Known Member
Joined
Oct 12, 2016
Messages
135
That's what I thought.

/moving on.

If this was production and not done in a few minutes of course I would do something like:

PHP:
DELIMITER $$
 
CREATE PROCEDURE GetItem()
BEGIN
 SELECT *
 FROM items;
    END$$

and then

PHP:
$sql = 'CALL GetItem()';

Why would you have completely different, insecure code in dev that will HAVE to change for it to be in production? Link some of your projects I'm (((interested)))
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Why would you have completely different, insecure code in dev that will HAVE to change for it to be in production? Link some of your projects I'm (((interested)))
Because this was not a project this was an illustration to the OP of the logical process that must happen for him to display the data and was in no way more than this.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Because this was not a project this was an illustration to the OP of the logical process that must happen for him to display the data and was in no way more than this.
I think many miss that point; explanations are best kept simple.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I like that! I like that a lot. Thanks Kabal

OOP is something I really need to start doing, I am just struggling to think of things in a Object manner and before I know it I have a begin middle end approach.
You should never fall into the quagmire of trying to envisage eveything as objects; that part of OOP is long considered BS. Design should be kept simple, don't ever build structure you'll probably never need.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,922
[)roi(];18691682 said:
You should never fall into the quagmire of trying to envisage eveything as objects; that part of OOP is long considered BS. Design should be kept simple, don't ever build structure you'll probably never need.

this is true.

that is why I didnt go crazy and use a full blown service repository "pattern". all I wanted to do was create a simple abstraction/separation, to make things easier, in my mind, to understand, and potentially expand on if required
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
SQL injection FTW

You can try

PHP:
$stmt = $pdo->prepare('SELECT * FROM items WHERE id = :id');

$stmt->execute(array('id' => $id));

foreach ($stmt as $row) {
    // do something with $row
}
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
this is true.

that is why I didn't go crazy and use a full blown service repository "pattern". all I wanted to do was create a simple abstraction/separation, to make things easier, in my mind, to understand, and potentially expand on if required
Was a suggestion for Thor187; your code was spot on.
 

markbdaniels

Well-Known Member
Joined
Jan 12, 2016
Messages
136
Why would you create a stored procedure for a simple select. I'd rather have it in code where it's more readable.
 

Genisys

Honorary Master
Joined
Jan 12, 2016
Messages
11,217
Why even load that initial table from a DB (that table with the city names)? That is wasteful and should be discouraged. The more you read from the Database, the more resources will be needed. Use JSON and read the table content from the JSON file (just in case it will have to be modified) then use the modal to load the other stuff from the Database.
 
Top