PHP IF statement not working in EE template
Posted: 22 May 2007 08:57 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

I’m using this code in my SAEF; to auto select options that is related to the current entry_id:

<select name="field_id_11">
{exp:query sql="SELECT rel_child_id FROM exp_relationships WHERE rel_parent_id='{segment_2}'"}
{options}
<option value="{option_value}"{selected} <?php $option = "{option_value}"; $child = "{rel_child_id}"; if($option == $child) {
    
echo 'selected="selected"';
} ?>>{option_name}</option>{/options}
{
/exp:query}
</select>

I can echo both {option_value} and {rel_child_id} within my PHP code. But when I’m using IF statement, it fails.

I’ve tried to set PHP parsing to In and Out without success. Pls help.

Profile
 
 
Posted: 22 May 2007 09:18 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  22384
Joined  05-20-2002

I’m going to move this one over to ‘how to’- as it’s a better fit.  Also- can you go ahead and past the full tag code?  I’m fuzzy on how the above would work.  I’d think you’d need to do the full field in php, but it’s hard to be certain.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 22 May 2007 09:30 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Thank you Robin for your reply.

Well there’s not much in my full code. Just that my IF statement is used to pre-select the relationship dropdown menu. Dropdown is working as expected.

{exp:weblog:entries weblog="books"}
<div id="container">
{exp:weblog:entry_form weblog="books"}{custom_fields}
{if relationship}
<select name="field_id_11">
{exp:query sql="SELECT rel_parent_id,rel_child_id FROM exp_relationships WHERE rel_parent_id='{segment_2}'"}
{options}
<option value="{option_value}"{selected} <?php $option = "{option_value}"; $child = "{rel_child_id}"; if($option == $child) {
    
echo 'selected="selected"';
} ?>>{option_name}</option>{/options}
{
/exp:query}
</select>
{/if}{/custom_fields}
{
/exp:weblog:entry_form}
</div>
{/exp:weblog:entries}

The query is selecting the correct value, its just that I can’t get the IF statement to work :(

Profile
 
 
Posted: 22 May 2007 09:42 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  22384
Joined  05-20-2002

Hm- so you’re wanting to create an edit form- correct?  Have you checked out Solspaces form helper plugin?  That’s what it’s designed to do.  Though I can’t swear it handles relationships.

When I’ve hard coded edit forms, I’ve generally done them in straight php- I suspect you’re running into a parse error with the above, but can’t spot where it’s coming from.  Shouldn’t be a format issue creeping in.  But- Solspace’s plugin is the way to go if it will work for what you want.  If not, I’ll dig a little deeper into what’s up in the above.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 22 May 2007 09:47 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Actually i’ve tried Solspace plugin, but I don’t think it supports relationship fields for the time being. I’m going to try the above IF statement with a category list. I’ll post the result once I’ve finish.

Profile
 
 
Posted: 22 May 2007 10:05 AM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  22384
Joined  05-20-2002

Darn- I was hoping it would by now.  I know the above would need to have parsing set to output to work- what’s the rendered code look like in the above?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 22 May 2007 10:33 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Well at least I know IF statement is functioning properly in this category select using solspace form helper:

<select multiple="multiple" name="category[]" size="11">
{exp:form_helper:categories entry_id="{entry_id}"}
<option value="{cat_id}" {selected} <?php if("{parent_id}" != "0") { echo 'class="child"'; } ?>>{cat_name}</option>
{/exp:form_helper:categories}
</select>

Very strange. Even if I use

<select name="field_id_11">
{options}<option value="{option_value}" {selected} <?php if("{option_value}" == "356") {echo 'selected="selected"';} ?>>{option_name}</option>{/options}
</select>

its still not working.

Edit: i’ve just removed the query from the above code to show that a simple IF statement doesn’t work.

Profile
 
 
Posted: 22 May 2007 10:43 AM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  22384
Joined  05-20-2002

I don’t have good data for testing or I’d dink with it on my own site.  Hm- ok, going back to the original, I don’t see the need for the if relationships- that’s for dynamically generating things.  I’m fuzzy on what {options} is doing as well.  I’d probably generate the whole bit via php- you’ve got enough nesting going on it’s hard to follow.  Of course- it’s working with cats- so it should be doable.  But php just seems more straightforward.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 22 May 2007 11:04 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Thank you for your patience Robin. Maybe its better if I explain a little bit more.

I’m creating a Stand Alone Edit Entry form (sorry for not mentioning that earlier). I noticed form helper doesn’t support relationship; yet. That’s why I’m going with the above method.

{if relationship} is needed because my dropdown select is a relationship data. Then I use query module to fetch the related rel_child_id, and compared it to the option value.

<?php $option = "{option_value}"; $child = "{rel_child_id}"; if($option == $child) { echo 'selected="selected"';} ?>

If the current option_value is the same as rel_child_id, I will have my dropdown option selected.

I would like to go thru the full PHP route as well, but I couldn’t grasp on how to display relationship data (I know its using PHP serialize()).

Profile
 
 
Posted: 23 May 2007 04:30 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  503
Joined  07-02-2005

It’s too late for me to study this in-depth, but a couple things come to mind when I look at this.

First of all, have you tried using EE’s built-in conditionals?  Does {if option_value == rel_child_id}selected=“selected”{/if} work?

Second, if this doesn’t work, you’re going to need to run this page on PHP output so you can pass the values of {option_value} and {rel_child_id} to the PHP processor.  PHP input is for doing stuff in PHP, then passing it to EE; PHP output is for doing stuff in EE< then passing it to PHP, which is what you want to do.

You might have a parsing conflict in there… it’s really hard to tell just from looking at the code.  What happens if you move the query tag inside the PHP tags?  Can you get that to work?

Third, would inverting the positions of rel_parent_id and rel_child_id do anything?

 Signature 

– Joseph Bermann
Admin, Deep Thought
EE 1.6.4, Forum 2.0.0

Profile
 
 
Posted: 23 May 2007 06:36 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Hi Arden,

Thanks for dropping by.

First: I can’t get {if option_value == rel_child_id}selected=“selected”{/if} to work; in fact nothing comes out on the page.

Second: I’ve set PHP parsing to output, I can also echo {option_value},{rel_child_id} within the PHP code.

Third: Changing positions for rel_parent_id and rel_child_id doesn’t do anything.

How can I detect a parsing error?

Profile
 
 
Posted: 23 May 2007 07:19 AM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  503
Joined  07-02-2005
Helmi_xisnet - 23 May 2007 06:36 AM

Hi Arden,

Thanks for dropping by.

No problem, what else should I be doing in the middle of the night? wink

First: I can’t get {if option_value == rel_child_id}selected=“selected”{/if} to work; in fact nothing comes out on the page.

This happens from time to time.  This doesn’t surprise me, since it’s in the query tags.  Moving along…

Second: I’ve set PHP parsing to output, I can also echo {option_value},{rel_child_id} within the PHP code.

How can I detect a parsing error?

If you can echo the values with PHP, then you probably don’t have one.  This should also give you a clue as to the fix for your problem: instead of assigning your EE tags to PHP variables, simply compare them direclty, like so:

{exp:weblog:entries weblog="books"}
<div id="container">
{exp:weblog:entry_form weblog="books"}{custom_fields}
{if relationship}
<select name="field_id_11">
{exp:query sql="SELECT rel_parent_id,rel_child_id FROM exp_relationships WHERE rel_parent_id='{segment_2}'"}
{options}
<option value="{option_value}"{selected} <?php



if('{option_value}' == '{rel_child_id}') {
    
echo 'selected="selected"';
}



?>
>{option_name}</option>{/options}
{
/exp:query}
</select>
{/if}{/custom_fields}
{
/exp:weblog:entry_form}
</div>
{/exp:weblog:entries}

Whitespace added for emphasis, feel free to condense.  But yeah, this will probably work if you’re able to echo the values directly.

Third: Changing positions for rel_parent_id and rel_child_id doesn’t do anything.

I’m not particularly surprised, I didn’t expect it would.

 Signature 

– Joseph Bermann
Admin, Deep Thought
EE 1.6.4, Forum 2.0.0

Profile
 
 
Posted: 25 May 2007 04:10 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  64
Joined  02-23-2007

Finally, after receiving the ‘light-bulb’ moment, I managed to create a relationship dropdown that remembers your selected values.

I use pure php to get the list and embed it into the form. Some might find this useful:

Embed this into your form:

{embed="template_group/template" fid="your_field_id" wid="your_related_weblog_id" the_id="{entry_id}"}

Query to get the related child_id, entry_id and field_id:

{exp:query sql="SELECT field_id_{embed:fid}, rel_child_id FROM exp_weblog_data, exp_relationships WHERE rel_id = field_id_{embed:fid} AND rel_parent_id = '{embed:the_id}'"}
{if rel_child_id}
<?php $child_id
= '{rel_child_id}'; ?>
{
/if}
{
/exp:query}

Query to get relationship dropdown list:

<select name="field_id_{embed:fid}">
{exp:query sql="SELECT title AS author_title, ewt.entry_id AS uid FROM exp_weblog_titles AS ewt WHERE weblog_id = '{embed:wid}' ORDER BY title ASC"}
  
<option value="{uid}" <?php if('{uid}'==$child_id) { echo 'selected="selected"'; } ?>>{author_title}</option>
{/exp:query}</select>

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 10:33 AM
Total Registered Members: 62690 Total Logged-in Users: 29
Total Topics: 77269 Total Anonymous Users: 10
Total Replies: 417091 Total Guests: 179
Total Posts: 494360    
Members ( View Memberlist )