2

I'm using PHPExcel to export data from a MYSQL database and import it into an excel file.

I've got a database that looks like this (this is also what my excel file currently looks like when I generate it using PHPExcel):

======================================================
|           Question                  |   Answer     |
=========+===========+===============================|
| Do you listen to music?             |    YES       |            
|----------------------------------------------------|
| Who is your favorite music artists? | Justin Beiber| 
|----------------------------------------------------|
| <p> Select an Answer<p>             |              | 
|----------------------------------------------------|
|Are you a Male or female             |      M       | 
|----------------------------------------------------|

I want my excel file to look like this:

======================================================
|           Question                  |   Answer     |
=========+===========+===============================|
| Do you listen to music?             |    YES       |            
|----------------------------------------------------|
| Who is your favorite music artists? | Justin Beiber| 
|----------------------------------------------------|
|Are you a Male or female             |      M       | 
|----------------------------------------------------|

I'm using this code:

$objPHPExcel = new PHPExcel();

$col = 1; 
while($row_data = mysql_fetch_assoc($result)) {
    $row = 1;
    if ($col == 1) {
        $row_headings = array_keys($row_data);
        foreach($row_headings as $value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $row++;
        }
        $row = 1;
        $col++;
    }
    foreach($row_data as $value) {
      if (!strstr('<p>', $value)){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);

    $row++;
         }
    }
    $col++;
}

1 Answer 1

0

One option would be to exclude the <p> Select an Answer<p> when doing a SQL query you would SELECT your columns and rows then from your selection you could DELETE WHERE question = "<p> Select an Answer<p> "

Or if column Anwser value is null or "" when Question value is <p> Select an Answer<p>, then you can could also say:

if($value !== null)
{
  // Output...
} 

In example:

$objPHPExcel = new PHPExcel();

$col = 1; 
while($row_data = mysql_fetch_assoc($result)) {
    $row = 1;
    if ($col == 1) {
        $row_headings = array_keys($row_data);
        foreach($row_headings as $value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $row++;
        }
        $row = 1;
        $col++;
    }
    foreach($row_data as $value) {
        if($value !== null || $value !== '')
        {
         $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
         $row++;
        }
    }
    $col++;
}

Or you could simply delete rows that contain <p> Select an Answer<p> unless you need them.

Sign up to request clarification or add additional context in comments.

8 Comments

The problem is that there are multiple fields in the db that contain other html instruction statements like <p> Fill out survey <p> so I couldn't just take out the <p> Select an Answer <p> field.. Also, the db cannot be altered at this point. Is there a way I could take out fields that start with html code such as- if($value !== <p>) {write to excel file}
Have you tried my example on your code should exclude any row's from your database that have Anwser column with and empty value or null value. Otherwise you can allways just say if(!strstr('<p>', $value)) { ... } and it should not output any rows to excel that containt that tag. That is if all entries in your database are like that, that question contains some html and anwser contains nothing.
I've updated my code. I added the if(!strstr('<p>', $value)) { ... } after the foreach, but I'm still getting the same output.
Could you tell me if there is there anything wrong with my code?
Can you post entire php file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.