Jump to content

Recommended Posts

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);
		} ?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/128603-solved-if-statement-help/
Share on other sites

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?

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

// 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);
}

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?

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

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);
		}

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];
            }

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.