Jump to content


Photo

making looping variables based on mysql data


  • Please log in to reply
4 replies to this topic

#1 dsaba

dsaba
  • Members
  • PipPipPip
  • Advanced Member
  • 724 posts

Posted 13 March 2006 - 11:24 PM

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)

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 14 March 2006 - 12:19 AM

You can do it in the MySQL query:

SELECT IF(mapmode_chaos, 'Chaos', '') as mapmode_chaos, anothercolumn, yetanothercolumn FROM maps WHERE something


#3 dsaba

dsaba
  • Members
  • PipPipPip
  • Advanced Member
  • 724 posts

Posted 14 March 2006 - 03:04 PM

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

SELECT IF(mapmode_chaos, 'Chaos', '') as mapmode_chaos, anothercolumn, yetanothercolumn FROM maps WHERE something
[/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?

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 14 March 2006 - 06:39 PM

This is purely PHP Help, but try this:

$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";
}


#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 March 2006 - 12:37 AM

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

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

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

Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users