Help with a php if statement - php

can somebody help me with this if statement?
My form appears every-time I load the page but it disappears after I submit the form!
Maybe it's the "endif" syntax that confuses me but I can't get this done properly...
here is the code:
<?php
if ($this->input->post('submit') && $this->input->post('categories')):
foreach($tagedImages as $image):
?>
<div class="thumb">
<?php echo'<a href="/toKoritsi/uploads/'.$image.'"/> <img src="/toKoritsi/uploads/thumbs/'.$image.'"/></a>' ?>
</div>
<?php
endforeach;
elseif(isset($photosQuery) && count($photosQuery)):
foreach($photosQuery->result_array() as $photo):
?>
<div class="thumb">
<?php echo'<a href="/toKoritsi/uploads/'.$photo['name'].'"/> <img src="/toKoritsi/uploads/thumbs/'.$photo['name'].'"/></a>' ?>
</div>
<?php
endforeach;
endif;
$options = array(
'bracelet' => 'Bracelets',
'ring' => 'Rings',
'bag' => 'Bags',
'earing' => 'Earings'
);
echo form_open("toKoritsi/gallery");
echo form_dropdown('categories', $options, 'bracelet');
echo form_submit('submit', 'Choose');
echo form_close();
?>
thanks in advance!

Should foreach($tagedImages as $image) be foreach($taggedImages as $image)?
The formatting of your code is very ugly. If you are intent on getting nicely-formatted HTML output, perhaps you should consider using Tidy, or a template engine that offers that functionality.

I donĀ“t know what the url structure of your site looks like, but are you sure you are submitting the form to the right place?
According to the CI user guide, it gets submitted to something like /index.php/toKoritsi/gallery but that depends on your config preferences.

You definitely need to format your code properly.
I'd also recommend trying to consolidate your PHP into blocks, and keep it separate from your HTML.
You can do all the conditionals and echos WITHIN some of those HTML elements:
<div>
<?php /*...*/ ?>
</div>
as opposed to what you're doing now:
<?php /*...*/ ?>
<div>
<?php /*...*/ ?>
</div>
<?php /*...*/ ?>
In addition, things like these blocks make no sense:
<?php endforeach;?>
<?php endif;?>
Even if you had to break your code into small chunks, wouldn't it be easier to say:
<?php
endforeach;
endif;
?>
EDIT: This answer was provided before the question source was reformatted and cleaned up. I was unable to even address the original issue until I could better see what was going on.
This might sound kind of dumb, but are you sure that your gallery controller is behaving correctly? If you're submitting and there's an error in one of your CodeIgniter functions, it could be messing up the rendering after the submit.

Related

Which is the better way to insert HTML tags into PHP code?

I see two methods but I don't know which is better, should I insert a div tag like this :
echo "<div>";
or like this ?
?> <div> <?php
There's no right answer to this.
When displaying a lot of HTML, I normally use ?> <div> <?php. This is especially true for when looping over data from a database, I would use:
<?php while ($row == $query->fetchObject() ) : ?>
<div class="row-<?php echo $row->id ?>">
//Some more of the data
</div>
<? endwhile; ?>
However, displaying only a little, I normally just echo it.
<?php echo "Hello, my name is <strong>Joe Doe<strong>"; ?>
I would encourage you to play with it and find what you like best.
The text editor i am using has the color formatting, when you are echoing things, it only all comes out the same color, so if you are outside of PHP you get the benefit of the color co-ordination.

CakePHP Recommended syntax for templates (views)

Ever since i've used CakePHP, I asked myself about the deeper sense of the recommended syntax of CTP-files, which is basically a HTML-file with all PHP code bracketed with tags. I find this very hard to read and I should think that the context switches between HTML and PHP would add some performance penalty.
Wouldn't it be faster and clearer to collect all output in a string and echo it at the end?
But there is some deeper sense for sure, just that i don't see it..
To make myself clearer, here's an example:
CakePHP:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
My approach:
<?php
$out = '';
if (!empty($file['User']['email'])) {
$out .= '<div class="mailto">'.$this->Html->link($file['User']).'</div>';
}
if (!empty($file['Document']['comments'])) {
$out .= '<div class="file-comment file-extra column grid_6">'
.'<div class="content">'.$file['Document']['comments'].'</div>'
.'</div>';
}
echo $out;
?>
So my question is: What are the drawbacks to my approach compared to CakePHP's ?
First things first: writing your entire template as PHP, then echoing it is not a great idea. As a general rule of thumb, I avoid echoing HTML from PHP ever, if I can. there are many reasons, but the main one will be the lack of syntax highlighting in your IDE.
Anyway, code formatting is entirely down to personal preference, but if you're writing your templates like this:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
...it's no wonder you can't read them.
There are a few things you could try, to make your code clearer and easier to read. Again, these are down to your own personal preferences, and you could get into the habit of using some or all of them.
Format your HTML properly, with indentations for child elements.
Add white space between lines of code that are too busy, particularly between lines of PHP and lines of HTML.
Use short echo tags syntax (<?= instead of <?php echo).
Assign the more complex PHP values to variables so that your HTML is easier to read.
Remember to comment your code (HTML or PHP), particularly adding HTML comments so that you can easily see separate components of your template at a glance.
Example
<?php
$user = $file['User'];
$comments = $file['Document']['comments'];
?>
<!-- User -->
<?php if (!empty($user['email'])) : ?>
<div class="mailto"><?= $this->Html->link($user); ?></div>
<?php endif; ?>
<!-- File Comments -->
<?php if (!empty($comments)) : ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?= $comments; ?></div>
</div>
<?php endif; ?>

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; ?>

What is this convention of using php code inside the first html tags?

I was referring the page.tpl.php(Drupal 7 theme) for understanding the code. I found the following code,
<?php if ($site_name || $site_slogan): ?>
<!-- !Site name and Slogan -->
<div<?php print $hgroup_attributes; ?>>
<?php if ($site_name): ?>
<h1<?php print $site_name_attributes; ?>><?php print $site_name; ?></h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<h2<?php print $site_slogan_attributes; ?>><?php print $site_slogan; ?></h2>
<?php endif; ?>
</div>
<?php endif; ?>
Can you see the code in third line, <div<?php print $hgroup_attributes; ?>> WHY the php code is inside the first div tag of html? Same thing in later part of code also, as you can see h1 and h2 code. So, what is this convention of combining the html and php in so complicated way? and how should I read that?
Combining HTML and PHP code in Drupal templates is actually a very strong feature. In this case, $hgroup_attributes will probably contain some classes that style the div. Printing it in the template results in something like
<div class="SOME_CLASSES"> ... </div>
If you're further interested in the variable $hgroup_attributes, you can inspect by pasting <?php dpm($hgroup_attributes); ?> in your template file after you've installed the Devel module.

PHP statement if cells in different tables are the same

I need help writing a php echo that checks if two cells are the same in two tables.
The cells are:
posts>id and reblogs>newid
I have the following code to start with:
<?php if(!empty($reblogDetails->newid)): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
which I borrowed from another snippet but isn't working.
can anyone help?
Your question is not completely clear, but I am assuming you want to show the table with an anchor tag only if the posts->id is equal to the reblogDetails->newid? If that is the case the code you would want would be more like the below.
<?php if(!empty($reblogDetails->newid) && $posts->id == $reblogDetails->newid): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
That being said I doubt your anchor tag is doing what you want. I would anticipate it creating an anchor that looks something like
12

Categories