How to work with if not in PHP? - php

I want to print the div with the class called inner-content-div, if the variable $name is not consisted with the following strings.
Gingelly Rolls
Kithul Treacle
Coconut Vinegar
But my PHP code is not working.
Here is my code.
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
?>
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
if(!in_array($name, $exclude_list)){ ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php
}
?>

It should be -
if(!in_array($name->name, $exclude_list)){
as you are printing it -
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>

<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>
<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>
<?php if(!in_array($name->name, $exclude_list)): ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php endif; ?>
It could be because $name is an object. Also, probably better to use PHP's alternate syntax for cleaner html/templates.
The other potential issue here is that the casing of the items in the exclude list could potentially not match the value of name. You should probably normalize those to all lower case when doing the comparison.

you can try like this. First print $vid[2], using echo $vid[2];. Then put $vid[2] values into the array called $exclude_list. Then use the following code.
$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
//Your HTML code
}

Related

Can access detailed information through URL with function parameters (Codeigniter)

I am creaiting my first Codeigniter application for a blog with news. In the main page there is only the title of the news which is also a link to a view with the detailed information and body of the news. Im having trouble on accessing the get through the URL that has to go over a function that receives the ID of the new as a parameter. I just can get that to work. Can someone help me?
The problem is not in the function itself because it works fine when i assig an static value to the URL, but for some reason i can send the $row->id object as a Get through the URL with the proper value for each of the news.
MODEL
class Post extends CI_Model{
public function getPost(){
$this->load->database('fintech_blog');
$data = $this->db->get('post');
return $data->$result();
}
CONTROLLER
public function getPost($id){
$query = $this->db->query("select * from post where id = '$id' ");
$rows = $query->result(); //method for putting into an array format
$data=array('result'=>$rows);
$this->load->view('view',$data);
}
VIEW
foreach ($result as $row):
$id = $row->id;
$post = site_url('welcome/getPost/$row->id');
?>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-preview">
<a href="<?php echo $post; ?>">
<h2 class="post-title">
<?php echo $row->title; ?>
</h2>
<h3 class="post-subtitle">
<?php echo $row->calling; ?>
</h3>
</a>
<p class="post-meta">Posted on
<!-- Start Bootstrap -->
<?php echo time_elapsed_string($row->created); ?></p>
</div>
<hr>
</div>
</div>
</div>
<?php endforeach; ?>
The problem comes from the way you create the string for $post.
As one commenter suggests the problem is the use of single quotes and to use double quotes instead. The most important feature of double-quoted strings is the fact that variable names will be expanded. Meaning the variable symbol ($test in this case) is replaced with the value of the variable.
To illustrate:
$test = "blue";
//first, a string created with single quotes
echo 'The sky is $test today.'; //outputs: The sky is $test today.
But using double quotes to create the string...
echo "The sky is $test today."; //outputs: The sky is blue today.
The var $test get expanded to the value it holds.
So, the first two lines of the foreach loop in the view should be.
$id = $row->id;
$post = site_url("welcome/getPost/$row->id");
However, you never use $id and you only use $post once. That says to me that these two lines are not needed. Delete them both.
Change the line
<a href="<?php echo $post; ?>">
to
<a href="<?php echo site_url("welcome/getPost/$row->id"); ?>">

Drupal entity with field collections not saving value to a variable

I have field collections within an entity and I'm trying to theme the values but having a hard time saving some of those values as variables. In the code below, I am trying to print the value for 'field_area_headline' but getting an 'undefined index' notice in my browser. I've included an image of my browser where I have dpm-'d my variables that will likely be a clue for those who know what to look for.
What am I doing wrong? Thanks in advance!
<?php foreach($variables['field_focusareas'] as $delta => $item) : ?>
<?php $focus_area_node = $item['entity']; ?>
<?php foreach($focus_area_node->field_area as $focus_delta => $area) : ?>
<!-- <?php dpm($area); ?> -->
<?php $focus_area = entity_load('field_collection_item', array($area[0]['value']));
dpm($focus_area);?>
<?php $headline = $focus_area['field_area_headline'][LANGUAGE_NONE][0]['safe_value']; ?>
<h3><?php print $headline; ?> </h3>
<span></span>
<?php endforeach; ?>
<?php endforeach; ?>
$focus_area is an object, not an array. You need to retrieve an object property - $headline = $focus_area->field_area_headline[LANGUAGE_NONE][0]['safe_value'];

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

Drupal template if statement

I'm trying to write an if statement in page.tpl.php to say:
If nodeid == 51, 52, 53 print:
<div class="band main-content">
<?php print render($page['content']); ?>
</div>
Else print:
<div class="band main-content">
<section class="layout">
<?php print render($page['content']); ?>
</section>
</div>
I haven't tested this, but it should work:
<?php
if ($node) {
// Get the current node nid
$nid = $node->nid;
}
//now compare the current node id present in $nid with some node ids you desire that you can put in $desired_node
$desired_node = array(51,52,53); //enter your desired node ids
if (in_array($nid, $desired_node)){
print render($page['content']);
}
if (!in_array($nid, $desired_node)){ ;?>
<section class="layout">
<?php print render($page['content']); ?>
</section>
<?php } ;?>
The condition you're looking for is in_array($nid, array(51, 52, 53)) (or you could use a bunch of logical ORs).
Putting it all together yields:
<?php if(in_array($nid, array(51, 52, 53))): ?>
<div class="band main-content">
<?php print render($page['content']); ?>
</div>
<?php else: ?>
<div class="band main-content">
<section class="layout">
<?php print render($page['content']); ?>
</section>
</div>
<?php endif; ?>
Also, I'm not sure what the end goal is here, but I would be wary about hard coding node IDs in a template. It's definitely not best practice.
I am not sure how do you get an array in $nid, but instead, you should call a function, written in a custom module. That function should check the current nid by taking it from the URL and return either TRUE of FALSE. In any cases, (other than known nodes), you should add logic in a function, defined in a custom module. This helps debugging & enhancements.

Encoding html entities in url string

I'm trying to prepare a url that has special characters in it, to be used as GET variables in a php file. I think I need html entities and urlencode, which I then need to decode in the other php file. But I'm running into some problems with correctly encoding them.
This is what I have:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleent = htmlentities($title); ?>
<?php $titleentencoded = urlencode($titleent); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title html entities: <?php echo $titleent; ?></p>
<p>Title encoded: <?php echo $titleencoded; ?></p>
<p>Go!</p>
The $titleencoded variable turns out to be empty. I'm overlooking something obvious but I can't see it. What am I doing wrong?
EDIT: New code after suggestions
Okay, so here's what I came up with:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleentencoded = urlencode($title); ?>
<?php $htmlent = htmlentities($titleentencoded); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title encoded: <?php echo $titleentencoded; ?></p>
<p>Title html entities: <?php echo $htmlent; ?></p>
<p>Go!</p>
Is this the right way?
Your variable is empty because you have a typo. You initialize $titleentencoded but later use $titleencoded:
<?php $titleentencoded = urlencode($titleent); ?>
// Should be
<?php $titleencoded = urlencode($titleent); ?>
See #Quentin's answer for a logic error.
You are doing it backwards.
You are putting data in a URL, then a URL in an HTML document.
You need to urlencode the data, put it in the URL then htmlencode the URL and put it in the document.

Categories