harkly Posted January 14, 2010 Share Posted January 14, 2010 I am pulling info from the db and need it to print out the values. My problem is I am not sure how to get what I need. Here is my table userID VARCHAR(32) NOT NULL PRIMARY KEY, asian TINYINT(1), black TINYINT(1), east_indian TINYINT(1), mid_eastern TINYINT(1), hispanic TINYINT(1), native TINYINT(1), pac_haw TINYINT(1), white_eu TINYINT(1), othr_Ethn TINYINT(1), othr_Ethn_txt VARCHAR(32) each one contains a 1 or null, 1 if true I want to be able to display all with the value of 1 ie. Black, Native Indian I know I need a while loop; $sql = mysql_query("SELECT * FROM ethn WHERE userId = 'test'"); while($r = mysql_fetch_array($sql)) { And I am thinking I need an if else statment to get my output if ($asian == 1){ echo " Asian \n";} else if ($black == 1) echo " Black \n";} Am I even close and if so how do I connect them? Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/ Share on other sites More sharing options...
daydreamer Posted January 14, 2010 Share Posted January 14, 2010 use curly braces: <?php while(..){ if(...){ }else{ } } ?> Although I would recommend learning how to pull just the data you need using the mysql query, using a WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995083 Share on other sites More sharing options...
harkly Posted January 14, 2010 Author Share Posted January 14, 2010 So you think I should do this? $sql = mysql_query("SELECT * FROM ethn WHERE userId = 'test' AND asian = "1" AND black = '1' ...etc "); Don't I still have to do the while/if statement?? Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995094 Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 If you want to just pull only asians and blacks, or something like that, than I would suggest using WHERE clause. if you just want to list everyone, and say what race they are, than select the columns you needs and leave out the Where clause (since you want every entry) and use the if statements Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995100 Share on other sites More sharing options...
harkly Posted January 14, 2010 Author Share Posted January 14, 2010 if you just want to list everyone, and say what race they are, than select the columns you needs and leave out the Where clause (since you want every entry) and use the if statements That is what I was thinking. What about using a foreach? Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995108 Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 a foreach is used with arrays. In this case, I don't think a foreach would be the best option, but what exactly were you intending to do with a foreach? which array would you iterate through? Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995109 Share on other sites More sharing options...
harkly Posted January 14, 2010 Author Share Posted January 14, 2010 I was just checking out the while loop on php.net and they used a foreach which gives the out put with out a comma at the end. But they also used a nested while loop to get the same results. All I want is to have my out put so that there is a comma between them and none at the end. Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995113 Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 you can do that a fews. The easiest (well easiest IMO) would be to just concatenate (or basically mush together) a string in the loop and just trim the last comma from it. while(...){ $string = "";//create an empty string if (..){ $string .= "Black, "; } if (..) { $string .= "Asian, "; //etc.. //trim last comma and space $string = rtrim($string, ", ");//use rtrim because we only want to trim from the right }//end while but there are multiple ways of doing it (and the method is pretty much the same (or similar) with the foreach or while) Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995114 Share on other sites More sharing options...
harkly Posted January 15, 2010 Author Share Posted January 15, 2010 Thanks! This way works great!! Is there any way to get this working in it? if ($othr_Ethn == 1) $string .= " $othr_Ethn_txt, "; Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995249 Share on other sites More sharing options...
laffin Posted January 15, 2010 Share Posted January 15, 2010 Not shure why ya used tinyints, but based on your db, i would prolly opt for something like $ethnics=array('Asian,'Black','East Indian','Mid Eastern','Hispanic','Native','Pac Haw','White EU'); $string=null; if($row=mysql_fetch_assoc(mysql_query("SELECT * FROM ethn WHERE userId = 'test'"))) { $user_eth=array_values(array_pop(array_shift($row))); foreach($user_eth as $key=>$val) { if($val) $string[]=isset($ethnics[$key])?$ethnics[$key]:$row['othr_Ethn_txt']; } $string=implode(', ',$string); } if(empty($string)) $string="Unknown'; Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995259 Share on other sites More sharing options...
harkly Posted January 15, 2010 Author Share Posted January 15, 2010 Yes, I will need to go in and change some of my TinyInt to Boolean. I would rather try to get the one code working instead of starting something different, pretty new at this. Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995297 Share on other sites More sharing options...
harkly Posted January 15, 2010 Author Share Posted January 15, 2010 Can anyone help me get this working? I need the data in $othr_Ethn_txt to print out if ($othr_Ethn == 1) $string .= " $othr_Ethn_txt, "; This is the full code: $sql = mysql_query("SELECT * FROM ethn WHERE userId = 'test'"); while($r = mysql_fetch_array($sql)) { $string = ""; if ($asian == 1) $string .= "Asian, "; if ($black == 1) $string .= "Black/African, "; if ($east_indian == 1) $string .= "East Indian, "; if ($mid_eastern == 1) $string .= "Middle Eastern, "; if ($hispanic == 1) $string .= "Latino/Hispanic, "; if ($native == 1) $string .= "Native American, "; if ($pac_haw == 1) $string .= "Pacific Islander/Hawaiian, "; if ($white_eu == 1) $string .= "White/Caucasian, "; if ($othr_Ethn == 1) $string .= " $othr_Ethn_txt, "; $string = rtrim($string, ", "); echo " $string \n"; } Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995713 Share on other sites More sharing options...
harkly Posted January 16, 2010 Author Share Posted January 16, 2010 I changed my code to see if I could get it to work but it still will not pull the data from the table for $othr_Ethn_txt $sql = mysql_query("SELECT * FROM ethn WHERE userId = 'kelly'"); while($r = mysql_fetch_array($sql)) { $string = ""; if ($r['asian']) $string .= "Asian, "; if ($r['black']) $string .= "Black/African, "; if ($r['east_indian']) $string .= "East Indian, "; if ($r['mid_eastern']) $string .= "Middle Eastern, "; if ($r['hispanic']) $string .= "Latino/Hispanic, "; if ($r['native']) $string .= "Native American, "; if ($r['pac_haw']) $string .= "Pacific Islander/Hawaiian, "; if ($r['white_eu']) $string .= "White/Caucasian, "; if ($r['othr_Ethn']) $string .= "$othr_Ethn_txt"; $string = rtrim($string, ", "); echo " $string \n"; } I know I am missing something but don't have a clue, can anyone help???? Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995949 Share on other sites More sharing options...
Buddski Posted January 16, 2010 Share Posted January 16, 2010 if ($r['othr_Ethn']) $string .= $r['othr_Ethn_txt']; Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-995977 Share on other sites More sharing options...
harkly Posted January 16, 2010 Author Share Posted January 16, 2010 WORKED!! Thanks so much!! Quote Link to comment https://forums.phpfreaks.com/topic/188487-while-loop-containing-an-if-else-statement/#findComment-996109 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.