Add value to variable PHP - php

I am creating a category list.. 20 different categories and when they click and send a category it will pass a numeric value representing the category to the Database..
For example:
<option value="1">Apple</option>
<option value="2">Microsoft</option>
The reason for that I am passing a numeric value in the database, is because I have more than 1 language on the webpage, and I have different label files. I would like to echo a php variable based on the category number.
<? echo $ct_1; ?>
But to avoid a lot of if/else statements, I would like to know if I could add the number to the variable. So if I am looping the category to be this: $row['category'] I would like to add that variable to the other one.. Like so:
<? $category = $.'ct_'.$row['category']; echo $category; ?>
I know that is not possible.. But I hope you get my idea and maybe have a solution for this.

This is called variable variables.
$category = ${'ct_'.$row['category']};
echo $category;
Or just:
echo ${'ct_'.$row['category']};
     

A tailored variable variableDocs example:
<?php $category = 'ct_'.$row['category']; echo $$category; ?>
If you look closely it's nearly identival to yours:
<?php $category = $.'ct_'.$row['category']; echo $category; ?>
^ ^

You COULD use eval() but that can lead to some very big security holes.
I suggest using an array instead:
$ct = array(1 => "Apple", 2 => "Microsoft");
echo $ct[$row['category']];

Quick and dirty approach would be to use eval
eval('$category = $ct_'. $row['category'] . ';');
echo $category;
Make sure input is sanitized first!

Related

How to change which variable is used, depending on another value

I'm trying to make a fun side project and have come to a stop on a small problem. I am trying to have the echo command placed inside of the while and mysqli_fetch_assoc command as the data changes everytime the page refreshes, from the select from table random function.
I select random items like this:
$survivor_set = random_survivor();
$item_set = random_item();
$firstaidkitaddon_set = random_firstaidkitaddon();
$flashlightaddon_set = random_flashlightaddon();
$keyaddon_set = random_keyaddon();
$mapaddon_set = random_mapaddon();
$toolboxaddon_set = random_toolboxaddon();
$survivoroffering_set = random_survivoroffering();
The problem line of code looks like this:
<?php while($toolboxaddon = mysqli_fetch_assoc($toolboxaddon_set)) { ?>
However I need the words after $ to be changed so was looking for something like this to work:
<?php while($ echo h($item['item']);addon = mysqli_fetch_assoc($ echo h($item['type']);addon_set)) { ?>
This is probably explained poorly, I would appreciate if anyone could lend some time to help me where I can show in more details what exactly I am trying to accomplish.
The code shows the random item that was selected from the table.
<?php while($item = mysqli_fetch_assoc($item_set)) { ?>
<?php echo h($item['name']);?> E.g. Engineers Toolbox
<?php echo h($item['rarity']);?> E.g. veryrare
<?php echo h($item['type']);?> E.g. toolbox
<?php echo h($item['media']);?> E.g. engineerstoolbox.png
<?php } ?>
I am then trying to find a way to put the echo h($item['type']) into the next output. so it would look like this when it is a toolbox.
<?php while($toolbox = mysqli_fetch_assoc($toolbox_set)) { ?>
But then could change due to it being a different item type that was pulled:
<?php while($flashlight = mysqli_fetch_assoc($flashlight_set)) { ?>
Full code, for context: https://zerobin.net/?9f772676aa87df3f#Gxy43WGqShTkL/VG42+t3nT4+sxGhxFy+GDB0B3+YH0=
You should store all your different add-on results in an associative array where the keys match the possible "type" values from your main item query. This allows you to select an item from the array using a string to reference its key - and you can take that string value from your $item['type'] variable.
e.g.
$survivor_set = random_survivor();
$item_set = random_item();
$addons = array(
"firstaid" => random_firstaidkitaddon(),
"flashlight" => = random_flashlightaddon(),
"key" = random_keyaddon(),
"map" = random_mapaddon(),
"toolbox" => random_toolboxaddon()
);
$survivoroffering_set = random_survivoroffering();
Then later on you can select the right set of results from the associative array by selecting the index which matches $item['type']:
<div class="itemaddonscontainer">
<div class="<?php echo h($item['type']);?>">
<?php while($addon = mysqli_fetch_assoc($addons[$item['type']])) { ?>
<img class="<?php echo h($addon['rarity']);?> survivor-item" src="imgs/survivor/itemaddon/<?php echo h($addon['media']);?>"/>
<?php } ?>
</div>
</div>
N.B. mysqli_free_result probably isn't necessary here.

Split Multiple Options in Single Attribute in Magento

I have multiple options in an attributes in Magento and when I call the attribute, all of the options show in a string and I would like to show them on separate lines (p tags or li's).
<?php echo $this->getChildHtml('spamodel') ?>
The above is my code, I think i need to use explode but I'm a newbie at php.
Thanks for any help.
<?php $spamodel = $this->getChildHtml('spamodel');
$spamodel_explode = explode(",",$spamodel);
echo $spamodel_explode[0];
?>
Here you can go with this code. you can get single the names with $spamodel_explode[0] and if you want to get all the names than you can use forloop
Here You can able to get the answer
<?php
$variable = $this->getChildHtml('spamodel')
$variable_exp = explode(",",$variable)
foreach($variable_exp as $var){
echo $var;
}
?>
In above coding you can explode based on your requirement.Just replace the ',' tag and get the results based on your requirement.
Find the below link for more details.
http://www.brandammo.co.uk/output-magento-custom-attributes-to-front-end-from-multi-select-dropdowns/

Using Meta Box Plugin to assign URL to a variable

I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>

Simple php syntax error, needs fixing

I have a code that can get me the category_id of an item. This is the code:
<?php echo lavada_category_id() ; ?>
I want to know how I can add this code. Inside this, I want to replace the number 2 in here;
<?php lavada_query_item("category=2");?>
with:
<?php echo lavada_category_id() ; ?>
I know you cannot do like this
<?php lavada_query_item("category=<?php echo lavada_category_id() ; ?>");?>
But how can I do it?
Why not store it into a variable and then use that variable?
<?php
$catID = lavada_category_id();
lavada_query_item("category={$catID}");
?>
OR if you just want category ID to be passed into lavada_query_item do this:
lavada_query_item($catID);
The syntax error that you have is that you can not use <?php within <?php
You just need to concatenate the string like this:
<?php lavada_query_item("category=". lavada_category_id() );?>
I think this is what you are looking for:
<?php lavada_query_item(lavada_category_id());?>
The value returned from thelavada_category_id() function will be passed into the lavada_query_item() function.

PHP echo variable within function arguments

I'm currently using a wordpress function in order to display posts from a specific category. A simplified example is shown below:
<?php query('cat_name=cat1&posts=1') ?>
Essentially this gets 1 post from the category cat1.
However I have a variable saved which gets the current category (this is on category pages):
<?php $thiscat = get_the_category(); ?>
Current Category: <?php echo $thiscat ?>
How can I now echo the variable $thiscat into the arguments of my query above so that the category name is filled in for me? This function is applied on different category pages so having it automatically passed to the arguments of my query saves a lot of hassle.
Thanks in advance for any help.
You only echo something when you want to output it to the browser, here we concatenate the query string with the variable:
<?php $thiscat = get_the_category(); ?>
<?php query('cat_name=' . $thiscat . '&posts=1') ?>
Not sure I understand the question, but it sounds like you want to use $thiscat in your query. This should do it:
<?php
$thiscat = get_the_category();
query("cat_name=$thiscat&posts=1")
?>
Note the double quotes, which are necessary. If you use single quotes, the variable will not get expanded.

Categories