I've just recently started using PHP 5.4 and have noticed as of 5.3 you can use goto to jump to sections of code which I am using to jump from a loop section. My question is after having read this post... Is GOTO in PHP evil? is this bad practice in this case or is it a viable solution?
<?php
while ($thisResult = mysql_fetch_array($result)) {
if($article && $i > 0) {
goto comments;
}
?>
<h2>
<?=$thisResult['post_title']?>
<span><?=$thisResult['post_modified_gmt']?></span>
</h2>
<p class="content">
<?=nl2br($thisResult['post_content']);?>
</p>
<br />
<?php
comments:
if ($article) {
?>
<p class="comment"><?=$thisResult['comment_content']?>
<?php
}
$i++;
}
?>
This is called spaghetti programming and is a bad practice.
http://en.wikipedia.org/wiki/Spaghetti_code
here is what you can do instead for your code
<?php
while ($thisResult = mysql_fetch_array($result)) {
if($article && $i > 0) {
}
else {
?>
<h2>
<?php $thisResult['post_title']?>
<span><?php $thisResult['post_modified_gmt']?></span>
</h2>
<p class="content">
<?php nl2br($thisResult['post_content']);?>
</p>
<br />
<?php
}
if ($article) {
?>
<p class="comment"><?php $thisResult['comment_content']?>
<?php
}
$i++;
}
?>
A simple else solves it. you can probably find more elegant solutions using switch cases or flags.
The problem is that your code will be hard to edit and add stuff to it.
This is rewrite of the same code without using goto, to show you that you can always make it without it
<?php
while ($thisResult = mysql_fetch_array($result)):
if(!$article || $i <= 0): ?>
<h2>
<?php echo $thisResult['post_title']; ?>
<span><?php echo $thisResult['post_modified_gmt']; ?></span>
</h2>
<p class="content">
<?php echo nl2br($thisResult['post_content']); ?>
</p>
<br />
<?php endif; ?>
<?php if ($article): ?>
<p class="comment"><?php echo $thisResult['comment_content']; ?></p>
<?php endif;
$i++;
endwhile;
I used alternative syntax for control structures, and replaced <?= with <?php echo as it's much more readable to me. Also, other commenters gave you some good suggestions regarding separation of markup and db functions etc, so please give it a thought
Related
Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.
I'm very new in PhP and trying to modify the code of may website.
I want to display a line only under certain condition but when I use if in the code, the site is showing blank
Thanks for your support
<?php
if ( $property->post_type != 'land')
{
<div class="property-drow">
<span>
<?php
if($site_language=='en_US') {
echo 'Age of Construction';
} else {
echo 'Âge Construction';
}
?>
</span><p> {echo $construction;} ?></p>
</div>
}?>
Probably the best answer would be to tell you to add some error reporting to your code as you test
Add to the top of your file(s) while testing right after your opening PHP tag for example
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// normal code
In this case you are not starting and stopping the PHP interpreter in the right places
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if ( $property->post_type != 'land')
{
?>
<div class="property-drow">
<span>
<?php
if($site_language=='en_US') {
echo 'Age of Construction';
} else {
echo 'Âge Construction';
}
?>
</span>
<p> <?php echo $construction;?></p>
</div>
<?php
}
?>
You have some html escaping issues.
To dry up your code you could consider using the ternary operator and short echo tag:
<?php if ($property->post_type != 'land'): ?>
<div class="property-drow">
<span><?=
$site_language=='en_US'
? 'Age of Construction'
: 'Âge Construction'
?></span><p><?= $construction ?></p>
</div>
<?php endif; ?>
I have a problem with this scenario. There is a certain field (textarea) - $ COMMENTS. If the information in the $COMMENTS starts with a number (the first is a number and then any letter) - the script works fine (hide / show "some HTML and PHP code"), but if the $COMMENTS starts with the letter - the code does not work at all. Please tell me what is my mistake. I suffer already the 5th day.
Here is the script code, unfortunately it did not work correctly:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=0): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
also didn't work like this:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=""): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
Thank you all in advance for your help.
use isset() with if()
<?php $comments=$hm->Zb('rs:def:comments');
if (isset($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
EDIT
Please try below code.
<?php $comments=$hm->Zb('rs:def:comments');
echo $comments; //for debugging to ensure $comments have value.
if ($comments){ ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php } else { ?>
<div class="sorry">Sorry, there is no comments</div>
<?php } ?>
http://php.net/manual/en/function.isset.php
try it : empty()
<?php $comments=$hm->Zb('rs:def:comments');
if (!empty($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).
Basically, I need for the image at the bottom of the page with no text wrapped around it to be in the place of test post 4 (it's going to be a Google Adsense block but I am using images as a placeholder for now). I'm not sure how to do this, right now the code is the following:
<div class="google_ad_post">
<?php if ($count == 3) { ?>
<br /><img src="****" alt="post ad">
<?php } ?>
<?php $count++; ?>
</div>
Yet, the image is still at the bottom of the page. How can I fix this?
Here is the image:
I can't post pictures just yet so the URL to the image is http://i.imgur.com/7rw5B.jpg
I have to see your code to help you better with the Scripting Part, but something logical like this should work:
<?php
$count = 0;
foreach( $posts as $post ) : ?>
<?php if ($count == 3) { ?>
<div class="google_ad_post">
<img src="****" alt="post ad">
</div>
<?php } else { ?>
<div class="post" id="post-<?php the_ID(); ?>><div class="content"><?php the_content(); ?></div>
<?php } ?>
<?php $count++; ?>
<?php endforeach; ?>