Jump to content

while loop containing an if else statement


harkly

Recommended Posts

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?

 

 

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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????

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.