Embedding PHP if statements into HTML - php

feel like i have tried every possible way..right now it is just skipping over the if statement and posting out the html.
<? if ($departmentID == 1 : ?>
<h4>Outreach Questions <?= $departmentID ?><h4>
<h4>Why Interested</h4>
<ul>
<strong>Why are you interested in volunteering as an outreach docent? </strong> <?php echo $whyInterested ?>
</ul>
<h4>Wildlife Issues</h4>
<ul>
<strong>What’s an environmental or wildlife issue you feel passionately about, and why? </strong> <?php echo $wildlifeIssue ?>
</ul>
<h4>Public Speaking</h4>
<ul>
<strong>Do you have prior experience speaking to the public? Please describe. </strong> <?php echo $publicSpeaking ?>
</ul>
<h4>Wildlife Groups</h4>
<ul>
<strong>Do you belong to any animal rights groups (PETA, The Humane Society, etc.)? If so, which ones? </strong> <?php echo $wildlifeGroup ?>
</ul>
<h4>Contributions</h4>
<ul>
<strong>What do you think you’d bring to the outreach volunteer team?</strong> <?php echo $bringToTeam ?>
</ul>
<? endif; ?>
</div>
the idea is to do an else if with id ==2 and so on..but it is pushing out all the HTML regardless of the departmentID.
thanks in advance.

<?php if ($departmentID == 1) {
echo " <h4>Outreach Questions = '$departmentID' </h4> #all html below
</ul>";} # ends block and closes echo
?>
</div>
Try this it uses php to echo out all of the html if the case is true otherwise no html gets echoed

Isn't the pretty first line wrong? You didn't close it:
Change the first line:
<?php if ($departmentID == 1) : ?>

Related

How to prevent empty spaces rendered on line ends?

The context
I have the following code:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
It renders:
<ul>
</ul>
When there are $users, everything works well. However, when there aren't, the <ul> is rendered with blank spaces in it, making it not attributable with the CSS selector :empty.
I managed to solve the problem by doing this:
<ul><?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?></ul>
It renders:
<ul></ul>
Without the like breaks in the code, the list is now empty (even empty of blank spaces), but I'm afraid to leave it that way and some other programmer fix the indentation and break it.
My question
Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
My question is: Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
You could use HTML comments
<ul><!--
<?php foreach($users as $user){ ?>
--><li><!-- …
This is a technique also used to fight the space between inline block elements, if you want to keep the HTML code readable.
Or you could of course use output buffering, and then replace those spaces before you return the output to the client (rather complex, if you want to do it right.)
And probably the most simple solution of all to your actual problem:
Why output the ul element at all, if there are not going to be any list items?
Surround the whole block with an if that checks how many elements the array contains – and just don’t output any of that stuff if there aren’t any.
Do a test before :
<?php if (isset($users) && count($users) > 0){ ?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php } ?>
With that way, <ul> won't be created if there's no users, so no empty spaces.
Don't tab your php statements:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
PHP will remove everything starting at the <?php and ending at the ?> including the newline character. This is leaving the tab you have before your opening php tag.
You can break into a new line using:
echo "\n" //this will break into a new line
But pay atention that you must use double quotes. Single quotes won't work:
echo '\n' //this will output \n
The full solution will be
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } echo "\n" ?></ul>
I tested and it works. But if you are worried about future development and wan't to make things clear, I would use
if(isset($users)){?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php }else{ //comment explaining that this prevents CSS selector ?>
<ul></ul>
<?php } ?>
I would go with this notation :
<ul><?php
foreach($users as $user){
?><li><?php echo $user->name ?></li><?php
}
?></ul>
It's still pretty readable, and HTML output contains no spaces that aren't needed, whether $users is empty or not!

MySQL/PHP - text formatting

I've created a simple query to create a list of dates and descriptions from my calendar database.
The idea is that I have a PHP webpage that show a text list that I can quickly copy and paste into an email or text message.
My problem is that, although the text shows up correctly on the web page, when I paste the info into a text editor (Word, email, whatever) I'm getting tabs between each column.
How can I format the text in PHP so that it pastes correctly?
This is my code:-
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li>
<?php echo htmlentities($row_AvDates['Month']);?>
<?php echo "-";?>
<?php echo htmlentities ($row_AvDates['the_days']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
This gives me output that looks correct, but pastes like this...
(Month[tab]"-"[tab] Dates)
How do I loose the tabs?
You're closing php and letting the HTML output be rendered. See tab chars below:
[tab]<li><?php echo htmlentities($row_AvDates['Month']);?>
[tab]<?php echo "-";?>
[tab]<?php echo htmlentities ($row_AvDates['the_days']);?></li>
Solution:
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities ($row_AvDates['the_days']); ?>
Darren.
Try to use this code, it should solve your problem:
<?php if (mysql_num_rows($AvDates) > 0) : ?>
<ul>
<?php while ($row_AvDates = mysql_fetch_assoc($AvDates)) : ?>
<li><?php echo htmlentities($row_AvDates['Month'])."-".htmlentities ($row_AvDates['the_days']);?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hope it helps.
you can have the whole output in one single like this
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities($row_AvDates['the_days']);?>
instead of separate php tags
If you want to separate code across multiple lines, not all code in one line:
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php
echo htmlentities($row_AvDates['Month']);
echo "-";
echo htmlentities($row_AvDates['the_days']);
?></li>
<?php
}
?>
</ul>
<?php
}
?>

Show div only if mySQL info exists

I have a MySQL database storing some basic user info, so: name, email, and two phone numbers. I want to make a page with dynamic php text showing the users info, but in the database only one phone number is required. On the page I have:
<p id="first"> Some non-dynamic text </p>
<p> <?php echo $row_rsCustomer['first_name']; ?> </p>
<p> <?php echo $row_rsCustomer['email']; ?> </p>
<p> <?php echo $row_rsCustomer['phone_one']; ?> </p>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<p id="second"> Some non-dynamic text </p>
I dont want the #second to be pushed down if the #phonetwo is empty. I was thinking of using some javascript like this:
if( #phonetwo.innerhtml == ""){
#phonetwo.style.display="none";
But I was wondering if there's a way to do this using php? The javascript solution will work I suppose, but I'm pretty sure I've seen somewhere a more "correct" way to do this, I just don't remember what it was nor can I find it anywhere.
Just wrap the paragraph in a conditional
<?php if(isset($row_rsCustomer['phone_two']) && $row_rsCustomer['phone_two']):?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php endif?>
<?php if (!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>
Just follow this pattern anytime you need to hide HTML from PHP:
<? if (your condition) { ?>
Html
<? } ?>
It is much better than doing it in JS since it does't send any data to the browser.
First check is it empty or not
<?php if(!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php } ?>
check if $row_rsCustomer['phone_two'] is not empty and then display your <p> :
<?php if (isset($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>

php/html automatically creating new line between rss posts?

My goal is to create a dynamic list of links next to each other (not on top of each other). So far I have the links outputting correctly using Simplepie and an Atom datasource but for some reason my output has a linebreak between each new item in the for each statement. Is it possible to skip the line break and put the output items next to each other instead? I am not getting any errors, just not getting my goal output. Please help this is making me age at an exponential rate :)
<body>
<?php
foreach ($feed->get_items() as $item):
?>
<div class="item"><?php echo $item- >get_title(); ?></div><?php endforeach; ?>
</body>
Put the links in a <ul> (Unordered list element) with style of float:left like so
<body>
<ul>
<?php
foreach ($feed->get_items() as $item):?>
<li class="item"><?php echo $item- >get_title(); ?>
</li>
<?php endforeach; ?>
</ul>
</body>

How to remove anchor from active navigation page using PHP?

I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.
I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.
Here is my code:
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1><?=$menuitem->title?></h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
Any help would be greatly appreciated. Thanks!
UPDATED CODE: (this is what works for me now)
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1>
<?php if ($menuitem->uri == $requesteduri):?>
<?=$menuitem->title;?>
<?php else: ?>
<?=$menuitem->title?>
<?php endif;?>
</h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)
<?php echo base_name($_SERVER['REQUEST_URI']); ?>
find out what your loop is outputting (Put this in the loop):
<?php echo $menuitem->uri; ?>
Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:
<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
<?=$menuitem->title?>
<?php else: ?>
<?=$menuitem->title;?>
<?php endif;?>
</h1>
Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.

Categories