Categories
PHP

PHP DOM and XPATH

After checking out beautifulsoup in Python, I became very curious to know what we have in PHP for accessing the DOM nodes of a html document. After exploring a while, I found out the PHP DOM and XPATH.

Here’s a little example:

masnun.html – The HTML file

Now we use PHP to access the document:

That’s it, pretty simple and easy ! 😀

Categories
PHP Python

Open Source and Bilingual Eid Greetings!

Wishing you all a very happy Eid Day 🙂

PHP:

Python:

Categories
PHP

A beginner’s look into the Orchid Models

In Orchid you must have the database setup and configured to use a model based on the tables of that database. The cool thing is that, you can use the tables of a database directly without explicitly constructing any models with them. Make sure that you have configured your database connection correctly in “app/config/configs.php”. For each table in the defined database, you can use models like this:

To find a specific entry, use the find() method of the model object. Example:

Remember that the columns are returned as associative arrays or dictionaries. To get the email address of the data marked by ID 10, we use:

Though I believe that it’d have been better if the resultset was an object as well. Then we would have been able to grab the email address by typing:

I have an work around it. Just cast the resultset into an object using this following code:

Well, having an associative array is not bad, but it’s cooler to have an object 🙂

If you are obsessed about OOP, take the advantage of the model class definition and override the find() method to return an object.

Suppose we have a table named “users” which we will convert into a model. Go to the “app/models” directory and save the following file as: usersmodel.php

Now you are done 🙂 You can now use the “users” model as following:

Cool, isn’t it ? Orchid is really fantastic when it comes to flexibility. I love it ! 😀

Methods:

Once you have retrieved or constructed a resultset, you can use the following methods to interact with the database:

clean() — Cleans all values of a model’s fields.
delete() — Delete the row that contains the current resultset.
insert() — Once you have added values to each of the fields, use this method to insert the data.
update() — Updates the resultset with altered data.
find() — Finds and returns a single resultset based on the given condition.
findAll() — Finds all the results based on the condition.
findById() — Finds a single resultset based on the “id” field.
save() — Similar to update, it saves the resultset into the database.
join() — For complex queries having “joining” operations.

PS: There are lots more functionalities in Orchid models. Hasin vai taught me many of the fantastic features that the models in orchid have. But unfortunately, I have lost many of those information from my memory. I’ll have to check my chat history or ask Hasin bro again when I need those functionalities. But mainly these techniques described above would do most of my database related tasks.