JohnMac328
asked on
Problem with syntax
I am trying to add some code to an exsisting form. Here is the form - The lines of code I am trying to add are the sections with //Change button text from default as the comment - 15 to 21 and 30 - 36 - When I try to add the code I don't get an error but the products that did display on the page no longer display
$html .='<div class="well well-sm clearfix">
<div class="pull-right">';
if($row['variable'] > 0){
// must fill form before adding product to cart
$html .='<form action="custom-form" method="post">
<input type="hidden" name="Item_item_id" value="' .$row['item_id']. '" />
<input type="hidden" name="qty" value="1" />
' .$this->show_list('list_product_form_hooks.', array(
"view_where"=>"item_active=1
AND associated_item_id='" .$row['item_id']. "'
AND associated_lid='" .$row['lid']. "'")). '
//Change button text from default
$custom_cart_button_text = $row['custom_cart_button_text']
$custom_cart_button_text
= !empty($custom_cart_button_text)
? $custom_cart_button_text
: 'Add to Cart';
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
</form>';
} else {
// add product to cart as normal
$html .='<form action="custom_form" method="post">
<input type="hidden" name="Item_item_id" value="' .$row['item_id']. '" />
<input type="hidden" name="qty" value="1" />
//Change button text from default
$custom_cart_button_text = $row['custom_cart_button_text']
$custom_cart_button_text
= !empty($custom_cart_button_text)
? $custom_cart_button_text
: 'Add to Cart';
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
</form>';
}
$html .='</div>
</div>';
ASKER
It's inside a CMS system which does not allow that option
This section:
Can be re-written as:
$custom_cart_button_text = $row['custom_cart_button_text']
$custom_cart_button_text
= !empty($custom_cart_button_text)
? $custom_cart_button_text
: 'Add to Cart';
Can be re-written as:
$custom_cart_button_text = isset($row['custom_cart_button_text']) && !empty($row['custom_cart_button_text']) ? $row['custom_cart_button_text'] : 'Add to Cart';
regarding the error reporting:
ok, well, then we can't say there are no errors ;)
ok, well, then we can't say there are no errors ;)
ASKER
True - this one is difficult to see :) - I thought your suggestion might work but it didn't.
oh wait, i think i see the problem. you're injecting php into the html string. that isn't going to work. hold on while i figure out where the quotes should go.
So the easiest way to fix this would be to just move your new code out of section that's creating the string for $html.
In fact, it appears you can do the variable assignment on line 4 of your code:
Then your if/else looks like:
To explain:
this code is building an html string. You can't just put PHP in the middle of it. Since you're referencing the same code twice, it's best to consolidate that into one piece of code, which is why I moved it above the if/else statement. It's also easier than trying to mess with where the html string ends and the PHP concatenates to the string. blah. I hate trying to track down missing quotes / parentheses / etc.
In fact, it appears you can do the variable assignment on line 4 of your code:
$custom_cart_button_text = isset($row['custom_cart_button_text']) && !empty($row['custom_cart_button_text']) ? $row['custom_cart_button_text'] : 'Add to Cart';
Then your if/else looks like:
if($row['variable'] > 0){
// must fill form before adding product to cart
$html .='<form action="custom-form" method="post">
<input type="hidden" name="Item_item_id" value="' .$row['item_id']. '" />
<input type="hidden" name="qty" value="1" />
' .$this->show_list('list_product_form_hooks.', array(
"view_where"=>"item_active=1
AND associated_item_id='" .$row['item_id']. "'
AND associated_lid='" .$row['lid']. "'")). '
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
</form>';
} else {
// add product to cart as normal
$html .='<form action="custom_form" method="post">
<input type="hidden" name="Item_item_id" value="' .$row['item_id']. '" />
<input type="hidden" name="qty" value="1" />
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
</form>';
}
To explain:
this code is building an html string. You can't just put PHP in the middle of it. Since you're referencing the same code twice, it's best to consolidate that into one piece of code, which is why I moved it above the if/else statement. It's also easier than trying to mess with where the html string ends and the PHP concatenates to the string. blah. I hate trying to track down missing quotes / parentheses / etc.
ASKER
This is actually the very first line in the code -
$html .='<div class="well well-sm clearfix">
so the new code goes above this line? I did not see where you added the new code except for
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
$html .='<div class="well well-sm clearfix">
so the new code goes above this line? I did not see where you added the new code except for
<input type="submit" name="submit" value="' . $custom_cart_button_text . '">
It's not about adding those lines of code. You're missing semicolons ";" on lines 16 and 31 (where "$custom_cart_button_text = $row['custom_cart_button_t ext']" is).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect - Thanks
@JohnMac328 -
Putting that line of code on line 4 should have worked as well. Lines 1 - 3 of your code were:
Which means line 4 can be PHP.
In the future, you can help narrow down syntax errors like the ones that were causing your problem by copying your code into a PHP IDE, like Netbeans.
You can see the error in your original code:
And you can see the code I suggested is clear of syntax errors:
One other point: The solution you accepted has the same block of code repeated twice, which is generally considered to be something you should avoid.
Putting that line of code on line 4 should have worked as well. Lines 1 - 3 of your code were:
$html .='<div class="well well-sm clearfix">
<div class="pull-right">'; //the html string is terminated here
Which means line 4 can be PHP.
$html .='<div class="well well-sm clearfix">
<div class="pull-right">';
$custom_cart_button_text = isset($row['custom_cart_button_text']) && !empty($row['custom_cart_button_text']) ? $row['custom_cart_button_text'] : 'Add to Cart';
In the future, you can help narrow down syntax errors like the ones that were causing your problem by copying your code into a PHP IDE, like Netbeans.
You can see the error in your original code:
And you can see the code I suggested is clear of syntax errors:
One other point: The solution you accepted has the same block of code repeated twice, which is generally considered to be something you should avoid.
@Brian Tao-
While it's true those lines are missing semi colons, that is not the cause of the original problem. Adding semi colons would not resolve the problem.
The problem was caused by PHP being used inside a string of HTML. There were missing quotes. Moreover, the code on lines 16 and 31, and the subsequent lines below, could have been consolidated into one line. Multiple assignments were being done when only 1 was needed, which was my point in this comment.
While it's true those lines are missing semi colons, that is not the cause of the original problem. Adding semi colons would not resolve the problem.
The problem was caused by PHP being used inside a string of HTML. There were missing quotes. Moreover, the code on lines 16 and 31, and the subsequent lines below, could have been consolidated into one line. Multiple assignments were being done when only 1 was needed, which was my point in this comment.
ASKER
Correct - I will ask for the points to be split
ASKER
Where is the button to alert the moderator that I want to split the points
On your original question, there's a button "Request Attention"
P.S. - I don't need the points. I just wanted to clarify a few things for future visitors who read this thread hoping to solve their own problem.
P.S. - I don't need the points. I just wanted to clarify a few things for future visitors who read this thread hoping to solve their own problem.
ASKER
My apologies - that was early this morning - I appreciated your input.
If not, put this just after your opening php tag
Open in new window