dsaba Posted March 13, 2006 Share Posted March 13, 2006 hey been having plently of problems with this thing I'm doinglet 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) Quote Link to comment https://forums.phpfreaks.com/topic/4876-making-looping-variables-based-on-mysql-data/ Share on other sites More sharing options...
wickning1 Posted March 14, 2006 Share Posted March 14, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4876-making-looping-variables-based-on-mysql-data/#findComment-17199 Share on other sites More sharing options...
dsaba Posted March 14, 2006 Author Share Posted March 14, 2006 [!--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, lola look at my maps table:map_name mapmode_chaos mapmode_sts mapmode_predatorJungle Warfare 1 0 1Beach Assault 1 1 1Terror Island 0 0 1a look at my table that loops for every record:Map Name: Jungle WarfareModes: 1, 0, 1---next record loops into the same table----Map Name: Beach AssaultModes: 1, 1, 1Map Name: Terror IslandModes: 0, 0, 1HOWEVER I WANT THOSE 1 AND 0 VALUES TO CHANGE TO:Map Name: Jungle WarfareModes: Chaos, PredatorMap Name: Beach AssaultModes: Chaos, STS, PredatorYou 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 wellWhat code do I need for that? Quote Link to comment https://forums.phpfreaks.com/topic/4876-making-looping-variables-based-on-mysql-data/#findComment-17379 Share on other sites More sharing options...
wickning1 Posted March 14, 2006 Share Posted March 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4876-making-looping-variables-based-on-mysql-data/#findComment-17464 Share on other sites More sharing options...
fenway Posted March 15, 2006 Share Posted March 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4876-making-looping-variables-based-on-mysql-data/#findComment-17627 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.