dennismonsewicz Posted October 15, 2008 Share Posted October 15, 2008 How would i use the following code to check if a value in the DB equals to 1 and if so convert it to a string letter of X without having to write a bunch of if statements? <?php while($row = mysql_fetch_array($val_qry)) { $id = $row['id']; $project = $row['project']; $project_id = $row['project_id']; $pm1 = $row['pm1']; $designer1 = $row['designer1']; $pm2 = $row['pm2']; $writer = $row['writer']; $pm3 = $row['pm3']; $designer2 = $row['designer2']; $pm4 = $row['pm4']; $christine = $row['christine']; $pm5 = $row['pm5']; $designer3 = $row['designer3']; $pm6 = $row['pm6']; $julie = $row['julie']; $pm7 = $row['pm7']; $designer4 = $row['designer4']; $pm8 = $row['pm8']; $proofreading = $row['proofreading']; $pm9 = $row['pm9']; $layne = $row['layne']; $pm10 = $row['pm10']; $programming = $row['programming']; $pm11 = $row['pm11']; $testing = $row['testing']; $arr = array($id, $project, $project_id, $pm1, $designer1, $pm2, $writer, $pm3, $designer2, $pm4, $christine, $pm5, $designer3, $pm6, $julie, $pm7, $designer4, $pm8, $proofreading, $pm9, $layne, $pm10, $programming, $pm11, $testing); $excel->writeLine($val); } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 How would i use the following code to check if a value in the DB equals to 1 and if so convert it to a string letter of X without having to write a bunch of if statements? Does this apply to all $row variables? Or just certain ones? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 just certain ones. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 15, 2008 Share Posted October 15, 2008 Which are....? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 <?php $pm1 = $row['pm1']; $designer1 = $row['designer1']; $pm2 = $row['pm2']; $writer = $row['writer']; $pm3 = $row['pm3']; $designer2 = $row['designer2']; $pm4 = $row['pm4']; $christine = $row['christine']; $pm5 = $row['pm5']; $designer3 = $row['designer3']; $pm6 = $row['pm6']; $julie = $row['julie']; $pm7 = $row['pm7']; $designer4 = $row['designer4']; $pm8 = $row['pm8']; $proofreading = $row['proofreading']; $pm9 = $row['pm9']; $layne = $row['layne']; $pm10 = $row['pm10']; $programming = $row['programming']; $pm11 = $row['pm11']; $testing = $row['testing']; ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 // list vars to check $check_vars = array('pm1', 'designer1','pm2','writer','pm3','designer2','pm4','christine','pm5','designer3','pm6','julie', 'pm7','designer4','pm8','proofreading','pm9','layne','pm10','programming','pm11','testing'); while($row = mysql_fetch_array($val_qry)) { $id = $row['id']; $project = $row['project']; $project_id = $row['project_id']; // loop through variables to check. foreach($check_vars as $key ) { if(isset($row[$key]) && $row[$key] == 1) { $$key = 'X'; } } $arr = array($id, $project, $project_id, $pm1, $designer1, $pm2, $writer, $pm3, $designer2, $pm4, $christine, $pm5, $designer3, $pm6, $julie, $pm7, $designer4, $pm8, $proofreading, $pm9, $layne, $pm10, $programming, $pm11, $testing); $excel->writeLine($val); } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 two questions: 1. where are you declaring the $val var? 2. why did you use two $ on the $$key var? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 two questions: 1. where are you declaring the $val var? I don't know. It was in your code originally. 2. why did you use two $ on the $$key var? This is the syntax for variable variable's Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 oh lol sorry about that... the $val var is supposed to be the $arr var... $excel->writeLine($val); The above code writes the line into the excel spreadsheet. So how would i use your code to change the values to X? and still append the data? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 Thats what my code does. The following code takes care of that // loop through variables to check. foreach($check_vars as $key ) { if(isset($row[$key]) && $row[$key] == 1) { $$key = 'X'; } } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 well it worked kinda... Its replacing everything thats a 1 or a 0 with an X... On the first two rows it adds x's all the way up to the PM column right after the Julie column Everyone after that adds x's all the up to the PM column after the Layne column and before the programming column Updated code: $val_qry = mysql_query("SELECT * FROM has_had_projects")or die(mysql_error()); // list vars to check $check_vars = array('pm1', 'designer1','pm2','writer','pm3','designer2','pm4','christine','pm5','designer3','pm6','julie', 'pm7','designer4','pm8','proofreading','pm9','layne','pm10','programming','pm11','testing'); while($row = mysql_fetch_array($val_qry)) { $id = $row['id']; $project = $row['project']; $project_id = $row['project_id']; // loop through variables to check. foreach($check_vars as $key ) { if(isset($row[$key]) && $row[$key] == 1) { $$key = 'X'; } } $arr = array($id, $project, $project_id, $pm1, $designer1, $pm2, $writer, $pm3, $designer2, $pm4, $christine, $pm5, $designer3, $pm6, $julie, $pm7, $designer4, $pm8, $proofreading, $pm9, $layne, $pm10, $programming, $pm11, $testing); $excel->writeLine($arr); } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 15, 2008 Share Posted October 15, 2008 Change // loop through variables to check. foreach($check_vars as $key ) { if(isset($row[$key]) && $row[$key] == 1) { $$key = 'X'; } } to // loop through variables to check. foreach($check_vars as $key ) { if($row[$key] == 1) { $row[$key] = 'X'; } $$key = $row[$key]; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Author Share Posted October 15, 2008 works like a charm! Thanks! Quote Link to comment 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.