473,513 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?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
--
Jul 17 '05 #1
4 2049
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. |
Jul 17 '05 #2
Pedro Graca <he****@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
--
Jul 17 '05 #3
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. |
Jul 17 '05 #4
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. |
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1795
by: Red | last post by:
I have an array which is dynamically generated by parsing a web page: titles Array ( => bookmarks => New Folder => New Folder2 ) and I want to insert html links into another,...
1
2523
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
1
2198
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
17
2928
by: meital | last post by:
There are three kinds of stones: red,green and blue. Each cell of the array contains one stone. The array needs to be sorted so that all the red stones will come first, then the blue ones and...
8
18863
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and...
1
1616
by: danilo of TUP | last post by:
Greetings, im Danilo, a student from TUP Manila and taking up COMPUTER SCIENCE. we are going to have a defense or we could say a project in turbo c. i just wanna know how to use the SWITCH...
38
3008
by: joeschnell | last post by:
First week of the current trimester and we've taken a huge leap from writing little code snippets of loops and switches to writing an entire program, and expecting it to work! I, nor anyone else in...
2
9006
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
4
4257
by: tokcy | last post by:
HI every one, i am using tooltip on click of link and i want like when that tooltip open then background window would be blure(). can anyone help me...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.