SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question switch / case question

    hi there,

    I have this strange idea, don't know if it's possible in any way but maybe someone has a clue, or another idea to handle this...

    Is it possible to auto-generate "case"-s for a "switch" depending on how many rows there are in a MySQL table?

    for example: I have a table with 5 rows so i would get:
    PHP Code:
    switch(statement
    {
     case 
    1: do something
     break;
     case 
    2: do something
     break;
     case 
    3: do something
     break;
     case 
    4: do something
     break;
     case 
    5: do something
     break;

    now when someone adds or deletes a row (trough a form) a case should be automatically added or deleted. the "do something" is of no matter and auto-generated as well, depending on the case selected...

    I was just wondering if this was possible in any way, but I doubt it... For the time being I'm stuck... I keep turning around the same idea of making the switch / case "dynamic" If someone has a complete different solution: please feel free to shoot it.

    greetz,
    koen
    The Path of excess leeds to the tower of wisdom (W. Blake)

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing I can think of off the top of my head is to build your switch statement as a string and then run it through eval()

    There may be another solution, but I don't think we have enough information to forumlate one yet.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Enthusiast asp.da's Avatar
    Join Date
    Nov 2004
    Location
    forest
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But why do you stick to the switch? I don't know what exactly you want to do, but from what You are saying I could suggest this:

    $n = (find out the number of rows in your database);

    for ($i = 1; $i <= $n; $i++) {

    (generate whatever you want depending on the value of $i)

    }

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again it's hard to tell what you're after here... but couldn't you use an array to keep track of rows and their values
    PHP Code:
    $arr[1] = something;
    $arr[2] = this;
    $arr[3] = that;
    $arr[4] = the other thing
    you can work with that dynamically

    rather than use a switch, just pick out the array key in question. It seems like you're trying to use a switch where your goals might be better achieved some other way. A bit more info might help.


  5. #5
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cringer
    A bit more info might help.

    Let's see if i can explain in clear words what has to be achieved.

    - Someone can add trough an admin page rows on a database. These rows are in fact the spine of my navigation trough the actual site visible for regular users.
    - So if the admin adds a row (read as: button name) the navigation bar is automatically ammended

    so far so good this works like a charm

    - problem is the actual navigation. If a user clicks on a button, the site has to determine wich button was clicked and do the corresponding part of code (this is why I wanted to use the switch / case)
    The code behind every button is more or less the same (in fact it is the same for each button apart from some variable content).

    Now I know I could have used the isset() function too for example. But in any case: the actual php code has to be changed according too what the admin adds or deletes from the DB.

    (read again my first post for the example)

    see what I mean? Even with isset() as a function I must have a part of code added/deleted in the actual php code.

    what asp.da suggested is not an option since it's code I want to generate not pure and simple "output" (and then again, in his example, I would have used while() loop instead of the for() )

    I guess what beetle has suggested is the best method. I'm working on it.

    greetz & thx for the replies,
    koen
    The Path of excess leeds to the tower of wisdom (W. Blake)


Bookmarks

Posting Permissions

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