Jump to content

Recommended Posts

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.

 

 

 

    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'> </td>" .

        nl2br("<td valign='top' class='location'>{$row['location']}</td>") .

"<td class='spacer'> </td>" .

nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .

"<td class='spacer'> </td>" .

nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a></td>") .

if ($with2) {echo  ", <a href='{$row['with2Link']}' target='_blank'>{$row['with2']}</a>";}

echo "</td>";

}

Link to comment
https://forums.phpfreaks.com/topic/168788-newbie-help-with-if-statement/
Share on other sites

I cleaned your code up some, I hope this helps

<?php
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'> </td>" .
nl2br("<td valign='top' class='location'>{$row['location']}</td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a></td>");
if ($with2) {
echo  ", <a href='{$row['with2Link']}' target='_blank'>{$row['with2']}</a>";
}
echo "</td>";

the error was that you had a "." at the end of the nl2br right before the if statement.  you can't do it that way.  If it doesn't work as expected, let us know.

Hi dedukes,

 

Don't feel too dismayed, only through practice and getting it wrong do we all learn how to program. PHP lends itself brilliant for readability, so feel free to add in spacing so that its clearer to read, you will save yourself a LOT of time. The <<<HTML and {${ }} is part of the heredoc syntax and makes it easier to seperate your code. The xxx ? xxx : xxx is the ternary operator and since I first learned it not long ago, has been the most useful tool in my web box.

 

Learn more about heredoc here: http://uk.php.net/manual/en/language.types.string.php

Learn more about the ternary operator here: http://uk.php.net/manual/en/language.operators.comparison.php

 

(I keep php.net as a bookmark in my browser and is probably my most visited site!)

 

<?php
echo <<<HTML
<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'>{${ $showTime ? "$hour:$minute $ampm" : NULL }}</td>
    <td class='spacer'> </td>
    <td valign='top' class='location'>{${ nl2br($row['location']) }}</td>
    <td class='spacer'> </td>
    <td valign='top' class='venue'><a href='{${ nl2br($row['venueLink']) }}' target='_blank'>{${ nl2br($row['venue']) }}</a></td>
    <td class='spacer'> </td>
    <td valign='top' class='with'><a href='{${ nl2br($row['with1Link']) }}' target='_blank'>{${ nl2br($row['with1']) }}</a></td>
    {${ $with2 ? ', <a href="' . $row['with2Link'] . '" target="_blank">' . $row['with2'] . '</a>' : NULL }}
    </td>
HTML;
?>

 

I've seperated your code into easier to read HTML and PHP. Using heredoc (start the statement with <<< then an identifier, in this case HTML, and end with no spaces, no indent, just HTML; on its own, on a new line) you can put variables in to be parsed, just as you would with double-quoted strings. Anything complex you can put between {${ ...... }} to be processed, as of PHP 5, this allows functions and method calls as well.

 

You could put the if statement in this, I'm sure I have done but Netbeans (the editor I use) was saying there were errors with it, so just through speed I used the ternary syntax...

 

(if whatever is before the question mark is true) ? (do whatever comes in this section) : (otherwise do this);

 

... is pretty much how it works.

 

Hopefully this has been of help to you...

 

iSE

Hey, gang.  Thanks for all your help!  I'm making a little progress, but I'm still not getting the results I'm hoping for.

 

Jonsjava, removing that period made it so that I'm getting my original output back.  So that's great.

 

But, alas, I'm still not getting the improvement I'm hoping to get.  Which is, when 'with2' has something in it, it gets listed after the value of 'with1' and a comma and space are inserted between them.  Again, the relevant code, i think, is this:

 

if ($with2) {
   echo  ", <a href='{$row['with2Link']}' target='_blank'>{$row['with2']}</a>";
}
echo "</td>";
} 

 

 

Could there be an issue in how I've defined the field in the mysql database?  It's TEXT and it defaults to NULL.

 

iSE: I tried your code and I got no output from some reason.

 

For the big picture, below is the entire bit of code:

 

 

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


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'> </td>" .
nl2br("<td valign='top' class='location'>{$row['location']}</td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a></td>");
if ($with2) {
   echo  ", <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>";
}


?>

Good question!  I don't know how to answer that.  Do I define $with2 at the top of the code?  like so?

 

$with2 = $row['with2'];

 

Okay: significant progress.  I entered the code above below the following line:

 

$showTime = $row['showTime'];

 

I got the output I wanted (comma and hyperlinked value from 'with2'), but it's in the wrong place--at the top and the bottom of the center columns.

 

WolfRage, I DIDN'T change the if statement as you suggested.  just defined 'with2'.

 

Where and how do I place the definition of 'with2' to ensure that it outputs to the same column and same cell as the output from 'with 1'?

 

 

Okay, I got it. 

 

I defined 'with2,' which gave me the output i wanted.

 

and the formatting issue was because the if statement output was writted outside of the </td> tag

 

thanks everyone for you help and patience!  the resolved code is below.

 

--dd

 

 

<?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'> </td>" .
nl2br("<td valign='top' class='location'>{$row['location']}</td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='venue'><a href='{$row['venueLink']}' target='_blank'>{$row['venue']}</a></td>") .
"<td class='spacer'> </td>" .
nl2br("<td valign='top' class='with'><a href='{$row['with1Link']}' target='_blank'>{$row['with1']}</a>");
if ($with2) {
   echo  ", <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>";
}


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.