Newbie: Help with "if" statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dedukes
Forum Newbie
Posts: 5
Joined: Tue Aug 04, 2009 6:56 am

Newbie: Help with "if" statement

Post by dedukes »

Hi, I'm new to PHP and not adept at programming in general. the following code worked beautifully until I added the "if" statement in the last line. I want output that 1) is hyperlinked text and 2) outputs a comma between the two possible values if the second value is not null. Does that make sense?

Thanks for your help.

Code: Select all

 
    echo "<tr><td valign='top' align='right' class='date'>$monthName $day, $year</td>" .
         "<td valign='top' align='right' class='dayw'>$dayw</td>" .
         "<td valign='top' align='right' class='time'>";
        if ($showTime) {echo  "$hour:$minute $ampm";}
    echo "</td>" . 
         "<td class='spacer'>&nbsp;</td>" .
         nl2br("<td valign='top' class='location'>{$row['location']}</td>") .
         "<td class='spacer'>&nbsp;</td>" .
         nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .
         "<td class='spacer'>&nbsp;</td>" .
         nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a></td>") .
         if ($with2) {echo  ",&nbsp;<a href='{$row['with2Link']}' target='_blank'>{$row['with2']}</a>";}
    echo "</td>";
}
 
Last edited by Weirdan on Tue Aug 04, 2009 5:08 pm, edited 1 time in total.
Reason: added [code=php] tags
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Newbie: Help with "if" statement

Post by papa »

Please use the php tags so the is easier to read.

You might want to use if(!empty($row['with2']))
dedukes
Forum Newbie
Posts: 5
Joined: Tue Aug 04, 2009 6:56 am

Re: Newbie: Help with "if" statement

Post by dedukes »

thanks.

but, hm. wouldn't that output the value only if $with2 is empty? I want the opposite to occur.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Newbie: Help with "if" statement

Post by papa »

Try and find out. :) Notice the ! before the function.
dedukes
Forum Newbie
Posts: 5
Joined: Tue Aug 04, 2009 6:56 am

Re: Newbie: Help with "if" statement

Post by dedukes »

well, i'm still not getting any output. hm.

might there be something wrong with the rest of the code?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newbie: Help with "if" statement

Post by jackpf »

You're getting no output at all?

If so, you might want to turn on error reporting......
dedukes
Forum Newbie
Posts: 5
Joined: Tue Aug 04, 2009 6:56 am

Re: Newbie: Help with "if" statement

Post by dedukes »

I got it. the problem was that i hadn't defined 'with2'. the resoled code is below.


Code: Select all

<?php
function displayEvents($row)
{
 $day = substr($row['date'], 8, 2);
$day = $day - 0;
$month = substr($row['date'], 5, 2);
$year = substr($row['date'], 0, 4);
$dayw =  date("D", mktime(0, 0, 0, $month, $day, $year));
$monthName = date("M", mktime(0, 0, 0, $month, $day, $year));
$hour = realHour($row['time']);
$minute = substr($row['time'], 3, 2);
$ampm = realAMPM($row['time']);
$showTime = $row['showTime'];
$with2 = $row['with2'];
 
echo "<tr><td valign='top' align='right' class='date'>$monthName $day, $year</td>" .
"<td valign='top' align='right' class='dayw'>$dayw</td>" .
"<td valign='top' align='right' class='time'>";
if ($showTime) {
   echo  "$hour:$minute $ampm";
}
echo "</td>" .
"<td class='spacer'>&nbsp;</td>" .
nl2br("<td valign='top' class='location'>{$row['location']}</td>") .
"<td class='spacer'>&nbsp;</td>" .
nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .
"<td class='spacer'>&nbsp;</td>" .
nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a>");
if ($with2) {
   echo  ",&nbsp;<a href='{$row['with2Link']}' target='_blank'>{$row['with2']}</a>";
}
echo "</td>";
} 
 
 
 
function displayActions($row)
{
         echo "<td valign='top'><a href='admin.php?id={$row['id']}&action=edit'>edit</a></td>" .
         "<td valign='top'><a href='admin.php?id={$row['id']}&action=delete'>delete</a></td></tr>" .
         "<tr><td colspan='9' height='0px'></td></tr>";
}
 
 
?>
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Newbie: Help with "if" statement

Post by peterjwest »

If you're getting no output you might want to include the following, which will display any errors:

Code: Select all

 
ini_set('display_errors', true); 
error_reporting(E_ALL);
 
I would recommend you create a function (or two) to make this code more readable and maintainable
dedukes
Forum Newbie
Posts: 5
Joined: Tue Aug 04, 2009 6:56 am

Re: Newbie: Help with "if" statement

Post by dedukes »

thanks for the tip.

by "create a function or two" do you mean add your display_errors code to mine? and where do i add this code?

thanks for your help.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Newbie: Help with "if" statement

Post by peterjwest »

Those statements should be added at the beginning of your code. They are standard functions used to display PHP errors.

I was suggesting that you add functions which reduce repetition in your code e.g.

Code: Select all

function getHyperlink($caption, $hyperlink,$target = NULL) {
  if ($target) { $target = ' target=".$target.'"'; }
  return '<a href="'.$caption.'" '.$target.'>'.$caption.'</a>'; }
 
 
This way whenever you need a hyperlink you can just put:

Code: Select all

echo getHyperlink('Google','www.google.com');
You'll need to decide for yourself what kind of functions are useful and appropriate.
Post Reply