Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    trying to order/sequence divs from a db table.

    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
    Code:
     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,

    Code:
     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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Code:
    require "$header";
    You need to refresh your memory on why you shouldn't do that.
    See: `perldoc -q quoting`

    Code:
    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'.

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Sorry for the sloppy post earlier. All of the 'requires' used " " though I have replaced that with

    Code:
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thank you FishMonger. I hope I have set it out clearly this time.

    Edit:
    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
    Code:
     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:

    Code:
      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
    Last edited by bazz; 02-11-2010 at 02:44 PM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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

  • #7
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •