Jump to content

Formatting Queryed Data


inspireddesign

Recommended Posts

Okay I have a strange one here:

 

When I query the database I would like the data to be formatted in a valuable to be placed somewhere else within my application.

 

Currently I have this:

 


$myData = $row[A1] . ' ' . $row[A1a] . '</br> '
               $row[A2] . ' ' . $row[A2a] . '</br> '
               $row[A3] . ' ' . $row[A3a] . '</br> ';

echo ($myData);
  

 

This returns the data fine but I have two problems:

 

Problem 1) If the second row doesn't have data to return, it shows a blank row or a gap (which I don't want).  I would like if row2 doesn't have data to return, for row 3 to move up or stack under row1.

 

This is how the data looks now when returned.

 

Row A1 information Row A2a information

 

Row A3 information Row A3a information

 

This is how I would like the data to look when returned.

 

Row A1 information Row A2a information

Row A3 information Row A3a information

 

Problem 2) The data array $myData is going to be placed inside a word document.  When the information is passed to Word it comes in with the '</br>' in it.  I know why but don't know if there's another way to create a line break in html that wont be seen within the array I pass?  I would like when the array is created the information is all formatted properly and ready to be placed in the document.

 

Thanks so much to whoever helps.

 

Damian

 

 

 

 

Link to comment
Share on other sites

I'll deal with your second question firt. You should compile your data using logical line breaks. Then when you want to display it in HTML use nl2br() to add BR tags where there are line breaks.

 

As to the first problem, just use appropriate IF statements to build the output appropriately. I would use a function to do this since it appears you may need to run this in a loop.

 

function getMyData($dataAry)
{
    $myData[] = array();

    if ($dataAry['A1']!='' || $dataAry['A1a']!='')
    {
         $myData[] = trim($dataAry['A1'] . ' ' . $dataAry['A1a']);
    }

    if ($dataAry['A2']!='' || $dataAry['A2a']!='')
    {
         $myData[] = trim($dataAry['A2'] . ' ' . $dataAry['A2a']);
    }

    if ($dataAry['A3']!='' || $dataAry['A3a']!='')
    {
         $myData[] = trim($dataAry['A3'] . ' ' . $dataAry['A3a']);
    }

    $myDataStr = implode("\n", $myData);
    return $myDataStr;
}


while ($row = mysql_fetch_assoc($result))
{
    //Format the record data
    $myData = myData($row);
    //Output the data for HTML output
    echo nl2br($myData) . '<br>';

    //To output the data to a word doc,
    //just use $myData without the nl2br
}

Link to comment
Share on other sites

mjdamato,

 

Thanks for the code.  It seems to do the trick when the output is HTML.  But when I return the value without the nl2br($myData) the string is all one line of text without any line breaks.  I tried changing the return implode statement from "\n" to "\r\n" and "\r\n\n" but that didn't seem to help.

 

Any ideas. 

 

Thanks again for the help.

Link to comment
Share on other sites

If you try to echo it in an HTML page it will "appear" on one line, but the line breaks are there. When a browser renders HTML code it does not interpret actual line breaks (only BRs). The exceptions would be when you use a PRE tag or populate a textarea, etc. If you look at the actual HTML code that is genmerated you will see that the line breaks are there. It will work fine as long as you use the value (without the nl2br() ) in a container that will interpret them - as I would expect your process of populating a word document would do.

 

When I used the code above like this:

    //Format the record data
    $myData = getMyData($row);
    //Output the data NOT for HTML output
    echo $myData;

 

The "rendered" HTML in the browseer looks like this:

Alpha1 Alpha1a Alpha2 Alpha2a Alpha3 Alpha3a

 

However, if you look at the actual HTML code, it looks like this

Alpha1 Alpha1a
Alpha2 Alpha2a
Alpha3 Alpha3a

 

That is how you should want your data - keep it in it's most basic format and the transform the data based upon the format of the output. (such as using nl2br() when outputin onto an HTML page)

Link to comment
Share on other sites

Thanks for all your help on this one.

 

I don't have anything in my class for interpreting the line breaks and I wouldn't have any idea where I would put it.  I'm really just placing data where a %%var%% is called within the Word Document.  In this case a string called $myData. Of course I can have more than one. Can you help me determine the best place to put the line break in the modifier?  Thanks again.  Maybe this isn't the right place to handle the line break. I don't know.

 

function modifier($vars, $rftfile) {

        $xchange = array ('\\' => "\\\\",
                             '{'  => "\{",
                               '}'  => "\}");

        $document = file_get_contents($rftfile);
        if(!$document) {
            return false;
        }

        foreach($vars as $key=>$value) {
            $search = "%%".strtoupper($key)."%%";

            foreach($xchange as $orig => $replace) {
                $value = str_replace($orig, $replace, $value);
            }
            $document = str_replace($search, $value, $document);
        }
        return $document;
    }

// Write the values to the file:

		$wFileName	 = 'writeNewFile.docx';

		$vars = array('var' => $myData ); 

		$new_rtf = modifier($vars, 'orgFileName.docx');

		$fr = fopen($wFileName, 'w') ;
		fwrite($fr, $new_rtf);
		fclose($fr);

 

 

Link to comment
Share on other sites

I think my modifier function is removing the \n from the NICE array you created.  I can't seem to figure out why it's not passing the line return correctly.  I can get it to work with this code just fine.  I noticed however that if the var has single quotes it doesn't work but if I have double quotes the line returns work great.  Try it out.

 

 

$File = "YourFile.rtf";

$Handle = fopen($File, 'w');
$Data = "Jane Doe\n TryThis\n And this";

fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";

fwrite($Handle, $Data);
print "Data Written";

fclose($Handle);

 

 

 

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.