Jump to content

Merging variables


khristian

Recommended Posts

Hi,

 

This might be simple, but I fear I am being a bit dim today.

 

I currently have a form that has three fields (id1, id2, and id3)

 

What I want to do is when it comes to processing form and storing it in a database I want a fourth field created (user) but this being a combincation of the previous three fields.

 

I have tried:

 

$user = $id1,$id2,$id3

or

$user = $id1+$id2+$id3

 

and many other variations, but I cannot seem to get it to work.

 

Any advice anyone?

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/
Share on other sites

LOL Well sadly I now have another issue (Yes I really am having a dumb day)

 

What I want, is if some selects 'Block One' from a list, then it has this stored as 'blockname', but also a shorter version as 'shortblockname'

 

I have tried this (but it dosent work):

 

if ($blockname == 'Block 1') {

$shortblockname = 'B1'};

else {

end if;

}

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433688
Share on other sites

The list itself comes from a form, but here it is:

 

<select size="1" name="blockname">

<option selected>-- SELECT BLOCK NAME --</option>

<option>Western Harbour Midway</option>

<option>Western Harbour Terrace</option>

<option>Western Harbour Breakwater</option>

</select>

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433692
Share on other sites

How about doing something like this

 

<select size="1" name="blockname">

      <option selected>-- SELECT BLOCK NAME --</option>

      <option value="WHM">Western Harbour Midway</option>

      <option value="WHT">Western Harbour Terrace</option>

      <option value="WHB">Western Harbour Breakwater</option>

      </select>

 

Now the value returned from $_POST['blockname'] is the Value instead of the name.  Or do you want both?

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433694
Share on other sites

Ah I need both, as one will go into an address field (blockname), and one will go to form part of the username (shortblockcode).

 

I just managed it with just:

 

if ($blockname == 'Western Harbour Midway') {

$shortblockname = 'WHM';}

 

if ($blockname == 'Western Harbour Terrace') {

$shortblockname = 'WHT';}

 

if ($blockname == 'Western Harbour Breakwater') {

$shortblockname = 'WHB';}

 

But I am unsure if there is a better way.

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433700
Share on other sites

Depends on how many you have, and if they are static or dynamic.  I personally would use a database and load them from there.

 

But you can make yours more compact.

 

$shortblockname = ($blockname == 'Western Harbour Midway') ? "WHM" : "";

$shortblockname = ($blockname == 'Western Harbour Terrace) ? "WHT" : "";

$shortblockname = ($blockname == 'Western Harbour Breakwater') ? "WHB" : "";

 

 

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433706
Share on other sites

Another way to do this would be to use an array:

<?php
$blocks = array('Western Harbour Midway' => 'WHM', 'Western Harbour Terrace' => 'WHT', 'Western Harbour Breakwater' => 'WHB');
$shortblockname = (array_key_exists($blockname, $blocks))?$blocks[$blockname]:'';
?>

 

It looks like the shortblockname is the first character of each word in the blockname. If this is correct, this is another method:

<?php
$tmp = explode(' ',$blockname);
$shortblockname = '';
foreach ($tmp as $word)
    $shortblockname .= strtoupper($word[0]);
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/85038-merging-variables/#findComment-433723
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.