This is a discussion on from array to switch... how to? within the PHP Language forums, part of the PHP Programming Forums category; I'm trying to get some data from MySql database and to create switch based on those data, but as ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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?action=" 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_data FROM modules"; $result = mysql_query ($query) or die ("Query failed ($query)"); list($some_id,$some_data) = mysql_fetch_array($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_data FROM modules"; $result = mysql_query ($query) or die ("Query failed ($query)"); while(list($some_id,$some_data) = mysql_fetch_array($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? http://fotozine.org -- |
|
|||
|
JaNE wrote:
> 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?action=" 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_data FROM modules"; $query += " where some_id='$action'"; So as not to get *all* rows in the database, but only those you're interested in. > $result = mysql_query ($query) or die ("Query failed ($query)"); You need to put the following list() and switch() inside a loop; unless you know for sure some_id is unique in the database. > list($some_id,$some_data) = mysql_fetch_array($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_data FROM modules"; > $result = mysql_query ($query) or die ("Query failed ($query)"); > while(list($some_id,$some_data) = mysql_fetch_array($result)) > { > switch ($action) { > case "$some_id": > print "$some_data"; > break; > default: > print "nothing"; > } > } > ?> 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. | |
|
|||
|
Pedro Graca <hexkid@hotpop.com> wrote:
> > $query += " where some_id='$action'"; > > 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. .... 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_script FROM modules WHERE mod_act='$action'"; $result = mysql_query ($query) or die ("Query failed ($query)"); if (mysql_num_rows($result) == 1) { list($mod_name,$mod_script) = mysql_fetch_array($result); include("$mod_script"); } else { include("warning.php"); } } else { include("default_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? http://fotozine.org -- |
|
|||
|
JaNE wrote:
> and it looks as is actualy working... ;-) That's good to know. > 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? 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_globals 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. | |
|
|||
|
Pedro Graca wrote:
> $query += " where some_id='$action'"; 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. | |