[FX.php List] Record Count after the fact

DC dan.cynosure at dbmscan.com
Fri Feb 16 11:25:44 MST 2007


i would recommend making temp variables and then embedding them in HTML. 
the less you directly mix HTML and PHP the happier you'll be down the 
road. it's easier to debug, read, explain, update, etc...

that said, yes, there is an "if" syntax that allows you to embed... it's 
generally useful in other places too!

here's a little example i put together to demonstrate a few little 
tricks i've picked up along the way! this is tested code

<?php
$temphtml = 'start<br>';
// create flag array same size as value array
$flag_arr = array(1,0,1,0);
$string = 'Not Showing because flag is ZERO';
$arr = array('value1','value2','value3','value4');
foreach ($arr as $key => $value) {
$temphtml .= (($flag_arr[$key]==$value)?$string:$value) . 'blah<br>';
}
echo $temphtml;
?>

note two things:

1) the string concatenation operator .= inside the foreach. allows you 
to more concisely say "append more to what's already there"

2) the alternate "if" syntax that is tucked inside parentheses. note 
that there is no need for echo because this syntax "emits" the result 
which is then picked up by the string concatenation operator. the syntax 
uses a question mark and a colon. it reminds me of filemaker's if syntax.

so, this:

if($flag_array[$key]==$value){echo $string;}else{echo $value;}

gets reduced to this:

echo ($flag_array[$key]==$value)?$string:$value;

only in the full code example i didn't use echo because it's not needed.

HTH,
dan


Jonathan Schwartz had written:
> So...there's no proper syntax for embedding an "if" statement in the 
> middle of defining a variable?
> 
> Here's what I want to do....
> 
> problem is the concatenation of 'blah' and if statement.
> 
> $temphtml = 'blah'. if($this == $that){echo $this}else{echo $that}.'blah';
> 
> 
> 
> J
> 
> 
> 
> 
> At 11:03 AM -0600 2/16/07, Bob Patin wrote:
>> That makes complete sense; I mix PHP & HTML like that all the time. 
>> Some guys don't seem to like it, but I'm used to it and it works fine 
>> in my forms.
>>
>> BP
>>
>>
>> On Feb 16, 2007, at 10:53 AM, Jonathan Schwartz wrote:
>>
>>> Hi Bob,
>>>
>>> I'm trying to write the html, including the results of the php if 
>>> statment to a variable ($temphtml).  At the end of the process, I 
>>> want to echo out the $temphtml.  The original reason for this is to 
>>> be able to count the records that are being evaluated  after 
>>> returning from the FMP query.  The reason for this was to deal with a 
>>> compound query that FMP could not handle nateively. Phew!
>>>
>>> Back to your suggestion...it doesn't look like it builds the 
>>> $temphtml variable.
>>>
>>> That being said, and still not being able to embed a php if statement 
>>> in the *middle* of building $html, I did succeed in removing all the 
>>> php if statements out of the middle of the loop and placed them at 
>>> the top of the loop, evaluated them there and set the values in 
>>> temporary variables...and then built the $temphtml in the normal way, 
>>> having eliminated the in-line if statements.
>>>
>>>
>>> $temphtml = ''
>>> foreach($searchResult['data'] as $key => $value)
>>>  {
>>>     if(this == that) { $tempdata = "xxx"}; //moved these if 
>>> statements to top.
>>>
>>>     $temphtml = '<tr><td>blah</td></tr>
>>>             <tr><td>'.$tempdata.'</td></tr>';
>>> }
>>>
>>> If there is a better way, I'd love to know it.
>>>
>>> J
>>>
>>>> Jonathan,
>>>>
>>>> Try this:
>>>>
>>>>
>>>> <?
>>>> $temphtml ='';    //set my variable for collecting the html
>>>> foreach($searchResult['data'] as $key => $value) {
>>>>     $temphtml = $temphtml;
>>>>     ?>
>>>>     <tr><td>blah</td></tr>
>>>>      <tr><td><?php if(empty($value['flag'][0] ))  { echo 
>>>> "$".$value['price'][0]; }else{ echo "Call"; }?>
>>>>     <tr><td>blah</td></tr>
>>>>     <?php
>>>> }
>>>> ?>
>>>>
>>>>
>>>> Bob Patin
>>>> Longterm Solutions
>>>> bob at longtermsolutions.com
>>>> 615-333-6858
>>>> http://www.longtermsolutions.com
>>>>
>>>>   CONTACT US VIA INSTANT MESSAGING:
>>>>      AIM or iChat: longterm1954
>>>>      Yahoo: longterm_solutions
>>>>      MSN: tech at longtermsolutions.com
>>>>      ICQ: 159333060
>>>>
>>>>>>
>>>>>> On Feb 15, 2007, at 10:53 AM, Jonathan Schwartz wrote:
>>>>>>
>>>>>>> I could do that.  However, that would then add a second for loop: 
>>>>>>> one to reduce the data set and write the results to an array and 
>>>>>>> one to write the array for display...in addition to the FMP 
>>>>>>> query. (Hey guys....do you believe that this kind of talk is 
>>>>>>> coming from me...former beginner. ;-)   ).
>>>>>>>
>>>>>>> Seems that this is particularly resource intensive,  No?
>>>>>>>
>>>>>>> Jonathan
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> At 11:25 AM -0500 2/15/07, Andy Gaunt wrote:
>>>>>>>> Jonathan,
>>>>>>>>
>>>>>>>> Rather than do a new page, can you do your for loop outside the 
>>>>>>>> HTML first
>>>>>>>> (writing everything to a new variable) then echoing this to your 
>>>>>>>> page once
>>>>>>>> completed?. If so then you could increment a counter variable 
>>>>>>>> when the
>>>>>>>> record matches your criteria ( $counter++; ) and then echo this 
>>>>>>>> out before
>>>>>>>> you echo out your content from the loop.
>>>>>>>>
>>>>>>>> Andy Gaunt
>>>>>>>> Office: 321.206.3658
>>>>>>>> Mobile: 407.810.4722
>>>>>>>> andy at fmpug.com
>>>>>>>> http://www.fmpug.com
>>>>>>>
>>>>>>> -- 
>>>>>>>
>>>>>>> Jonathan Schwartz
>>>>>>> FileMaker 8 Certified  Developer
>>>>>>> Associate Member, FileMaker Solutions Alliance
>>>>>>> Schwartz & Company
>>>>>>> jonathan at eschwartz.com
>>>>>>> http://www.eschwartz.com
>>>>>>> http://www.exit445.com
>>>>>>> 415-381-1852
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> FX.php_List mailing list
>>>>>>> FX.php_List at mail.iviking.org
>>>>>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>>>>>
>>>>>> _______________________________________________
>>>>>> FX.php_List mailing list
>>>>>> FX.php_List at mail.iviking.org
>>>>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>>>>
>>>>>
>>>>> -- 
>>>>>
>>>>> Jonathan Schwartz
>>>>> FileMaker 8 Certified  Developer
>>>>> Associate Member, FileMaker Solutions Alliance
>>>>> Schwartz & Company
>>>>> jonathan at eschwartz.com
>>>>> http://www.eschwartz.com
>>>>> http://www.exit445.com
>>>>> 415-381-1852
>>>>>
>>>>> _______________________________________________
>>>>> FX.php_List mailing list
>>>>> FX.php_List at mail.iviking.org
>>>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>>>
>>>> _______________________________________________
>>>> FX.php_List mailing list
>>>> FX.php_List at mail.iviking.org
>>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>>
>>>
>>> -- 
>>>
>>> Jonathan Schwartz
>>> FileMaker 8 Certified  Developer
>>> Associate Member, FileMaker Solutions Alliance
>>> Schwartz & Company
>>> jonathan at eschwartz.com
>>> http://www.eschwartz.com
>>> http://www.exit445.com
>>> 415-381-1852
>>>
>>> _______________________________________________
>>> FX.php_List mailing list
>>> FX.php_List at mail.iviking.org
>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
> 
> 


More information about the FX.php_List mailing list