Jump to content
Larry Ullman's Book Forums

When To Use ' Or " Or Even {}


Recommended Posts

Hi all,

Please take a look at the following:

 

<td><input name="display_y" type="radio" value="Yes" checked='"if($snippets_list['display']='y'){echo."checked".;}"'/></td>

 

I have queried a database, assigned the values to an array and now I'm trying to preset a pair of radio buttons with the result of part of the query. The query works, I've tested that so I reckon I've messed up my quotes somewhere along the line. I've read the section in the start of Larry's book about single quotes being literal and double quotes being interpreted. I would have thought that the 'if' section needs to be interpreted.

 

The whole lot is in the middle of an echo that contains the test of the HTML table. Also the array value is a string, not a boolean.

 

Thanks

Paul

Link to comment
Share on other sites

There are a number of ways you can code this. Personally, if I'm coding alot of html mixed with php processing and variables I don't use echo. When you need to some php processing, just insert it within <?php and ?> tags. Be sure to not use the shortcode tags as they have been deprecated.

<table class="myclass"><tr><td><input name="display_y" type="radio" value="Yes" checked=" <?php if($snippets_list['display']='y'){echo 'checked';} ?>"/></td></tr></table>

 

An alternative is to use echo with the heredoc syntax.

  • Upvote 2
Link to comment
Share on other sites

I like to use single quotes in my echo statements when printing out only HTML code. If I have some php variables mixed in with the html i will use the double quotes so variable values are printed rather than the literal variables, then also escape double quotes in the HTML quote so it is not messed up.

 

I am like you margaux also i do tend to add the php tags in and out of code, but also frequently use the above method, it really depends on the situation. If you are using an MVC and you have your views script the php tabs method works great.

 

HEREDOC is cool but for me it is a little ugly, may come to use it though.

  • Upvote 2
Link to comment
Share on other sites

Hi all,

Thanks for the responses, I got the syntax error to go away by inserting php tags, it does seem a cleaner way to do it. Although for simpler stuff like the price, name and description it seems easier to just concatenate.

 

However the if statement doesn't work. Have a look at the whole foreach loop:

 

foreach ($snippets_8 as $snippets_name => $snippets_list)

{

echo'

<tr>

<td><img src="images/snippets/'. $snippets_list['image'] .'.jpg" width="80px" height="80px" ></td>

<td>'. $snippets_list['price'] . '</td>

<td>'. $snippets_list['name'] . '</td>

<td>'. $snippets_list['description'] . '</td>'

?>

<td><input name="display_y" type="radio" value="Yes" checked="<?php if($snippets_list['display']='y'){echo 'checked';}?>"/></td>

 

<td><input name="display_n" type="radio" value="No" checked="<?php if($snippets_list['display']='n'){echo 'checked';}?>" /></td>

<?php

echo'

 

<td><input name="remove" type="radio" value="' . $snippets_name . '" /></td>

</tr>';

}

 

I've checked the $snippets_8 and $snippets_list arrays and they appear to be OK (the snippets_list array only showed the last item) and the list outputs OK. However the only radio buttons that are checked are both display yes and display no on the last item in the list, the rest are blank. In fact, what should happen is that the first six are checked yes and the last one checked no. I also removed the quotes from 'y' and 'n' in the if statement in case it was treating it as a boolean rather than a string. Lots of errors with that one.

 

Cheers everyone.

Paul

 

PS, Edward, what's a MVC?

Link to comment
Share on other sites

Hi all,

Ignore me I think I've sorted it. Radio buttons can only have one 'on' at a time, that's why the foreach loop only has the last one updated. The reason it had both checked was because I gave the radio buttons a different name instead of the same name (which would have toggled).

 

I need to have a total rethink now about how I do what I'm trying to do, which is to give the user the ability to amend the displayability (if that's a word!).

 

Thanks

Paul

 

PS. I'm still interested in what a MVC is though.

Link to comment
Share on other sites

HEREDOC is cool but for me it is a little ugly, may come to use it though.
I also agree that its ugly but once I used it for some lengthy php/ html mix and I was converted. Is it bad form or just not pretty?
for simpler stuff like the price, name and description it seems easier to just concatenate.
I do too. For different situations its good to have a couple of options you can fall back on to make things simpler.

 

MVC - stands for Model, View, Controller. Its a programming approach to keep your code organised and easier to maintain especially good when working on large sites involving many people. Many frameworks follow this approach. The model is the data, the view is the presentation and the controller is the logic that binds the model and the view. As an analogy think of good website development - you separate the html from the css and the javascript so you are not mixing presentation with styling with behaviours.

  • Upvote 2
Link to comment
Share on other sites

Hi all,

Ignore me I think I've sorted it. Radio buttons can only have one 'on' at a time, that's why the foreach loop only has the last one updated. The reason it had both checked was because I gave the radio buttons a different name instead of the same name (which would have toggled).

 

I need to have a total rethink now about how I do what I'm trying to do, which is to give the user the ability to amend the displayability (if that's a word!).

 

Thanks

Paul

 

PS. I'm still interested in what a MVC is though.

 

What I like about MVC is the benefit of just been able to completely work on code logic first of all. Then later its just a case of the views files which present the standard CSS and HTML to be passed the data via the controller. It means you can work on the look of the site last of all and not getting it all mixed up in the logic which can be difficult to change after its done.

Link to comment
Share on other sites

 Share

×
×
  • Create New...