I have the following coding:
<div class="product-top-icons">
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<div class="energy-rating-2"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_two');?>.jpg"></div>
<div class="energy-rating-3"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_three');?>.jpg"></div>
<div class="guarantee-icon"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('warranty');?>.jpg"></div>
</div>
I would like to add an if statement in there basically to say the following:
If the value in the 'energy_rating_one' attribute is null then don't display the division energy-rating-1, if the 'energy_rating_two' attribute is null then don't display the div energy-rating-2 and so on...
something like this:
<?php if($_product->getAttributeText('energy_rating_one') !== null): ?>
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<?php endif; ?>
and that for all the others as well.
<?php
function echoIfExists($argument) {
$val = $_product->getAttributeText($argument);
if($val)
/*your echo stmt*/
}
echoIfExists('energy_rating_one');
/** continue here*/
?>
Make it easy on yourself. Just change the css class rules from energy-rating-1 to energy-rating-one and echo the variable you already have i.e energy-rating-one
You have a function "int_to_words" to transform "int" value into a text value in this post:
http://www.php.net/manual/en/function.strval.php#41988
After that, you just have to iterate into all values
for(i = 0; i < 100; i++) {
$item = 'energy_rating_'.int_to_words($i);
if($_product->getAttributeText($item) != null){
echo "<div class=\"energy_rating_$i\"><img src=\"http://www.justhome.co/skin/frontend/default/just2011/images/assets/".$_product->getAttributeText($item).".jpg\"></div>";
}
}
Look at short hand if statement:
http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/
$total==1 ? "is 1 item" :
"are ".($total == 0 ? "no items" : "$total items"
Related
my function is not worked.
$displayon = "display:none !important";
$displayoff = "padding:0";
$displayres = "responsive";
$displayres1 = "responsive1";
function displays($check)
{
if ($display1 OR $display2 OR $display3 OR $display4 == $displayon){
echo $displayres1;
return true;
}
else {
echo $displayres;
}
}
and
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display1;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display2;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display3;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display4;?>">
But not worked.I want if "$display1 or $display2 or ..." was equal to "$displayon" , Use responsive1. Otherwise, use responsive.
I have a photo gallery of 4 columns. In the module, you can disable each column. I want the responsive to become 3 columns if a column is disabled. And if two columns are disabled, responsive becomes 2 columns. Each display (display1,2,3,4) represents a column.
If I understand correctly, the function needs to do this:
function displays($check) {
if ($check == "display:none !important") {
return 'responsive1';
} else {
return 'responsive';
}
}
And be used like this:
<div class="<?php echo displays($display1); ?>"style="<?php echo $display1;?>">
<div class="<?php echo displays($display2); ?>"style="<?php echo $display2;?>">
<div class="<?php echo displays($display3); ?>"style="<?php echo $display3;?>">
<div class="<?php echo displays($display4); ?>"style="<?php echo $display4;?>">
You pass the style string as the argument to the displays function, and return one class or the other depending on whether or not it matches the specific string you're looking for.
Some important things to note:
you can't use a variable in a function if it's defined outside the function. (See variable scope)
You can't compare multiple things to one thing like this
if ($display1 OR $display2 OR $display3 OR $display4 == $displayon){
Instead you'll need to do them one at a time like
if ($display1 == $displayon || $display2 == $displayon ... etc.
Or put them in an array and use in_array. But I don't think that's what you were going for here anyway.
When you define a function like function displays($check), the $check is a parameter, and when the function is executed it will take the value of whatever you put there when you call the function. For example if we say displays('hi'); then $check will have the string 'hi'. And if we say $someVar = 42; and call displays($someVar); then $check will have the value 42.
Can i use if else statement for css?
This is where i want the color of the text to change:
<?php echo $status; ?>
There will be 2 status: Pending & Delivered
Pending will be red color and delivered will be green
Can i do something like (for CSS):
.pending {text-decoration:underline; color:red;}
.delivered {text-decoration:underline; color:green;}
and if else statement:
if ($status==delivered)
{
//this is where i don't know what to do and code
}
else
{
//and here
}
What should i put there? Or any other solution?
If the $status variable in PHP actually matches your class names, just use it in your PHP when displaying whatever the thing is that's being styled:
e.g. if $status == 'pending', then:
<div class="<?= $status ?>">...</div>
will render
<div class="pending">...</div>
and match your .pending rule.
Output html with php / javascript / any other language, and assign classes to the whatever element you want.
pure PHP example:
<?php
if(true) {
echo '<div class="pending">content</div>';
} else {
echo '<div class="delivered">content</div>';
}
?>
Another way using variables (PHP + html):
<?php
if(true) {
$status = 'pending';
} else {
$status = 'delivered';
}
?>
<html>
<head>
</head>
<body>
<div class="<?php echo $status; ?>">content</div>
</body>
</html>
First of all this is a wordpress page so [insert_php] is a replacement for <?php.
The url of the page is as follows "http://**.com/?page_id=98#highlights?sel=q1s"
the code on the page is as follows
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { return "black"; } else { return "white"; };">
I'm sure I'm overlooking something here.
EDIT :
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }[/insert_php];">
this is my current code and it still showing no results, I checked the actual value I'm getting for background-color and for some reason am getting the result "white".
I'm not sure but I think the parenthesis is missing around the condition, and also you have to use an echo; like this :
if ($_GET['sel'] == "q1s") { echo "black"; }
<div id="Question1" style="background-color:<? if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; };">
you want echo instead of return
You forgot the closing php tag. Normally ?> but since you are using wordpress use [/insert_php]
Also, you should echo instead of return. You are not in a function.
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }; [/insert_php]">
Trying to figure out how to better write this chunk of code. I'm wanting to get the list of the roster members and then create an array of options for the view dropdown to display inside the select dropdown and also have it have an option to display "Please Select An Option". However what if what is returned from the getAllRoster function is NULL which is what I have returned if no results are returned from a query. How should I handle that which I just want the empty option displayed.
Also I need to think about is do a function to retrieve all the allies for that specific matter and then display that ally as the default ally in the dropdown for each dropdown.
Controller:
$rosterList = $this->bios->getAllRoster();
$allies = array();
$allies[''] = 'Please Select An Opion';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php echo form_dropdown( 'ally1', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 2', 'ally2'); ?>
<div>
<?php echo form_dropdown( 'ally2', $allies, ''); ?>
</div>
<?php echo form_label( 'Ally 3', 'ally3'); ?>
<div>
<?php echo form_dropdown( 'ally3', $allies, ''); ?>
</div>
EDIT :
What I am wanting to do is if the allies array is empty it needs to display the message No wrestlers in database but its instead giving me an error in my view file.
Controller:
pastebin.com/1Bf721zJ
View:
<?php echo form_label( 'Ally 1', 'ally1'); ?>
<div>
<?php if ($allies[''] == 'No Wrestlers In Database') {
echo $allies[''];
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
</div>
I also am curious about something. I have the alliesList variable that either has a value of a resultset or null and what I want to do if its a result set is have each of the allies be the default value in each of the dropdowns.
You could do something like this:
$rosterList = $this->bios->getAllRoster();
$allies = array();
if (empty($rosterList) {
$allies[] = 'nothing to display';
}
else
{
$allies[] = 'Please Select An Option';
foreach ($rosterList AS $ally)
{
$allies[$ally->id] = $ally->rosterName;
}
}
also in your view, if you don't want to display a drop down you could put a conditional in to display something else, e.g.:
<?php if ($allies[0] == 'nothing to display') {
echo $allies[0]
}
else {
echo form_dropdown( 'ally1', $allies, '');
} ?>
Im not sure i fully understand your question but if im right cant you just do
if $_GET['allies'] == "Please select an option"{
$something = Null
}
else{
$something = $_GET['allies']
}
and use $something where you would have used $_GET['allies']?
How do I get this to pull my 2nd variable? (I already have a switch setup)
<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">
I have a switch statement that pulls the pages from
index.php?page=#####
and I have just added this part to my switch:
index.php?page=####§ion=#####
Right now, if I am on page=photos, my code ends up being:
<body id="photos">
I need to make it so that if any link has the "sections" variable on it like this page=photos§ion=cars it uses the same ID:
<body id="photos">
First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:
<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">
For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php
I am not entirely sure I understand your question, but where you're doing:
$_GET['page']; echo $page;
What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:
echo $_GET['page'];
GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).
Wouldn't that be an if to find out it if the section was defined? i.e.
if(isset($_GET['section'])){
//create div
} elseif(isset($_GET['page']){
//create fallback div
}
Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID = $_GET['section'];
} else if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
?>
...
<body id="<?php echo $bodyID; ?>">
Alternatively,
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
$bodyID='home';
foreach (array('section', 'home') as $key) {
if (isset($_GET[$key]) && isValidID($_GET[$key])) {
$bodyID = $_GET[$key];
break;
}
}
?>
...
<body id="<?php echo $bodyID; ?>">
In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.
If you decide you want both page & section in the ID, you can try something like:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID .= '_' . $_GET['section'];
}
?>
...
<body id="<?php echo $bodyID; ?>">