I am looking to add a where clause to the following:
<?php foreach($this->assignments as $assignmentIndex => $assignmentArray): ?>
I want basically to only pull data from the database where assignmentType=pdf
any help would be great!
thanks
<?php foreach($this->assignments as $assignmentIndex => $assignmentArray): ?>
<?php if(assignmentType == 'pdf') :?>
....
....
<?php endif; ?>
<?php end foreach; ?>
well you can't change the sql line in the foreachloop
however if the assignmentArray containts assignmenType
you could ignore the other types like this:
if($assignmentArray['assignmentType'] != 'pdf'){
continue;
}
or somewhat nicer would be:
if($assignmentArray['assignmentType'] == 'pdf'){
// do your fancy stuff here
}
Related
I am very new to PHP and I am trying to get a piece of PHP code to run inside of my HTML file.
I have a drop down menu. If users select one item, it should display additional fields. So, I want them to only display if they select that item from the drop down menu. I am trying to select it based on the value for that drop down item. I have not declared any PHP values in a PHP script. This is all in HTML.
I know that with jquery you have to pull in the jquery library before running the script. Do I need to do this with PHP also?
Here is the code that I am trying to run:
Thank you in advance!
<?php
if ($dropmenuValue == "specificParameter") {
?>
<div>
-conditional content-
</div>
<?php
}
?>
<?php
if ($value ) {
?>
<div>
-conditional content-
</div>
<?php
}
?>
If you are setting the $values variable like this
$value = true
Then you can do
<? if($value){ ?>
<div>Content Here</div>
<? } ?>
Is the page a .php page?
A multi conditional statement is done in a different manner. The manner being:
<?php if($val): ?>
<div>
-conditional content-
</div>
<?php endif; ?>
the problem is that your comparing a boolean to a String.That's why your code won't work I would suggest something like If($value){ or if($value==true){.
you want to do if($value == true). The quotes you have around true does not make it a boolean variable.
Everything else in your code looks find.
tldr: correct: if($value == true) ----------
incorrect: if($value == "true")
<?php if ($value ):?>
<div>
-conditional content-
</div>
<?php endif;?>
Here are a few pointers.
$value is a boolean value. It is best to evaluate using if($value)
The PHP if syntax used above is a much cleaner way.
You will be able to see where your if statement begins and ends.
Enjoy!
Is there a way of adding a where class to a foreach equation in PHP.
At the moment I am adding an if to the foreach like this.
<?php foreach($themes as $theme){
if($theme['section'] == 'headcontent'){
//Something
}
}?>
<?php foreach($themes as $theme){
if($theme['section'] == 'main content'){
//Something
}
}?>
Presumably the PHP has to loop through all results for each of these. Is there are more efficient way of doing this. Something like
foreach($themes as $theme where $theme['section'] == 'headcontent')
Can this be done
A "foreach-where" would be exactly the same as a "foreach-if", because anyway PHP has to loop through all items to check for the condition.
You can write it on one line to reflect the "where" spirit:
foreach ($themes as $theme) if ($theme['section'] == 'headcontent') {
// Something
}
This becomes really the same as the construct suggested at the end of the question; you can read/understand it the same way.
It does not, however, address the fact that in the question's specific scenario, using any kind of "foreach-where" construction would in effect loop through all items several times. The answer to that lies in regrouping all the tests and corresponding treatments into a single loop.
Use a SWITCH Statement.
<?php
foreach($themes as $theme)
{
switch($theme['section'])
{
case 'headcontent':
//do something
break;
case 'main content':
//do something
break;
}
}
?>
You better use for loop for that like
<?php
$cnt = count($themes);
for($i = 0;$i < $cnt,$themes[$i]['section'] == 'headcontent' ;$i++){
}
?>
If someone uses MVC framework ,the answer to "foreach where" is here
<?php foreach ($plans as $plan) if ($plan['type'] == 'upgrade'): ?>
// Your code here
<?php endif; ?>
Remeber there is no need for the endforeach; statement after endif;
If someone wants to write more code between endif; and endforeach; then the above should be:
<?php foreach ($plans as $plan): if ($plan['type'] == 'upgrade'): ?>
// Your code here
<?php endif; ?>
// More of your code
<?php endforeach; ?>
How can I combine the two if statements below, but I would like the page to display the breadcrumbs if one OR the other fields are present.
<?php if (!empty($secondaryslider)): ?>
<?php if (!empty($node->field_upload_banner[0]['view'])) {?>
Use || or OR
if (!empty($secondaryslider) || !empty($node->field_upload_banner[0]['view']))
{
}
Just use an ||:
<?php if ( !empty($secondaryslider) || !empty($node->field_upload_banner[0]['view']) ): ?>
very straight forward
<?php if (!empty($secondaryslider) && !empty($node->field_upload_banner[0]['view'])): ?>
I need a code that pulls out the listings count for a product in order to have them in the navigation. I am trying to get the product and listing number in the navigation (if there is any), but I don't want to display something if the listings count is equals to 0.
Here follows my code:
<?php if($listingsCount = getListingsCount(0,0,2,2,1,86) > 0): ?>
Bakery
<?php echo $listingsCount['totalRecords'] ?>.
<?php else: ?>
It should be
if ( ($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0)
In this case you the assignment is done first and is then compared to 0. So $listingCount contains the real count.
The way you did it, the comparison is done first and the return value (true/false) is assigned to $listingCount.
Nevertheless. If you never reach the else-part, maybe something in your method getListingsCount() is broken.
Try this:
<?php if (($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0) { ?>
Bakery
<?php echo $listingsCount['totalRecords']; ?>
<?php } else { ?>
Other text
<?php }?>
Thanks guys for your answers. managed to get it working with this...
<?php
$listingsCount = getListingsCount(0,0,2,2,1,86);
if($listingsCount['totalRecords'] > 0):
?>
<dd>
Bakery
[<?php echo $listingsCount['totalRecords'] ?>]
</dd>
<?php endif; ?>
I am trying to use substr to remove 4 characters from a URL string. The goal is to remove .jpg from the string and replace it with "-220x124.jpg".
I am using a wordpress plugin, advanced custom fields, but that is not the issue here. The issue is that the subst is not working with the Advanced custom fields code, the_sub_field. It returns the entire URL string without the last 4 characters removed. Any idea why?
Code below:
<?php if(get_field('still_uploads')): ?>
<?php $i = 0; ?>
<?php while(the_repeater_field('still_uploads') && $i <= 0 ): ?>
<?php
$imagejesse = the_sub_field('still_image');
$imagejessenew = substr($imagejesse,0,-4);
?>
<?php echo $imagejessenew.'-220x124.jpg'; ?>
<?php $i++ ?>
<?php endwhile; ?>
<?php endif; ?>
You can see an example here: http://gicreative-dev.com/blog/genre/gay/
Use strtr() function like that:
$imagejessenew = strtr($imagejesse, array(
'.jpg' => '-220x124.jpg',
));
See this for a proof: http://ideone.com/B8ZQe
Try this:
<?php
$imagejessenew = substr(trim(the_sub_field('still_image')),0,-4);
if($imagejessenew !== FALSE)
{
$imagejessenew .= '-220x124.jpg';
}
else
{
// Shit happened, the_sub_field('still_image') was shorter than 4 characters
}
?>
$imagejessenew = preg_replace('/(.*)(.(jpg|png|gif))$/i', '$1-220x124.$3', trim($imagejesse));