Jump to content

Too Many If Statements


kn1dmay1
 Share

Recommended Posts

I am working on a page that pulls data from a SQL database then displays the data with the echo command. I want to echo the database values only if there is something there. I know I can write an IF statement for every line I am pulling like this:If ($data[23] != "") { echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[23]></center><br>"; }If ($data[24] != "") { echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[24]></center><br>"; }So if it does not have anything it will just move on. My question is I have like 71 variables so that would be 71 IF statements. I know there has to be a way to do this without all the IF statements with like a while or loop or something but I cannot find anything on it. Does anyone have any suggestions?Thanks in advance!Doug

Edited by Dmayes
Link to comment
Share on other sites

I am working on a page that pulls data from a SQL database then displays the data with the echo command. I want to echo the database values only if there is something there. I know I can write an IF statement for every line I am pulling like this:If ($data[23] != "") { echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[23]></center><br>"; }If ($data[24] != "") { echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[24]></center><br>"; }So if it does not have anything it will just move on. My question is I have like 71 variables so that would be 71 IF statements. I know there has to be a way to do this without all the IF statements with like a while or loop or something but I cannot find anything on it. Does anyone have any suggestions?Thanks in advance!Doug
try something like...
$i = 0;while($i<71){  If ($data[$i] != "")	{	  echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[24]></center><br>";	}	$i += 1;}

Edited by smerny
Link to comment
Share on other sites

I noticed you have the number in the echo as well, sorry, try this

$i = 0;while($i<71){  If ($data[$i] != "")	{	  echo "<center><img src=http://midwest.eusweb.local/sitesurveys/$data[0]/pictures/$data[$i]></center><br>";	}	$i += 1;}

Link to comment
Share on other sites

Here is the whole code. I tried the first suggestion but it displayed it 71 times.

 	echo " <b>Store Name:</b> <font color=#434381><b>$data[1]</b></font><br>";	echo " <b>Store ID:</b> $data[0]<br>";	echo " <b>Main Phone:</b> $data[2]<br>";	echo " <b>Manager:</b> $data[4]<br>";	echo " <b>Address:</b> $data[5]<br>";	echo " <b>City:</b> $data[6]<br>";	echo " <b>State:</b> $data[7]<br>";	echo " <b>Zip Code:</b> $data[8]<br>";	echo " <b>Cost Center:</b> $data[9]<br>";	echo " <b>Circuit ID #1:</b> $data[10]<br>";	echo " <b>Circuit ID #2:</b> $data[11]<br><br>";	echo " <b><u>   -- Telco --	</b></u><br><br>";	echo " <b>Main Phone:</b> $data[2]<br>";	echo " <b>Hunt #2:</b> $data[27]<br>";	echo " <b>Hunt #3:</b> $data[28]<br>";	echo " <b>Hunt #4:</b> $data[29]<br>";	echo " <b>Hunt #5:</b> $data[30]<br>";	echo " <b>Hunt #6:</b> $data[31]<br>";	echo " <b>Hunt #7:</b> $data[32]<br>";	echo " <b>Hunt #8:</b> $data[33]<br>";	echo " <b>Fax:</b> $data[3]<br>";	echo " <b>TC/SP :</b> $data[35]<br>";	echo " <b>Rad #:</b> $data[34]<br><br>";	echo " <b><u>   -- Scope --	</b></u><br><br>";	echo " <b>DHCP Range:</b> $data[12] - $data[71]<br>";	echo " <b>Store Router IP:</b> $data[36]<br>";	echo " <b>Store Printer IP 1 :</b> $data[37]<br>";	echo " <b>Store Printer Name 1:</b> $data[38]<br>";	echo " <b>Store Printer Model:</b> $data[39]<br>";	echo " <b>Store Printer IP 2 :</b> $data[40]<br>";	echo " <b>Store Printer Name 2:</b> $data[41]<br>";	echo " <b>Store Printer Model 2:</b> $data[42]<br>";	echo " <b>Store Printer IP 3 :</b> $data[43]<br>";	echo " <b>Store Printer Name 3:</b> $data[44]<br>";	echo " <b>Store Printer Model 3:</b> $data[45]<br>";	echo " <b>CSU IP #1:</b> $data[46]<br>";	echo " <b>CSU IP #2:</b> $data[47]<br>";	echo " <b>Call Pilot IP:</b> $data[13]<br>";

I want to be able to check each one and if one does not exist skip it.

Link to comment
Share on other sites

If you want to do it within a loop, you could put the left column items in the database as well, then do something like..

while ($row = mysql_fetch_array($result)){  echo " <b>".$row['item']."</b> ".$row['data']."<br>";}

how are your tables set up?

Link to comment
Share on other sites

The table is a simple table with the field headings and data. the Fields are like store_fax and store_id so I think that this would not work because if I am reading it right it would display store_fax 865-555-1212 instead of Store Fax#: 865-555-1212

Link to comment
Share on other sites

Well you could have a table store data strings of "Store Fax#:" etcotherwise you'd have to have it all in the php like you do... would be like this: echo ($data[1] == "" ? "" : " <b>Store Name:</b> <font color=#434381><b>$data[1]</b></font><br>"); echo ($data[0] == "" ? "" : " <b>Store ID:</b> $data[0]<br>")?etc

Link to comment
Share on other sites

When I tried this if there was data there it displayed it and if not it did not. This is exactly what I wanted. Thanks! I am not sure why it works though. Can you explain what this does?

$data[1] == "" ? "" :

What does the : do?Thanks very much for your help!!!!!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...