How to retrieve variables easily

Category:   Blog | Author:   nlstart

When initially I started coding for e107, I was unaware of the power of PHP. I developed many plugins before I discovered this handy way of getting my variables.

Suppose the table design looks as follows:
Image: http://e107.webstartinternet.com/images/newspost_images/blog001.jpg

Up and until yourfirstplugin v1.4 I would use the following construction to retrieve the variables:

    // Select the single record from the database
    $sql -> db_Select("yourfirstplugin_categories", "*", "yourfirstplugin_id=".intval($_GET['yourfirstplugin_id']));
    if($row = $sql-> db_Fetch())
    {
        $yourfirstplugin_id = $row['yourfirstplugin_id'];
        $yourfirstplugin_name = $row['yourfirstplugin_name'];
        $yourfirstplugin_image_path = $row['yourfirstplugin_image_path'];
        $yourfirstplugin_test_01 = $row['yourfirstplugin_test_01'];
        $yourfirstplugin_test_02 = $row['yourfirstplugin_test_02'];
        $yourfirstplugin_test_03 = $row['yourfirstplugin_test_03'];
        $yourfirstplugin_test_04 = $row['yourfirstplugin_test_04'];
        $yourfirstplugin_test_integer = $row['yourfirstplugin_test_integer'];
        $yourfirstplugin_active_status = $row['yourfirstplugin_active_status'];
        $yourfirstplugin_order = $row['yourfirstplugin_order'];
    }

Typically, all variables are named the very same as the name of the field name of the row that is retrieved from the e107 MySQL database.
If you use variables with their original table field name, instead of having the trouble to define them all individually you can also do this:
    // Select the single record from the database
    $sql -> db_Select("yourfirstplugin_categories", "*", "yourfirstplugin_id=".intval($_GET['yourfirstplugin_id']));
    if($row = $sql-> db_Fetch())
    {
        extract($row);
    }

This second piece of coding has the exact same result as the first example, and further in the coding variables like e.g. $yourfirstplugin_name can be used. Much more efficient by using the PHP function extract on the array that is the result of db_Fetch. It saves a lot of code lines, the code is easier to read and when the table is expanded with new fields these will be taken into account as well without having to add them to the code.

 printer friendly create pdf of this news item