Jump to content

Archived

This topic is now archived and is closed to further replies.

dsaba

making looping variables based on mysql data

Recommended Posts

hey been having plently of problems with this thing I'm doing
let me describe what I'm trying to do:
I have a table named 'maps' and a field in there named 'mapmode_chaos', the values of that field are always either '1' or '0'
I have a looping table that displays data from the 'maps' table right now, yet I don't want to display the values of either '1' or '0' from that 'mapmode_chaos' field, I'd rather it say 'Chaos' if it has a value of '1', and rather it be null if it has a value of '0'

so basically i want to create some kind of function, if and else statement, maybe a variable, that will loop in sync with the other rows in my looping html table, and translate values of 'chaos' for when that field says '1'



I've tried unsuccesfully with diff. functions and if and else statements, but can't make it in sync with the other looping main html i'm doing.., i've pretty inexperienced in php so I'm not going to bother posting the crap I made up that didn't work, i want to start from scratch, just thinking about those other so called "functions" i made up frustrate me.....


well i think i described myself in what i want to do pretty well, could someone write a proper code to do this, whether it be a function to call on, if and else statement... etc...

thanks! (maybe after I get all this stuff downpact I can start answering questions here too)

Share this post


Link to post
Share on other sites
You can do it in the MySQL query:

[code]SELECT IF(mapmode_chaos, 'Chaos', '') as mapmode_chaos, anothercolumn, yetanothercolumn FROM maps WHERE something[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=354717:date=Mar 13 2006, 07:19 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 13 2006, 07:19 PM) [snapback]354717[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You can do it in the MySQL query:

[code]SELECT IF(mapmode_chaos, 'Chaos', '') as mapmode_chaos, anothercolumn, yetanothercolumn FROM maps WHERE something[/code]
[/quote]

ok let me better explain myself, I kick myself for not doing it before but this time I won't be lazy, lol

a look at my maps table:
map_name mapmode_chaos mapmode_sts mapmode_predator
Jungle Warfare 1 0 1
Beach Assault 1 1 1
Terror Island 0 0 1

a look at my table that loops for every record:
Map Name: Jungle Warfare
Modes: 1, 0, 1
---next record loops into the same table----
Map Name: Beach Assault
Modes: 1, 1, 1

Map Name: Terror Island
Modes: 0, 0, 1

HOWEVER I WANT THOSE 1 AND 0 VALUES TO CHANGE TO:
Map Name: Jungle Warfare
Modes: Chaos, Predator

Map Name: Beach Assault
Modes: Chaos, STS, Predator

You notice how I somehow determined whether the value of those fields was 1 or 0, and then changed it to a name I wanted which was 'Chaos' the name of the mode, when its '0', I simply display nothing and go on to the next one, all of these if and else statements or however they are loop in sync with the map name as well

What code do I need for that?

Share this post


Link to post
Share on other sites
This is purely PHP Help, but try this:

[code]$result = mysql_query("SELECT * FROM maps");
while ($row = mysql_fetch_assoc($result)) {
   echo "Map Name: " . $row['map_name'] . "<br>\n";
   $modes = array();
   if ($row['mapmode_chaos']) $modes[] = "Chaos";
   if ($row['mapmode_sts']) $modes[] = "STS";
   if ($row['mapmode_predator']) $modes[] = "Predator";
   echo "Modes: " . implode(', ', $modes) . "<br><br>\n\n";
}[/code]

Share this post


Link to post
Share on other sites
If you want MySQL-only and you're willing to live with an ugly-looking statement, try the following (UNTESTED):

[code]SELECT map_name, CONCAT_WS( ', ', IF(mapmode_chaos, 'Chaos', NULL),  IF(mapmode_sts, 'STS', NULL),  IF(mapmode_predator, 'Predator', NULL) ) AS modes FROM maps WHERE something[/code]

CONCAT_WS() ignores NULL values, so you'll only get the "on" modes.

Hope that helps.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.