Thread: DESIGN CONCEPT (performance) - switch/arrays/forms

DESIGN CONCEPT (performance) - switch/arrays/forms

From
Bruce Young
Date:
Hi list!
i am working on a little project where i have a PHP script that selects a form
(located on hard drive)
using the switch statement and "include".  for example:

switch ($type)
{
    case 1:
        include 'forms/add_edit/form_book.php';
        break;

    default:
        show_error("Item Type not found",1);
}

i have about 5 scripts that use switch statement to select forms.

Problem is, right now i only have 1 category ($type).  Now when i  add about 10
or 20+ more categories, these scripts will start to become a pain to maintain
with all the switch/cases.
My idea is to have a category table with references to the forms on disk so i
can fetch it into an array; search the array to find the corresponding form for
the category id, and include that form;
example:

Category table

 id | name |    form
---+-------+---------------
  1 | Book  | form_book.php

--------------------------
$type = $HTTP_GET_VARS['type'];

# select id, form  from categories;

# assign array values returned by query
# search if category exists ($type) and present error message

# present corresponding form to $type.
----------

My concern is, would this be too much work on the database and would it be
better to manually code it into the PHP script.
if this concept could work fine, could anyone give me an example on how to do
this?  i am just learning PHP.  thanks a lot.

  - bruce


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

Re: DESIGN CONCEPT (performance) - switch/arrays/forms

From
brew@theMode.com
Date:
Bruce......

> My idea is to have a category table with references to the forms on disk
> so i can fetch it into an array; search the array to find the
> corresponding form for the category id, and include that form;

I think this is something like I'm doing on my musician's classified,
TheMode.com on some pages.

I have a few master table of contents pages that show the number of
listings in each telephone area code.  On my laptop it ran in less than a
second, but on my shared web host it took several seconds. I think there
are about 80 accounts on the machine, and EACH one runs it's own copy of
Apache and the accounts that have DB servers EACH have their own copies,
too, be they mySQL or postgreSQL, or whatever.  Nice concept, but it bogs
down with processes when something goes wrong in an account.

At any rate, what I did to speed it up was run a cron job once every few
hours to assemble the Table of Contents pages and store them as one record
in the postgreSQL database, using php to pull it out.  It's faster that
way, but still takes about 100 milliseconds on the online shared machine,
on my laptop it takes 20 milliseconds.

On the shared machine when processes start running away the static pages
still serve fairly quickly but the DB stored pages get noticibly slower.
When I find my Round Tuit I'll make more and more of the cron assembled
pages store on the file system instead of the database.  Of course for
interactive DB searches I'll still use the DB server.  Or maybe I'll work
out a way to get a dedicated machine on a fast pipe eventually.

So I guess it's hard to predict as it depends on the server load.  My
website only gets a few page views a minute, I would be afraid to try to
serve multiple page views a second on a shared machine like this, but I
bet a dedicated machine could do it and probably many on the list have
dedicated machines.

BTW, for the record I use www.viaverio.com for my webhost.

> if this concept could work fine, could anyone give me an example on how
> to do this?  i am just learning PHP.  thanks a lot.

If you search around the web you'll find examples of using php and
postgreSQL together, start reading and coding and reading and coding.

Maybe someone can recommend a book, I can't because when I started with
Relational Databases it was with perl and mSQL (that's mini SQL, not
mySQL), I transposed to php a few years later.  I use postgreSQL for
TheMode.com, even though mySQL could do everything I'm doing now. I want
to get more familiar with transactions, functions and triggers eventually
for other projects so I went with PostgreSQL for the experience.

The other thing that helps is having a machine to play on.  I have perl,
php, mySQL and postgreSQL all on my Debian linux laptop (and all running
at once).

brew

 ==========================================================================
                  Strange Brew   (brew@theMode.com)
     Check out my Musician's Online Database Exchange (The MODE Pages)
                        http://www.TheMode.com
 ==========================================================================



Re: DESIGN CONCEPT (performance) - switch/arrays/forms

From
"Andrew J. Kopciuch"
Date:
Why not just store the types in the DB and query that?

-- table definition

create table categories
(
    type                whateverTypeThisFieldActuallyIs (ie int) primary key,
    name                text,
    form                text
);



// PHP code

$type = $HTTP_GET_VARS['type'];

$rv = pg_exec($dbConnection,
             "SELECT form FROM categories WHERE type = '$type';");

if (pg_numrows($rv) > 0)
{
    list($form) = pg_fetch_row($rv, 0);
    include($form);
}
else
{
    show_error("Item Type not found", $type);
}





No arrays ... no loops.  One simple IF.  Should do what you need.

I have worked on sites where the entire content is stored in the database ...
works fine.  Pefectly fast.

Have you looked into PHP cache extensions if speed is an issue for file
parsing?  try:  http://www.php-accelerator.co.uk/


Re: DESIGN CONCEPT (performance) - switch/arrays/forms

From
Bruce Young
Date:
I dont know why i was thinking about solving the problem with arrays.  i came
up with the same solution and was about to post it.  thanks a lot guys for the
advice, or any more.  : )

$query = "select * from test.category where id=$type";
$result = pg_query ($dbconnect, $query);

if (pg_num_rows($result))
{
    $row = pg_fetch_array($result);
    include $row['form'];
} else {
    show_error("Item Type not found",1);
}





--- "Andrew J. Kopciuch" <akopciuch@bddf.ca> wrote:
> Why not just store the types in the DB and query that?
>
> -- table definition
>
> create table categories
> (
>     type                whateverTypeThisFieldActuallyIs (ie int) primary key,
>     name                text,
>     form                text
> );
>
>
>
> // PHP code
>
> $type = $HTTP_GET_VARS['type'];
>
> $rv = pg_exec($dbConnection,
>              "SELECT form FROM categories WHERE type = '$type';");
>
> if (pg_numrows($rv) > 0)
> {
>     list($form) = pg_fetch_row($rv, 0);
>     include($form);
> }
> else
> {
>     show_error("Item Type not found", $type);
> }
>
>
>
>
>
> No arrays ... no loops.  One simple IF.  Should do what you need.
>
> I have worked on sites where the entire content is stored in the database ...
>
> works fine.  Pefectly fast.
>
> Have you looked into PHP cache extensions if speed is an issue for file
> parsing?  try:  http://www.php-accelerator.co.uk/
>


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com