from array to switch... how to?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JaNE

    from array to switch... how to?

    I'm trying to get some data from MySql database and to create switch
    based on those data, but as an php beginer, I have no clear idea how to
    do this. have tryed many options which loked promising, but with limited
    success. here are two versions of code:

    the 1st one is executing just 1st loaded data, ie: if case is "id_001"
    it will print "data for id #001", and will, in case there is nothing in
    url like "index.php?acti on=" print "nothing" as expected. but It will
    also print that ugly "nothing" for "id_002", etc...

    <?
    //just taking something from mysql
    $query = "SELECT some_id,some_da ta FROM modules";
    $result = mysql_query ($query) or die ("Query failed ($query)");
    list($some_id,$ some_data) = mysql_fetch_arr ay($result);

    switch ($action)
    {
    case "$some_id":
    print "$some_data ";
    break;
    default:
    print "nothing";
    }
    ?>

    the 2nd one is executing all I want and "much more"..! it will in case
    of "id_003" print "yet another something about #003", but will print
    "nothing" (default statement) as much times as I have rows in my mysql
    databse...

    <?
    //just taking something from mysql
    $query = "SELECT some_id,some_da ta FROM modules";
    $result = mysql_query ($query) or die ("Query failed ($query)");
    while(list($som e_id,$some_data ) = mysql_fetch_arr ay($result))
    {
    switch ($action) {
    case "$some_id":
    print "$some_data ";
    break;
    default:
    print "nothing";
    }
    }
    ?>

    can anyone help me to find the right way to do this? (I have shortened
    code (querys) in this post, actualy, this is suposed to load different
    pages/scripts...)

    tnx
    Janko

    --
    Jan ko?
    fotografija = zapisano svjetlom | fotozine = foto-e-zin

    --
  • Pedro Graca

    #2
    Re: from array to switch... how to?

    JaNE wrote:[color=blue]
    > I'm trying to get some data from MySql database and to create switch
    > based on those data, but as an php beginer, I have no clear idea how to
    > do this. have tryed many options which loked promising, but with limited
    > success. here are two versions of code:
    >
    > the 1st one is executing just 1st loaded data, ie: if case is "id_001"
    > it will print "data for id #001", and will, in case there is nothing in
    > url like "index.php?acti on=" print "nothing" as expected. but It will
    > also print that ugly "nothing" for "id_002", etc...
    >
    ><?
    > //just taking something from mysql
    > $query = "SELECT some_id,some_da ta FROM modules";[/color]

    $query += " where some_id='$actio n'";

    So as not to get *all* rows in the database, but only those you're
    interested in.

    [color=blue]
    > $result = mysql_query ($query) or die ("Query failed ($query)");[/color]

    You need to put the following list() and switch() inside a loop; unless
    you know for sure some_id is unique in the database.
    [color=blue]
    > list($some_id,$ some_data) = mysql_fetch_arr ay($result);
    >
    > switch ($action)
    > {
    > case "$some_id":
    > print "$some_data ";
    > break;
    > default:
    > print "nothing";
    > }
    > ?>
    >
    > the 2nd one is executing all I want and "much more"..! it will in case
    > of "id_003" print "yet another something about #003", but will print
    > "nothing" (default statement) as much times as I have rows in my mysql
    > databse...
    >
    ><?
    > //just taking something from mysql
    > $query = "SELECT some_id,some_da ta FROM modules";
    > $result = mysql_query ($query) or die ("Query failed ($query)");
    > while(list($som e_id,$some_data ) = mysql_fetch_arr ay($result))
    > {
    > switch ($action) {
    > case "$some_id":
    > print "$some_data ";
    > break;
    > default:
    > print "nothing";
    > }
    > }
    > ?>[/color]

    I think this is one is almost ok. You just need to select fewer records
    from the database.


    --
    USENET would be a better place if everybody read: | to email me: use |
    http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
    http://www.netmeister.org/news/learn2quote2.html | header, textonly |
    http://www.expita.com/nomime.html | no attachments. |

    Comment

    • JaNE

      #3
      Re: from array to switch... how to?

      Pedro Graca <hexkid@hotpop. com> wrote:
      [color=blue]
      >
      > $query += " where some_id='$actio n'";
      >
      > So as not to get *all* rows in the database, but only those you're
      > interested in.
      >
      >
      >
      > You need to put the following list() and switch() inside a loop; unless
      > you know for sure some_id is unique in the database.
      >
      >
      > I think this is one is almost ok. You just need to select fewer records
      > from the database.[/color]
      ....

      well, thank you, here is how it looks now... no need for switch, if and
      else are fine enough:

      <?
      if ($action) {
      $query = "SELECT mod_name,mod_sc ript FROM modules WHERE
      mod_act='$actio n'";
      $result = mysql_query ($query) or die ("Query failed ($query)");
      if (mysql_num_rows ($result) == 1) {
      list($mod_name, $mod_script) = mysql_fetch_arr ay($result);
      include("$mod_s cript");
      }
      else {
      include("warnin g.php");
      }
      }
      else {
      include("defaul t_content.php") ;
      }
      ?>

      and it looks as is actualy working... ;-)

      of course, there is one
      $action = $_GET['action'];
      in my globals.php

      btw, is it clever to keep all possible globals in one "document" and to
      be included in my template?

      --
      Jan ko?
      fotografija = zapisano svjetlom | fotozine = foto-e-zin

      --

      Comment

      • Pedro Graca

        #4
        Re: from array to switch... how to?

        JaNE wrote:[color=blue]
        > and it looks as is actualy working... ;-)[/color]

        That's good to know.
        [color=blue]
        > of course, there is one
        > $action = $_GET['action'];
        > in my globals.php
        >
        > btw, is it clever to keep all possible globals in one "document" and to
        > be included in my template?[/color]

        It saves you some typing ... I prefer to write

        $_GET['action']

        when I need it.

        When you come back to this script (or any other) six months from now,
        are you sure you will not be tempted to change '$action'?



        In your code you had

        switch($action) {/* ... */}

        and I wondered what that '$action' was ...
        First I thought you had register_global s on and was about to say
        something about it
        .... then I decided to ignore it as it wasn't relevant :)


        If that was my code it'd read

        switch($_GET['action']) {/* ... */}

        --
        USENET would be a better place if everybody read: | to email me: use |
        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
        http://www.expita.com/nomime.html | no attachments. |

        Comment

        • Pedro Graca

          #5
          Re: from array to switch... how to?

          Pedro Graca wrote:[color=blue]
          > $query += " where some_id='$actio n'";[/color]
          oops ^^^^
          that should have been '.='


          I'm doing some learning of C++ and a lot less coding in PHP

          --
          USENET would be a better place if everybody read: | to email me: use |
          http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
          http://www.netmeister.org/news/learn2quote2.html | header, textonly |
          http://www.expita.com/nomime.html | no attachments. |

          Comment

          Working...