variable inside variable in a PHP function? - php

I have the following function.
The problem is that the $lang variable will vary depending on the $this->setts['site_lang']; . The actual problem is the following:
$condition_details['description_$lang']
(which isn't working). How do I get this to display
$condition_details['description_us']
or
$condition_details['description_fr']
depending on the $lang setting?
And here is the full function:
function shipped_drop_down($box_name = 'item_shipped_via', $selected = null, $form_refresh = null)
{
(string) $display_output = null;
$lang = $this->setts['site_lang'];
$sql_select_conditions = $this->query("SELECT id, description_".$lang." FROM " . DB_PREFIX . "advanced_shipping_carriers ORDER BY theorder,description_".$lang." ASC");
$display_output = '<select name="' . $box_name . '" ' . (($form_refresh) ? 'onChange = "submit_form(' . $form_refresh . ', \'\')"' : '') . ' style="font-size:10px; width: 120px;"> ';
while ($condition_details = $this->fetch_array($sql_select_conditions))
{
$display_output .= '<option value="' . $condition_details['id'] . '" ' . (($condition_details['id'] == $selected) ? 'selected' : '') . '>' . $condition_details['description_$lang'] . '</option> ';
}
$display_output .= '</select> ';
return $display_output;
}

You could do
$condition_details['description_'.$lang]
or use "
$condition_details["description_$lang"]

You could store your lang settings in arrays like this :
$lang['fr']['condition_details']
so you could use $lang[$selected_lang]['condition_details']

Use "double quotes" instead of 'single quotes'. That way the $lang will be parsed and replaced with the relevant value.

Use double quotes like so:
$condition_details["description_$lang"]

Related

Changing radiolist to dropdown

I have stuck ;/ I have activeradiolist and works ok, but I need to create another list bud with dropdowns item's
my code with $model for activeradioList
echo Html::activeradioList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
Now i try to convert this to dropdownList like ->
echo CHtml::dropDownList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
and have htmlspecialchars() expects parameter 1 to be string, object given
1) The CHtml is class from old Yii 1.x framework. Yii2 doesn't use the C prefix for its class names.
2) You are pairing the form field with your model instance so you should use the activeDropDownList() method instead of dropDownList().
3) In your radio button options you have item callback that is used to generate the html code for radio button. The drop down list doesn't have anything like that so you should remove it from its options. You can completly omit the fourth parameter because the item callback is the only option there.
So the code for drop down list should look like this:
echo Html::activeDropDownList($add, 'type_contact', $items);

issue with generating links from database in PHP

First of all, I will try and keep this as simple as possible.
This is part of a larger script that generates a breadcrumb link list depending on which category the user is currently in on the page.
Something like:
Hats > Mens > Green
Each of the categories also have their corresponding links so the user can click back on it.
The problem I'm having is that for some reason, I'm getting an additional link inserted that is the current category, right before the top category.
For example, the output, should be:
<b>Category:</b>
Hats >
Mens >
Green
But I am getting this:
<b>Category:</b>
Hats >
<a href="categories.php?parent_id=175828&name=Green&keywords_search=" title="">
Mens >
Green
</a>
Note that the difference is that the current category link (but without the name) gets inserted almost at the very top again, in addition to an additional link closing tag at the very end as well.
What could be the problem? I've spent nearly 6 hours on this and can't figure it out, so I'd really appreciate it if someone could tell me what is wrong. Maybe I spent too much looking at this and am "stuck inside the box" :)
function category_navigator ($parent_id, $show_links = true, $show_category = true, $page_link = null, $additional_vars = null, $none_msg = null, $reverse_categories = false)
{
global $reverse_category_lang, $category_lang, $db;
(string) $display_output = NULL;
(int) $counter = 0;
$none_msg = ($none_msg) ? $none_msg : GMSG_ALL_CATEGORIES;
$page_link = ($page_link) ? $page_link : $_SERVER['PHP_SELF'];
if($parent_id > 0)
{
$root_id = $parent_id;
while ($root_id > 0)
{
$row_category = $db->get_sql_row("SELECT category_id, name, parent_id, hover_title FROM
" . DB_PREFIX . (($reverse_categories) ? 'reverse_categories' : 'categories') . " WHERE
category_id=" . $root_id . " LIMIT 0,1");
if($counter == 0)
{
$display_output = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$display_output = (!empty($display_output)) ? $display_output : $row_category['name'];
$ace = $row_category['category_id'];
$acename = $row_category['name'];
}
else if($parent_id != $root_id)
{
$hover = $db->get_sql_field("SELECT hover_title FROM " . DB_PREFIX . "categories WHERE category_id='".$ace."'","hover_title");
$category_name = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$category_name = (!empty($category_name)) ? $category_name : $row_category['name'];
$display_output = (($show_links) ? '<a href="' . $page_link . '?parent_id=' . $row_category['category_id']
. '&name=' . $category_name . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$row_category['hover_title'].'">' : '') . $category_name . (($show_links) ? '</a>' : '</a>')
. ' > <a href="'. $page_link . '?parent_id=' . $ace . '&name=' . $acename . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$hover.'">' . $display_output . '</a>';
}
}
$counter++;
$root_id = $row_category['parent_id'];
}
$display_output = (($show_links && $show_category) ? '<b>Category:</b>' : '') . $display_output;
}
$display_output = (empty($display_output)) ? $none_msg : $display_output;
return $display_output;
}

Update drop down from SQL database using PHP

I know this should be simple but I just can't seem to get my head around it.
I have a list of continents in a sql database that I get back using PHP DBO and display in a drop down list. What I then want to do is get the users preferred continent from the sql database and select that one in the list. E.g if the list contains World, Africa, Europe, N America and S America but the users favorite is 'Europe' I want that one selected. $getContinent is the users preference.
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
if ($getContinent != ''){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
I would be most grateful if someone could set me straight as I have found some examples on the internet but have been unable to get them to work :)
Your code should be like this
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
//just check if the option id is equal to the chosen value
if ($getContinent != '' && $getContinent==$row['CONTINENT_ID'] ){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
Its simple, as you guessed :D
You would use something like this:
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] . '">' . $row['CONTINENT_NAME'] . '</option>';
}
I hope that helps!
--al
You can use ternary operator
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] .
($getContinent == $row['CONTINENT_NAME']) ? '" selected="selected"' : '"' . '>' .
$row['CONTINENT_NAME'] . '</option>';
}

Keeping selected dropdown value

I have this drop-down and user can select multiple options ,how can i keep selected value on form after submit button, if error comes on form
<select onclick="document.getElementById('cand_qual4').style.display='none'; " name="oca[]" id="oca" multiple="multiple">
<?php
$odrop = array('B COM','M COM','BBA','MBA','LLB','LLM','CPA','CIMA','MS FINANCE','DISA','CISA','OTHER');
foreach ($odrop as $odrop1)
{
echo '<option value="' . $odrop1 . '"' . (isset($_POST['oca']) && in_array($odrop1,$_POST['oca']) ? ' selected' : '') . '>' . $odrop1 . '</option>';
}
?>
</select>
instead of
$_POST['oca'] == $odrop1
condition as $_POST['oca'] would be an array, try
in_array($odrop1,$_POST['oca'])
TRY THIS-
echo '<option value="' . $odrop1 . '"' . (is_array($_POST['oca']) && in_array($odrop1,$_POST['oca'] ) ? ' selected' : '') . '>' . $odrop1 . '</option>';

Load data from database & retain data between postbacks

In PHP Project, I'm creating page a which retrieve the data from database on page load then allow the user to change data & Update. I have to use input text and option controls to display data. Editing form data may involve in few POST requests. To load data from database and retain data between POSTs I use this approach.
Load Data from Database
$oClass = new DataClass();
$oClass ->GetRecordById($_REQUEST['id']);
$dataName = $oClass ->Name;
$dataYear = $oClass ->Year;
INPUT TEXTs
<input type="text" name="txtName" value="<?php echo isset($_POST['txtName']) ? $_POST['txtName'] : $dataName ?>" >
OPTIONs
$arrYears = GetYears();
foreach ($arrYears as $year) {
if (isset($_POST['ddlYear'])) {
if ($_POST['ddlYear'] == $year) {
echo '<option value="' . $year . '" selected="selected">' . $year . '</option>';
} else {
echo '<option value="' . $year . '">' . $year . '</option>';
}
} else {
if ($dataYear == $year) {
echo '<option value="' . $year . '" selected="selected">' . $year . '</option>';
} else {
echo '<option value="' . $year . '">' . $year . '</option>';
}
}
}
Is there a better approach to retain data between posts than using like
if (isset($_POST['NAME']))
{$_POST['NAME']}
else
{$name}
Is there a reason why you need to retain the info? If a user submits the form it will update the info and therefore will need to be fetched again for the next person to come in and update or read.
Above though you assign $year = $oClass ->Year; but it's not referenced anywhere. the $year in the foreach loop is a local scope variable only for that foreach loop, after that loop finishes executing it disappears.
you can do stuff like this.
if (isset($_POST['txtName']))
{
$name = $_POST['txtName'];
}
else
{
$name = '';
}
<input type="text" name="txtName" value="<?php echo $name; ?>" />
Since the only difference between the options in the foreach loops are whether or not the selected attribute is applied you can shorten both of them to the following.
if (isset($_POST['ddlYear'])) {
echo '<option value="' . $year . ($_POST['ddlYear'] == $year) ? '" selected="selected"' : '' . '>' . $year . '</option>';
}
else {
echo '<option value="' . $year . ($movieyear == $year) ? '" selected="selected"' : '' . '>' . $year . '</option>';
}
Also to get a quicker answer please provide the simplest working use case, for example above example has $_POST variables that aren't defined anywhere and others like $movieyear also not defined.

Categories