Multilanguage and select option - php

I'm working with a multi-language site, and there is an HTML select element with 100 choices. The code looks like this:
<option value="1"><? echo $lang['CARGOTYPE_1']; ?></option>
So system insert into mysql table "type" number "1", or number "2" pr...
But when I select number from mysql, I need to change number to word.
With if, else I can change its meanings:
If($type == 1) { echo $lang['CARGOTYPE_1']; } elseif($type == 2){ echo $lang...
But the problem is, that code will be very long...
Any smart solutions for my problem?

If you know number of choices, use for cycle for that.
for ($i = 1; $i <= 30; $i++) {
echo '<option value="' . $i . '">' . $lang['CARGOTYPE_' . $i] . '</option>';
}

I am not sure what you want... If you want to "echo" something with arguments, you can do something like :
echo $lang['CARGOTYPE_'.$type];
But, is that you need ?

Related

Get a sequence with difference using for loop in PHP

How to get a sequence like (50 difference)
100,150,200,250,.....,900.
using 2 values (100 & 900).
My try gets an increment with 50,
for($i=100;$i<900;$i++)
{
$ii=$i+50;
echo '<option value="'.$ii.'">'.$ii.'</option>';
}
Go simple like this:
for($i=100;$i<=900;$i+=50)
{
echo '<option value="'.$i.'">'.$i.'</option>';
}
Also if you wanted to include 900, use <= not just <
Your Eval
Another option: use range() (http://php.net/manual/en/function.range.php)
$sequence = range(100, 900, 50);
foreach ($sequence as $step) {
echo '<option value="' . $step . '">' . $step . '</option>';
}
This is less memory efficient than a for loop, but I believe it is more readable. In PHP 5.5 you can make it both efficient and readable by defining a generator version of range (a-la the Python xrange). See the first example here: http://php.net/manual/en/language.generators.overview.php

Change classname if i is a certain number

Im looking for a way to change the number in a class if i=4 or higher.
$i=1;
foreach($properties['blocks'] as $block)
{
echo '<div class="colum1" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
Is there any way to do this ? colum1 needs to become colum2. i've been trying to use things like if else etc. but so far no success.
$i = 1;
foreach($properties['blocks'] as $block)
{
echo '<div class="' . ($i < 4 ? 'colum1' : 'colum2') . '" id="pic'.$i.'" style="background-image:url(/uploaded/overige/'.$block['image2'].');">
// --------------

How do I make a foreach loop display items on more than one column?

I'm trying to modify a joomla module and I have a small problem (which is not joomla related, thus no code necessary).
I have a foreach loop which has a block of code in it which displays an article. It repeats itself as many times as you set it up in the admin panel. I want to add the feature that makes this module display items on more than 1 column. All I need is the perk, I think I have everything else covered.
Basically how do I modify a simple foreach loop so that it displays articles on more than one column?
Instead of this
a
b
c
d
e
I want this
a ........ d
b ........ e
c
You can get the count of results and work from the middle if you're sticking with tables
$half_count = floor(count($entries) / 2);
for($i=0;$i<$half_count;$i++)
{
echo '<tr>';
echo '<td>' . $entries[$i] . '</td>';
echo '<td>' . (isset($entries[$half_count + $i]) ? $entries[$half_count + $i]: '') . '</td>';
echo '</tr>';
}
Here a simple way to do it :
php > $arr = array(1,2, 3, 4, 5, 6, 7, 8, 9, 10);
php > for ($i=0; $i<count($arr); $i+=2) { print $arr[$i] . "\t" . $arr[$i+1] . "\n"; }
1 2
3 4
5 6
7 8
9 10
As far as I know, you won't be able to do it with a foreach statement but with a for.
For example:
$iterations = (count($array_of_items) % 2) ? (count($array_of_items) / 2) + 1 : count($array_of_items) / 2;
for ($i = 0; $i <= $iterations; $i++) {
if (isset($array_of_items[$i+3]))
echo $array_of_items[$i].'........'.$array_of_items[$i+3];
else
echo $array_of_items[$i];
}
Really simple code without so little info but could make the trick!
Instead of outputting in the for loop, I would create two arrays of the articles in the for loop. Then loop through those arrays to create your columns outside of the main loop.
You have little info in the question, but here's something you could use
$arr = array("a","b","c","d","e","f");
for ($i = 0; $i<count($arr); $i++){
echo $arr[$i]." ". $arr[$i+3] ."\n";
if($i == 2){ break;} //Modify 2 as more alphabets are added
}
Outputs
a d
b e
c f
For a generic solution, assuming a packed array....
function show_table($data, $columns)
{
$items=count($data);
$iters=$items/$columns + ($items % $columns) ? 1 : 0;
for ($y=0; $y<$iters; $y++) {
for ($x=0; $x<$columns; $x++) {
$offset=$y*$columns + $x;
if ($offset<$items) print $data[$offset] . ' ';
}
print "\n";
}
}

Inserting array values into SQL

I randomized a set of questions using the following code:
for($i=0; $i < count($nwi); $i++)
$itemorder[$i] = $i;
shuffle($itemorder);
$_SESSION["itemorder"] = $itemorder;
A few pages later, a portion of the questions are presented:
for ($i=0; $i<40; $i++) {
if ($i % 10 ==0) echo $header;
echo '<tr class="';
if(in_array($itemlabel[$_SESSION["itemorder"][$i]], $errors)) echo 'skip';
elseif ($i % 2 == 0) echo 'even';
else echo 'odd';
echo '">';
echo '<td>' . ($i + 1) . ".</td><td>" . $itemtext[$_SESSION["itemorder"][$i]] . '</td>';
for ($j = 1; $j <= 6; $j++) {
echo "\n" . '<td';
if ($j == 6) echo ' style="background-color:#999;"';
echo '><input type="radio" name="';
echo $itemlabel[$_SESSION["itemorder"][$i]];
echo '" value="';
echo $j; //value of the input
echo '"';
if ($_POST[$itemlabel[$_SESSION["itemorder"][$i]]] == $j) echo " checked";
echo '></td>';
}
At the end of the survey, I am trying to put the answers to the questions (which should range in value from 1-8) into my SQL database:
"INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES
('$_SESSION[id]', '$_SESSION[itemorder][0]',
'$_SESSION[itemorder][1]', '$_SESSION[itemorder][2]',
'$_SESSION[itemorder][3]',...
I am getting only zeros in my SQL database regardless of how I answer the questions. Any suggestions?
Well, first I don't see anything assigning anything to the session values, but the issue with your code is in this pattern: '$_SESSION[itemorder][1]'. First, I would make sure that MySQL is expecting a varchar there and not an int. If it is an int, good form would be to make sure it isn't quoted.
More importantly, though, when you have an associative array in PHP, you need to make sure PHP expects that.
This
$a = array("hi"=>array("world"=>0)); echo "$a[hi][world]";
Outputs
Array[world]
Put braces around the lookup to make sure it knows to treat it as an array, and then put quotes around all string indexes:
// note the braces and quotes
$a = array("hi"=>array("world"=>"here")); echo "{$a["hi"]["world"]}";
Outputs
"here"
But, I wonder if you wouldn't be better off just using implode:
$columns = implode(',', $_SESSION['itemorder']);
$query = "INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES ('{$_SESSION["id"]}', $columns )";
I do feel obliged to point out that that system does not seem scaleable, and column names like agree_pppg_02 are not descriptive. You may want to go to the codereview stackexchange site to see if they can't offer tips on database design.

why isn't this php loop working?

I have this code:
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
This works fine to generate a select input with options from 01:00 through to 24:45 at 15 minute intervals.
However, if I change the code and add an if statement I get some odd results...
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
if ($perfTime == '19:30') {
$sel = "selected";
}
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
The idea is to (arbitrarily!) make the select input default to 19.30. The code above adds
selected = "selected" to every option after 19:30, not just the 19:30 option. If I change the if statement slightly to be if ($perfTime = '19:30') { ... i.e., having a single = instead of == it creates a set of options all with the value of 19:30. What am I doing wrong?
Short answer: Because every single echo operation uses the current value of $sel. I assume it's initially blank, so the first N echos contain selected=''. If test succeeds, $sel is set to "selected", and every later print includes selected='selected'. If you use $perfTime = '19:30', it's an assignment, so the test always succeeds, and $sel is always 'selected'.
Quick fix: Add an else clause that sets $sel = ''. However, there are other oddities that make me think this is only a code snippit (i.e. always using $thisTime for $perfTimestamp , rather than something loop indexed, so it always prints the same time?).
This is because you never reset $sel.
Try this instead:
$sel = $perfTime == '19:30' ? 'selected' : '';
$sel isn't explicitly intitialised anywhere, so it's maintaining its 'selected' value for each run through the loop.
Try $sel = ""; as the first line in your loop as a quick fix.
Hm, might be that you should do this:
...
if ($perfTime == '19:30') {
$sel = 'selected="selected"';
}else{
$sel = "";
}
...
I think just having the 'selected' attribute present makes it selected.
Oops, I forgot: And
echo '<option value="'. $perfTimeNumber .'" '.$sel.'>' .$perfTime .'</option>';

Categories