Hi
I am a php newbie although I have been a programmer for years, this can be
dangerous because all the languages I know use = as equal comparison so it
took me a long time to debug if ($STRING = 'fred') { not realising I was
assigning fred to $STRING and the evaluation is always true! It took ages to
discover if ($STRING = = 'fred') {.
I eventually managed to create a validated form which re-displays with an
error list, but I have a few questions about 'best practise' programming
because my solutions seem a little clumsy:
Combobox selection: How do you set a default or remember what the user
selected so the form is re-displayed with their selection. I did this: Item1
= '< -- Select a Country --> as "selected", I then changed the text to the
actual selection using echo if the form is re-displayed, Question: How do
you dynamically set a default or dynamically move the SELECTED tag?
<option selected><?echo $COUNTRY?></option>
<option>Afghanistan</option>
<option>Albania</option>
<option>Algeria</option>
Radio Buttons: I assigned each with a <? echoDEFAULT1 ?>, <? echo DEFAULT2
?> etc and set each to "" except for the default I really want, this is set
to "checked", surely I am not doing this the right way?
<input type="radio" value="MSWORD" <?echo $CKWORD?> name="RB1"
tabindex="14">Microsoft Word<br>
<input type="radio" value="TXT" <?echo $CKTXT?> name="RB1"
tabindex="15">Text File<br>
<input type="radio" value="PDF" <?echo $CKPDF?> name="RB1"
tabindex="16">Portable Document Format (PDF)</td>
TIA
Barry 10 2731
Barry,
For a combo box (drop-down select), use something like this:
<label for='fld_location' accesskey='L'><u>L</u>ocations:</label>
<select id='fld_location' name='location[]'>
<?php
// POPULATE LOCATIONS
$locations = pg_query($db_conn, 'SELECT name FROM locations ORDER BY
name;');
if ($locations) {
while ($location = pg_fetch_object($locations)) {
echo '<option
value=\''.$location->name.'\''.(($location->name==$thisrecord->location)?'
selected=\'selected\'':'').'>'.$location->name.'</option>';
}
}
?>
</select>
The ternary operator (cond?val1:val2) is used to check each option. If
the option is the one currently selected output selected='selected',
otherwise output an empty string. If you have a static list (as opposed
to a database query), just put the ternary operator in each of the
options checking each options value against the current selection.
So the same thing in radio buttons using the ternary operator except
use checked='checked'. Hope that helped.
-Robert
Best practice would be to use a class that encapsulates all of this,
where the basic logic would be, more or less, in a simple model:
Have a value that keeps the track of the selected item, an array of all
the items, and a loop that checks if the currently outputted value is
selected or not.
in almost-correct-code:
$options = array();
$options['msword'] = 'MS Word';
$options['txt'] = 'Text File';
$selectedKey = null;
echo "<selct bla bla>";
foreach($options as $key=>$value) {
$selected = '';
if($key == $selectedKey)
$selected = 'selected';
}
echo "<option name='foo' value='$key' $selected>$value</option>"
}
echo "</select>"
You can use the same procedure for radio buttons.
Here's one way you can do that - have a function similar to this:
function selectValue($name,$value) {
if(isset($_POST[$name]) && $_POST[$name] == $value) {
return ' selected="selected"';
} else {
return '';
}
}
Then, in your option list, do this:
<select name="myOption">
<option<?=selectValue('myOption','Option A');?>>Option A</option>
<option<?=selectValue('myOption','Option B');?>>Option B</option>
</select>
Scott
Barry Morris wrote: Hi
I am a php newbie although I have been a programmer for years, this can be dangerous because all the languages I know use = as equal comparison so it took me a long time to debug if ($STRING = 'fred') { not realising I was assigning fred to $STRING and the evaluation is always true! It took ages to discover if ($STRING = = 'fred') {.
I eventually managed to create a validated form which re-displays with an error list, but I have a few questions about 'best practise' programming because my solutions seem a little clumsy:
Combobox selection: How do you set a default or remember what the user selected so the form is re-displayed with their selection. I did this: Item1 = '< -- Select a Country --> as "selected", I then changed the text to the actual selection using echo if the form is re-displayed, Question: How do you dynamically set a default or dynamically move the SELECTED tag?
<option selected><?echo $COUNTRY?></option> <option>Afghanistan</option> <option>Albania</option> <option>Algeria</option>
Radio Buttons: I assigned each with a <? echoDEFAULT1 ?>, <? echo DEFAULT2 ?> etc and set each to "" except for the default I really want, this is set to "checked", surely I am not doing this the right way?
<input type="radio" value="MSWORD" <?echo $CKWORD?> name="RB1" tabindex="14">Microsoft Word<br> <input type="radio" value="TXT" <?echo $CKTXT?> name="RB1" tabindex="15">Text File<br> <input type="radio" value="PDF" <?echo $CKPDF?> name="RB1" tabindex="16">Portable Document Format (PDF)</td>
TIA
Barry
I do something similiar. For added flexibility, I pass the field value
instead of the index into $_POST. I also have the function perform the
echo.
Here's the equivalent for checkboxes and radio buttons:
function checked($current_value, $item_value) {
if(is_array($current_value)) {
$checked = in_array($item_value, $current_value);
}
else {
$checked = ($current_value == $item_value);
}
if($checked) {
echo "checked";
}
}
<input type="checkbox" <? checked($ck, 'hello') ?> name="ck[]"
value="hello">
I've been programming since the middle of the 20th century and I still
type '=' when I mean '=='. One trick I learned is this:
if ($string = 'fred') will NOT generate a syntax error.
if ('fred' = $string) WILL generate a syntax error. You can't assign a
variable to a constant.
Put the constants first in compares and if it's not '==' it will warn
you.
That doesn't solve you larger problem, but it might help avoid one
common debugging annoyance.
--gary shannon
Thanks for the tips and example code. Are there any php books you can
recommend?
Barry
Thanks for that tip, I had a similar problem with syntax once before, I
could not for the life of me work out why the Wang interpreter was throwing
an error when I tried to display text in the middle of the screen. I had
only just come from programming ICL VME computers (British) so no matter how
hard I looked I could not see that CENTRE was spelt wrong. Good job the Wang
was not in colour!
Barry
Gary,
One thing about that is that it is counter-intuitive to read. For
example you are not asking "if 'fred' equals your name..." but rather
"if your name equals fred'...".
That said, your method is a very practical solution to a common
problem.
In a way I kinda wish that the '=' operator worked like the ECHO
construct in that you wouldn't be able to use it as a function. That
way if your source tried to actually look at the result of an '='
assignment a syntax error would occur (like the distinction between
echo and print). The only side effect would be that you would be unable
to chain assignments like this:
a = b = c = d = 0;
But an alternate method could be added to the language such as:
(a, b, c, d) = 0;
Which I think is actually used by LISP if I recall correctly.
But of course I'm just dreaming/thinking out loud here. :o)
-Robert
rlee0001 wrote: Gary,
One thing about that is that it is counter-intuitive to read. For example you are not asking "if 'fred' equals your name..." but rather "if your name equals fred'...".
That said, your method is a very practical solution to a common problem.
In a way I kinda wish that the '=' operator worked like the ECHO construct in that you wouldn't be able to use it as a function. That way if your source tried to actually look at the result of an '=' assignment a syntax error would occur (like the distinction between echo and print). The only side effect would be that you would be unable to chain assignments like this:
a = b = c = d = 0;
But an alternate method could be added to the language such as:
(a, b, c, d) = 0;
Which I think is actually used by LISP if I recall correctly.
But of course I'm just dreaming/thinking out loud here. :o)
-Robert
Why not change all the primitives to objects and then move the '='
operation to equal()? Then you could have:
$a->equal($b)
Or DEFINE the '=' to be '.equals.'? Then it would be:
$a .equals. $b
[Just joking. Programmers in other languages may recognize these.]
-david-
Are you using PHP 4 or 5?
I thought PHP and MySQL Web Development by Luke Welling and Laura
Thompson was a good book for learning PHP 4. I don't know if they have
a new edition out for PHP 5 yet. Make sure you don't get the 1st
edition of that book, though, because it was written before some of the
major changes to PHP 4 were out, such as register_globals disabled by
default.
Scott
Barry Morris wrote: Thanks for the tips and example code. Are there any php books you can recommend?
Barry
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Smithz |
last post by:
Having adopted someone else's PHP cope and completing a crash course in the
language I came across a (probably common) problem with the current code.
On a registration form, whenever users names...
|
by: Aadam |
last post by:
Does Microsoft have a best practices for tracking errors? (like in a
database, what info, etc)
|
by: Jon Rea |
last post by:
I am currently cleaning up an application which was origainlly hashed
together with speed of coding in mind and therefore contains quite a few
"hacky" shortcuts.
As part of this "revamping"...
|
by: cbrown |
last post by:
I am rebuilding an existing application that relies on an SQL DB. The
app is a scheduling/employee management program. My question pertains
to best practices in dotnet and database.
I use a 3...
|
by: bonk |
last post by:
I have come across the need to distinguish between the creation of a
deep and a shallow copy and with great interest I have read this
article:
...
|
by: Ned Balzer |
last post by:
Hi all,
I am pretty new to asp.net; I've done lots of classic asp, but am just
beginning to get my mind wrapped around .net.
What I'd like to do is include some code that tests if a user is...
|
by: =?Utf-8?B?VzFsZDBuZTc0?= |
last post by:
When one architects a new project one of the first steps in the decision is
to decide on the layers. (In my opinion anyway)
One architecture that I have used before is to go solid OO and create...
|
by: Paul |
last post by:
Hi,
I feel I'm going around circles on this one and would appreciate some other
points of view.
From a design / encapsulation point of view, what's the best practise for
returning a private...
|
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= |
last post by:
Hi,
I am after suggestions on the best practice declaring and destroying objects.
some example code:
Private Sub MySub
Dim frmMyForm As MyForm
Try
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |