Jump to content

[SOLVED] Concatenation of IF and FOR EACH statement...


Thetcm

Recommended Posts

I'm working on a web site that uses Ajax to retrieve information from our server. I return a simple string that I assign on my website's inner HTML. Now when I create that string I include a bunch of information from our database(It's a FileMaker database, altough that is of no importance).

 

Calls that take 1 lines and enclosed in a nl2br function, so the $string='text'.nl2br().'text'; actually works without any problem.

 

I start getting problems with bigger lines that include IF and FOR EACH statement. It seems PHP is not a big fan of

 

$string='text'.if(){}.'text';

 

I've been enclosing the result of IF function in a separated variable and including this in my string, but that means there's more line codes than necessary. I tried enclosing the whole IF in a nl2br function but to no avail.

 

My problem is kind of solved when I insert the information in a variable before building the string, but there must be a way to pull this off directly with an IF statement?

 

Thanks.

Link to comment
Share on other sites

I *think* I understand your problem. Try using the ternary operator (If/Else shorthand)

 

$var = 'foo';
$string='Start'.(($var=='foo')?'-TRUE-TEXT-':'-FALSE-TEXT-').'End';
echo $string;
//Output: Start-TRUE-TEXT-End

$var = 'bar';
$string='Start'.(($var=='foo')?'-TRUE-TEXT-':'-FALSE-TEXT-').'End';
echo $string;
//Output: Start-FALSE-TEXT-End

Link to comment
Share on other sites

The big problem is that there's also a FOR EACH statement in the IF...

 

                    <td>'. 
                    $relatedRecords = $record->getRelatedSet("relatedSetName");
                    if (FileMaker::isError($relatedRecords) === false) {
                        $master_record = $record;
                        foreach ($relatedRecords as $record) {
                            echo nl2br($record->getField('fieldName::Language', 0));			
                        }	
                        $record = $master_record;	
                    } 
                    .'</td>

 

That's what I'm trying to concatenate. And yes, I know I'm using tables. Never mind that.

Link to comment
Share on other sites

I've reread your post a few times and I'm wondering "why" you need to take this approach? I always appreciate compact code, but not at the expense of readability and useability.

 

If this is server-side code to generate the response for an AJAX call you don't have to echo all of the text in one line.

 

However, if you need to run nl2br() on more than one array, you may want to create a function to do that and return the array as a single string using implode

 

function arrayToString($array)
{
  foreach ($array as $key => $value)
  {
    $array[$key] = nl2br();
  }
  return implode('', $array);
}

Link to comment
Share on other sites

That's because I'm not simply returning a few line of datas. I'm returning a complete page along with divs and styles and then I simply insert it into the awaiting DIV through innerHTML.

 

Imagine a list of items, when you click on one, the result appears on top of the list. It's a page that contains a lot of information. At the top of that "new" page, there are tabs that represent the different sections of that group of information.

 

It used to be a completely different page altogether. You'd click on the list item and it would bring you to the first page of the information and each tab would be a completely different page too. I'm trying to make it more useful by loading all the information at once(tests proved that loading all at once was as quick as each page separately), stuffing that information into divs called section1, section2, section3, etc. And make the content change when you click on a tab, which is simply changing the innerHTML with another pre-made DIV.

 

So with the AJAX call, I'm basically rebuilding the whole page with divs that are set to display:none.

 

As far as I know I can't send back more than one response with an AJAX call, which is why I am sending back one big string. It loads instantly right now, so speed isn't really an issue.

 

Then I reached the FileMaker PHP API calls that were done with IFs and FOR EACHs and that's when I sorta hit a brick wall.

 

I can PM you links if you want to see exactly what I'm trying to accomplish? I don't really want to post links to developmental code, especially with pages like this that are given strictly to our clients and not available anywhere else.

Link to comment
Share on other sites

As far as I know I can't send back more than one response with an AJAX call, which is why I am sending back one big string.

 

Yes, but...

 

You can send back multiple responses as one response! I've done something similar in the past. So, let's say you have 10 divs on the page: div1, div2, etc. Then let's say there is a button on the page that will make an AJAX call and the response from that AJAX call needs to update one or more of the divs but the JS code won't know ahead of time how many or which ones - it will be determined when teh AJAX call is received by the server-side code and is processed.

 

Here is one solution:

When the call is made to the server-side page and logic is run to determine what needs to be returned to the JavaScript, the server-side code will determine which divs need to be updated and return a single sting that the javascript will parse and populate the appropriate divs.

 

Here's an example:

 

The PHP code will determine which Divs need to be updated and will return the entire set of data in this format:

div1:div 1 content|div3:This content goes in div 3|div8:And of course,this goes in div8

 

The JavaScript will then parse the return value and populate the divs accordingly such as this

// returnVal is the value returned fromt eh AJAX call
//Split the return value into an array of name/value pairs for each div
var divs = returnVal.split('|');

//Iterate through each div name/value pair
for(var i=0; i<divs.length; i++)
{
    //Split the div name/value pair into it's parts
    divParts = divs[i].split(':');
    //Poulate the div with it's content
    document.getElementById(divParts[0]).innerHTML = divParts[1];
}

 

This was just an example. Youwould need to make sure that whatever characters you use to deliniate the data would not be included in your data. You could always use quotes around the text, control characters, etc.

 

Hope this helps.

Link to comment
Share on other sites

Ok, I get your drift. Get all the information with AJAX and insert them into their specific DIVs that are already on the page. That would be the opposite of what I do right now, which is create the divs through AJAX then send the string to my Javascript that inserts it into the container div.

 

The big problem here is that I have over 250 different fields that I get from my database that are put into specific TDs or DIVs to form the layout. I'd need over 350 different IDs to associate the information at the right place.

 

And if I separate them in sections and send my 9 different sections, I'm back at the beginning, where I have problem concatenating an IF that has a FOR EACH inside.

 

I'm not sure which solution is the best at this point. Mine is simple, considering I'm passing the whole extra page. Yours is, I guess, more friendly since you simply gather data but in my case it would be FAR too complicated. I can't have 250 divs running around with names like "div1". I would get lost rather easily or the code would be ultra commented, making the file larger than necessary. It would also be pretty hard to go back and delete a field if necessary. With my method there's no need for multiple IDs since I'm sending the design aswell as the information.

 

What I can tell you is that there's no might or might not be updated. Every field contains something.

 

Is there no way to concatenate an IF that contains a FOR EACH? I would keep my bulky string if I could just do that. There's also one section I haven't done that I'm not even sure if I'm method will work considering the FileMaker call is quite weird.

 

Link to comment
Share on other sites

To be honest, I still don't completely understand what exactly you are doing. My suggestion above was only that - it can be modified to fit your particular needs (if it makes sense). For example, you don't have to use Div1, Div2, etc. You could have descriptive names for them if need be.

 

But, building the entire page should be straitforward as well. I guess I'm just not following "why" you need to create the text in a single line of code. Could you not do somthing like this:

$responseText = '<td>';
$relatedRecords = $record->getRelatedSet("relatedSetName");
if (FileMaker::isError($relatedRecords) === false)
{
    $master_record = $record;
    foreach ($relatedRecords as $record)
    {
        $responseText .= nl2br($record->getField('fieldName::Language', 0));			
    }	
    $record = $master_record;	
} 
$responseText .= '<td>';

//Perform additional operations as needed and add text to the
//$responseText variable to be returned through the AJAX call

//Output the entire response text for AJAX response
echo $responseText

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.