...

View Full Version : trying to order/sequence divs from a db table.



bazz
02-10-2010, 10:30 PM
Hi,

I am trying to make it possible to sequence the output of divs, as per the data in a MySQL table. trouble is; if I rely on variables, the scripts won't compile, because the vars won't have been set.

here is my table


create table div_sequencing
( id int not null auto_increment
, div_name varchar(32)
, sequence number int
) engine=innodb default charset=utf8 collate utf8_unicode_ci;


so if the data is

| 1 | header | 1 |
| 2 | nav_menu | 3 |
| 3 | thumbs | 4 |
| 4 | address | 2 |

If my script is like this,



require "$header";
show_header($session,$subject);

require '$nav_menu';
show_nav($session);

require '$thumbs';
show_thumbs($session);

require '$address';
show_address($session);


That runs but, not in the order as per the db. I recall from a long time ago; the trigger for a sub cannot be a $var.

What is the best approach please?

bazz

FishMonger
02-11-2010, 12:34 AM
require "$header";You need to refresh your memory on why you shouldn't do that.
See: `perldoc -q quoting`



require '$nav_menu';I doubt that is doing what you want/think. Besides the fact that you're quoting a single var, which you shouldn't do, the single quotes don't allow for interpolation, so instead of using/requiring the value of the var, you're using the literal string '$nav_menu'.

bazz
02-11-2010, 04:19 AM
Sorry for the sloppy post earlier. All of the 'requires' used " " though I have replaced that with



require $var;


Thanks FishMonger.

I'll ponder some more, to try to fix the issue I asked about. I am 'sure' there is a way to do it.



bazz

FishMonger
02-11-2010, 04:25 AM
I'm not sure what recommendations I can make, since the code you posted, i.e., the require statements, doesn't shed any light on what you're doing or want to accomplish.

bazz
02-11-2010, 03:42 PM
Thank you FishMonger. I hope I have set it out clearly this time.


I have edited the table structure to include the page/script subject


OK, I am calling in fields from my db in a specific order. then I am trying to make subs run in that same order. Thing is; the subs will not always be of the same name because they are to be named according to what they output, eg header, address, thumbs, gallery, textfiles, prices, specification etc. Not all these subs run would run on one outputted xhtml page.

here is my table


create table sub_sequencing
( id int not null auto_increment
, subject varchar(32)
, sub_name varchar(32)
, sequence_number int
) engine=innodb default charset=utf8 collate utf8_unicode_ci;


so if the data is

| 1 | header | 1 |
| 2 | nav_menu | 3 |
| 3 | thumbs | 4 |
| 4 | address | 2 |

The rest of my code should follow like this:



my $connect = db_connect();
my @subs;

my $sth = $connect->prepare("
select sub_name
from sub_sequencing
where subject = ?
order by sequence_number
") or die "prepare statement failed:$DBI::errstr\n";

$sth-execute($subject);

while (my (@fields) = $sth->fetchrow_array){

push(@subs,$fields[0]);


foreach my $sub (@subs);
my $script_to_require = (join ( "_" , ( $business_id , $sub, '.pl' )));

require $script_to_require;
my $sub_to_run = $sub;

show_header($session,$subject);
or
$sub_to_run($session,$subject);

}



I am struggling with the section in red.
The sub names may vcary so I need to be able to accommodate that reality.

I am pretty certain there is a better way than to run a load of conditionals for every possible sub name.

But I need a nudge or more, to achieve this please.

Two things to note:
1. I have run the foreach loop for ease of explanation ~ especially for when an answer may come back. I would normally try to run it within the while loop.

2. the area in red shows the hard coded way (wrong for this app) and my thought for the dynamic method.


bazz

FishMonger
02-11-2010, 04:37 PM
That's a bad approach.

Instead of using .pl library scripts, use fully fledged .pm module(s) that include all of the subs. This would mean that you would have a single use statement and be able to access any/all subs.

If you need custom subs based on the $business_id, then have 1 base module that defines all of the common subs and separate modules for each business_id. With this approach, you could have 3 possible sub approaches.

Meaning that you could either:
1) manually use/require both the common and specific business_id modules
2) have the common module 'require' the business_id module, just like DBI does with the DBD:: modules
3) only 'use' the business_id module, which is a sub class of the common module

bazz
02-11-2010, 05:04 PM
Thanks FishMonger,

By way of background info. I am trying to provide a range of subs which can grow/be updated with new functionality, such that each business can choose which to use and what order to have them output on their page(s). So, they have full control of their page structure and content as well as the flexibility to alter their css to suit the structure they decide upon.

I had made the .pl files for each business's subs so that if a business needed something different from the norm; they could have it without 'breaking' any other site.

I'll mull over your suggestions to see which way may work best but, is the approach I had started with wrong, because it has too many requires, for example and is therefore inefficient?


bazz

FishMonger
02-11-2010, 06:23 PM
is the approach I had started with wrong, because it has too many requires, for example and is therefore inefficient?
Yes, that is part of the problem. Another is that it becomes a maintenance nightmare and creates spaghetti code. That's what PHP is for. ;)

There is no logical reason to have a separate .pl file for each and every sub that the customer might use. Just think of the mess we'd be in if Lincoln Stein used that approach in the CGI module.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum